comparison 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
comparison
equal deleted inserted replaced
12:6d89ea94930b 13:94af113e498a
55 55
56 if startswith: 56 if startswith:
57 retval = [] 57 retval = []
58 for i in cities: 58 for i in cities:
59 if i[name].startswith(startswith): 59 if i[name].startswith(startswith):
60 retval.append(i[name]) 60 retval.append({"name": i[name],
61 return sorted(retval)[:limit] 61 "geonameid": i['geonameid']})
62 else: 62 else:
63 return sorted([i[name] for i in cities])[:limit] 63 retval = [{"name": i[name],
64 "geonameid": i['geonameid']}
65 for i in cities]
66 return sorted(retval)[:limit]
64 67
65 68
66 class Handler(object): 69 class Handler(object):
67 """base class for HTTP handler""" 70 """base class for HTTP handler"""
68 71
110 self.datafile = datafile 113 self.datafile = datafile
111 self.cities = read_city_list(self.datafile, 114 self.cities = read_city_list(self.datafile,
112 fields=fields) 115 fields=fields)
113 self.locations = locations(self.cities) 116 self.locations = locations(self.cities)
114 117
118 # get country codes
119 self.country_codes = sorted(set([city['country code']
120 for city in self.cities
121 if city['country code']]))
122
115 # convert cities to a dict for lookup 123 # convert cities to a dict for lookup
116 self.cities = {city['geonameid'] : city 124 self.cities = {city['geonameid'] : city
117 for city in self.cities} 125 for city in self.cities}
126
118 127
119 # declare handlers 128 # declare handlers
120 self.handlers = {'/cities': 129 self.handlers = {'/cities':
121 CitiesHandler(self.locations, 130 CitiesHandler(self.locations,
122 self.cities)} 131 self.cities)}
128 137
129 138
130 def GET(self, request): 139 def GET(self, request):
131 if request.path_info in ('', '/'): 140 if request.path_info in ('', '/'):
132 # Landing page 141 # Landing page
142 body = self.index.render(n_cities=len(self.cities),
143 country_codes=self.country_codes)
133 return Response(content_type=self.content_type, 144 return Response(content_type=self.content_type,
134 body=self.index.render(n_cities=len(self.cities))) 145 body=body)
146
135 elif request.path_info in self.handlers: 147 elif request.path_info in self.handlers:
136 return request.get_response(self.handlers[request.path_info]) 148 return request.get_response(self.handlers[request.path_info])
137 else: 149 else:
138 try: 150 try:
139 geoid = int(request.path.strip('/')) 151 geoid = int(request.path.strip('/'))