Mercurial > hg > GlobalNeighbors
comparison 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 |
comparison
equal
deleted
inserted
replaced
6:316e1d54ffd4 | 7:254195d0bac2 |
---|---|
20 from .schema import name | 20 from .schema import name |
21 from .template import template_dir | 21 from .template import template_dir |
22 from .template import TemplateLoader | 22 from .template import TemplateLoader |
23 | 23 |
24 | 24 |
25 def autocomplete(cities, startswith=None): | 25 def autocomplete(cities, startswith=None, limit=None): |
26 """autocomplete function for city names""" | 26 """autocomplete function for city names""" |
27 ### TODO: sort once, ahead of time | 27 ### TODO: |
28 # - sort once, ahead of time | |
29 # - return most populous cities | |
28 | 30 |
29 if startswith: | 31 if startswith: |
30 retval = [] | 32 retval = [] |
31 for i in cities: | 33 for i in cities: |
32 try: | 34 if i[name].startswith(startswith): |
33 if i[name].startswith(startswith): | |
34 retval.append(i[name]) | 35 retval.append(i[name]) |
35 except Exception as e: | 36 return sorted(retval)[:limit] |
36 import pdb; pdb.set_trace() | |
37 return sorted(retval) | |
38 else: | 37 else: |
39 return sorted([i[name] for i in cities]) | 38 return sorted([i[name] for i in cities])[:limit] |
40 | 39 |
41 | 40 |
42 class Handler(object): | 41 class Handler(object): |
43 """base class for HTTP handler""" | 42 """base class for HTTP handler""" |
44 | 43 |
69 startswith=startswith) | 68 startswith=startswith) |
70 | 69 |
71 def GET(self, request): | 70 def GET(self, request): |
72 return Response(content_type=self.content_type, | 71 return Response(content_type=self.content_type, |
73 body=json.dumps(self.cities( | 72 body=json.dumps(self.cities( |
74 startswith=request.GET.get('q')))) | 73 startswith=request.GET.get('term')))) |
75 | 74 |
76 | 75 |
77 class GlobalHandler(Handler): | 76 class GlobalHandler(Handler): |
78 """WSGI HTTP Handler""" | 77 """WSGI HTTP Handler""" |
79 | 78 |