diff globalneighbors/web.py @ 13:94af113e498a

we have links
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 14:04:49 -0700
parents d1b99c695511
children 27925261c137
line wrap: on
line diff
--- a/globalneighbors/web.py	Sun Jun 25 13:18:13 2017 -0700
+++ b/globalneighbors/web.py	Sun Jun 25 14:04:49 2017 -0700
@@ -57,10 +57,13 @@
         retval = []
         for i in cities:
             if i[name].startswith(startswith):
-                    retval.append(i[name])
-        return sorted(retval)[:limit]
+                retval.append({"name": i[name],
+                               "geonameid": i['geonameid']})
     else:
-        return sorted([i[name] for i in cities])[:limit]
+        retval = [{"name": i[name],
+                  "geonameid": i['geonameid']}
+                   for i in cities]
+    return sorted(retval)[:limit]
 
 
 class Handler(object):
@@ -112,10 +115,16 @@
                                      fields=fields)
         self.locations = locations(self.cities)
 
+        # get country codes
+        self.country_codes = sorted(set([city['country code']
+                                         for city in self.cities
+                                         if city['country code']]))
+
         # convert cities to a dict for lookup
         self.cities = {city['geonameid'] : city
                        for city in self.cities}
 
+
         # declare handlers
         self.handlers = {'/cities':
                          CitiesHandler(self.locations,
@@ -130,8 +139,11 @@
     def GET(self, request):
         if request.path_info in ('', '/'):
             # Landing page
+            body = self.index.render(n_cities=len(self.cities),
+                                     country_codes=self.country_codes)
             return Response(content_type=self.content_type,
-                            body=self.index.render(n_cities=len(self.cities)))
+                            body=body)
+
         elif request.path_info in self.handlers:
             return request.get_response(self.handlers[request.path_info])
         else: