comparison globalneighbors/web.py @ 1:1b94f3bf97e5

* limit distance function * start gridding * improve unicode handling
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 14:02:14 -0700
parents 5dba84370182
children 316e1d54ffd4
comparison
equal deleted inserted replaced
0:5dba84370182 1:1b94f3bf97e5
15 from .locations import city_dict 15 from .locations import city_dict
16 from .read import read_cities 16 from .read import read_cities
17 from .read import read_city_list 17 from .read import read_city_list
18 from .schema import fields 18 from .schema import fields
19 from .schema import name 19 from .schema import name
20
21
22 def autocomplete(cities, startswith=None):
23 """autocomplete function for city names"""
24 ### TODO: sort once, ahead of time
25
26 if startswith:
27 retval = []
28 for i in cities:
29 try:
30 if i[name].startswith(startswith):
31 retval.append(i[name])
32 except Exception as e:
33 import pdb; pdb.set_trace()
34 return sorted(retval)
35 else:
36 return sorted([i[name] for i in cities])
20 37
21 38
22 class Handler(object): 39 class Handler(object):
23 """base class for HTTP handler""" 40 """base class for HTTP handler"""
24 41
43 self._cities = cities 60 self._cities = cities
44 61
45 62
46 def cities(self, startswith=None): 63 def cities(self, startswith=None):
47 """return list of cities""" 64 """return list of cities"""
48 65 return autocomplete(self._cities,
49 if startswith: 66 startswith=startswith)
50 return sorted([i[name] for i in self._cities
51 if i[name].startswith(startswith)])
52 else:
53 return sorted([i[name] for i in self._cities])
54 67
55 def GET(self, request): 68 def GET(self, request):
56 return Response(content_type=self.content_type, 69 return Response(content_type=self.content_type,
57 body=json.dumps(self.cities( 70 body=json.dumps(self.cities(
58 startswith=request.GET.get('q')))) 71 startswith=request.GET.get('q'))))