changeset 13:94af113e498a

we have links
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 14:04:49 -0700
parents 6d89ea94930b
children 27925261c137
files globalneighbors/templates/index.html globalneighbors/web.py tests/test_web.py
diffstat 3 files changed, 40 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/globalneighbors/templates/index.html	Sun Jun 25 13:18:13 2017 -0700
+++ b/globalneighbors/templates/index.html	Sun Jun 25 14:04:49 2017 -0700
@@ -3,29 +3,27 @@
   <head>
     <title>Global Neighbors</title>
     <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
-    <!-- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> -->
-    <!-- <script> -->
-    <!--   $( function() { -->
-    <!--   $( "#cities" ).autocomplete({ -->
-    <!--   source: "/cities", -->
-    <!--   autoFocus: true, -->
-    <!--   minLength: 3, -->
-    <!--   delay: 1500 -->
-    <!--   }); -->
-    <!--   }); -->
-    <!-- </script> -->
     <script src="/js/jquery.easy-autocomplete.min.js"></script>
     <link rel="stylesheet" type="text/css" href="/css/style.css"/>
     <link rel="stylesheet" type="text/css" href="/css/easy-autocomplete.css"/>
     <script>
       $( function() {
       var options = {
-      url: "/cities",
+      url: function(phrase) {
+      return "/cities?term=" + phrase;
+      },
+      getValue: "name",
       list: {
 		match: {
 			enabled: true
-		}
-	}
+            }
+      },
+      template: {
+        type: "custom",
+        method: function (value, item) {
+          return '<a href="/' + item.geonameid + '">' + value + '</a>';
+        }
+      }
       };
       $( "#cities" ).easyAutocomplete(options);
       });
@@ -38,6 +36,12 @@
     <div class="ui-widget">
       <label for="cities">Enter a city: </label>
       <input id="cities"/>
+      <select>
+        <option value="">ALL</option>
+        {% for country_code in country_codes %}
+        <option value="{{ country_code }}">{{ country_code }}</option>
+        {% endfor %}
+      </select>
     </div>
 
   </body>
--- 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:
--- a/tests/test_web.py	Sun Jun 25 13:18:13 2017 -0700
+++ b/tests/test_web.py	Sun Jun 25 14:04:49 2017 -0700
@@ -24,10 +24,12 @@
         # Let's look for Chicago
         q = u'Ch'
         results = autocomplete(cities, q)
-        assert all([result.startswith(q)
-                    for result in results])
-        assert sorted(results) == results
-        assert 'Chicago' in results
+        names = [result['name']
+                 for result in results]
+        assert all([name.startswith(q)
+                    for name in names])
+        assert sorted(names) == names
+        assert 'Chicago' in names
 
 
 if __name__ == '__main__':