comparison globalneighbors/web.py @ 22:e69cb496324e

we have a data dump
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 17:45:19 -0700
parents 2fef925fbf37
children 6891c5523b69
comparison
equal deleted inserted replaced
21:22c384fe954d 22:e69cb496324e
106 def GET(self, request): 106 def GET(self, request):
107 return Response(content_type=self.content_type, 107 return Response(content_type=self.content_type,
108 body=json.dumps(self.cities( 108 body=json.dumps(self.cities(
109 startswith=request.GET.get('term')))) 109 startswith=request.GET.get('term'))))
110 110
111 class NeighborsHandler(Handler):
112
113 content_type = 'application/json'
114
115 def __init__(self, neighbors):
116 self.neighbors = neighbors
117
118 def GET(self, request):
119 geoid = request.GET.get('geoid')
120 neighbors = self.neighbors.get(geoid, [])
121 return Response(content_type=self.content_type,
122 body=json.dumps(neighbors))
123
111 124
112 class GlobalHandler(Handler): 125 class GlobalHandler(Handler):
113 """WSGI HTTP Handler""" 126 """WSGI HTTP Handler"""
114 127
115 content_type = 'text/html' 128 content_type = 'text/html'
124 self.datafile = datafile 137 self.datafile = datafile
125 self.cities = read_city_list(self.datafile, 138 self.cities = read_city_list(self.datafile,
126 fields=fields) 139 fields=fields)
127 self.locations = locations(self.cities) 140 self.locations = locations(self.cities)
128 if neighbors_file: 141 if neighbors_file:
129 pass # TODO 142 self.neighbors = read_neighbors_file(neighbors_file)
143 else:
144 self.neighbors = None
130 145
131 # get country codes 146 # get country codes
132 self.country_codes = sorted(set([city['country code'] 147 self.country_codes = sorted(set([city['country code']
133 for city in self.cities 148 for city in self.cities
134 if city['country code']])) 149 if city['country code']]))
164 geoid = int(request.path.strip('/')) 179 geoid = int(request.path.strip('/'))
165 city = self.cities.get(geoid) 180 city = self.cities.get(geoid)
166 if not city: 181 if not city:
167 return 182 return
168 variables = dict(city=city) 183 variables = dict(city=city)
184 if self.neighbors:
185 import pdb; pdb.set_trace()
169 return Response(content_type=self.content_type, 186 return Response(content_type=self.content_type,
170 body=self.citypage.render(variables)) 187 body=self.citypage.render(variables))
171 except ValueError: 188 except ValueError:
172 pass 189 pass
173 190