diff globalneighbors/web.py @ 7:254195d0bac2

partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 09:13:48 -0700
parents 316e1d54ffd4
children 21ed15391e8a
line wrap: on
line diff
--- a/globalneighbors/web.py	Sat Jun 24 15:47:59 2017 -0700
+++ b/globalneighbors/web.py	Sun Jun 25 09:13:48 2017 -0700
@@ -22,21 +22,20 @@
 from .template import TemplateLoader
 
 
-def autocomplete(cities, startswith=None):
+def autocomplete(cities, startswith=None, limit=None):
     """autocomplete function for city names"""
-    ### TODO: sort once, ahead of time
+    ### TODO:
+    # - sort once, ahead of time
+    # - return most populous cities
 
     if startswith:
         retval = []
         for i in cities:
-            try:
-                if i[name].startswith(startswith):
+            if i[name].startswith(startswith):
                     retval.append(i[name])
-            except Exception as e:
-                import pdb; pdb.set_trace()
-        return sorted(retval)
+        return sorted(retval)[:limit]
     else:
-        return sorted([i[name] for i in cities])
+        return sorted([i[name] for i in cities])[:limit]
 
 
 class Handler(object):
@@ -71,7 +70,7 @@
     def GET(self, request):
         return Response(content_type=self.content_type,
                         body=json.dumps(self.cities(
-                            startswith=request.GET.get('q'))))
+                            startswith=request.GET.get('term'))))
 
 
 class GlobalHandler(Handler):