comparison globalneighbors/web.py @ 10:21ed15391e8a

add a placeholder view for a city based on geoid
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 12:28:36 -0700
parents 254195d0bac2
children d1b99c695511
comparison
equal deleted inserted replaced
9:638fad06e556 10:21ed15391e8a
18 from .read import read_city_list 18 from .read import read_city_list
19 from .schema import fields 19 from .schema import fields
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
24 here = os.path.dirname(os.path.abspath(__file__))
25 static_dir = os.path.join(here, 'static')
26
27
28 class PassthroughFileserver(object):
29
30 """serve files if they exist"""
31
32
33 def __init__(self, app, directory=static_dir):
34 self.app = app
35 self.directory = directory
36 self.fileserver = StaticURLParser(self.directory)
37
38 def __call__(self, environ, start_response):
39
40 path = environ['PATH_INFO'].strip('/')
41
42 if path and os.path.exists(os.path.join(self.directory, path)) and '..' not in path:
43
44 return self.fileserver(environ, start_response)
45 return self.app(environ, start_response)
23 46
24 47
25 def autocomplete(cities, startswith=None, limit=None): 48 def autocomplete(cities, startswith=None, limit=None):
26 """autocomplete function for city names""" 49 """autocomplete function for city names"""
27 ### TODO: 50 ### TODO:
62 self._cities = cities 85 self._cities = cities
63 86
64 87
65 def cities(self, startswith=None): 88 def cities(self, startswith=None):
66 """return list of cities""" 89 """return list of cities"""
67 return autocomplete(self._cities, 90 return autocomplete(self._cities.values(),
68 startswith=startswith) 91 startswith=startswith)
69 92
70 def GET(self, request): 93 def GET(self, request):
71 return Response(content_type=self.content_type, 94 return Response(content_type=self.content_type,
72 body=json.dumps(self.cities( 95 body=json.dumps(self.cities(
85 self.datafile = datafile 108 self.datafile = datafile
86 self.cities = read_city_list(self.datafile, 109 self.cities = read_city_list(self.datafile,
87 fields=fields) 110 fields=fields)
88 self.locations = locations(self.cities) 111 self.locations = locations(self.cities)
89 112
113 # convert cities to a dict for lookup
114 self.cities = {city['geonameid'] : city
115 for city in self.cities}
116
90 # declare handlers 117 # declare handlers
91 self.handlers = {'/cities': 118 self.handlers = {'/cities':
92 CitiesHandler(self.locations, 119 CitiesHandler(self.locations,
93 self.cities)} 120 self.cities)}
94 121
95 # template loader 122 # template loader
96 self.loader = TemplateLoader(template_dir) 123 self.loader = TemplateLoader(template_dir)
97 self.index = self.loader.load('index.html') 124 self.index = self.loader.load('index.html')
125 self.citypage = self.loader.load('city.html')
98 126
99 127
100 def GET(self, request): 128 def GET(self, request):
101 if request.path_info in ('', '/'): 129 if request.path_info in ('', '/'):
102 # Landing page 130 # Landing page
103 return Response(content_type=self.content_type, 131 return Response(content_type=self.content_type,
104 body=self.index.render(n_cities=len(self.cities))) 132 body=self.index.render(n_cities=len(self.cities)))
133 elif request.path_info in self.handlers:
134 return request.get_response(self.handlers[request.path_info])
105 else: 135 else:
106 handler = self.handlers.get(request.path_info) 136 try:
107 if handler: 137 geoid = int(request.path.strip('/'))
108 return request.get_response(handler) 138 city = self.cities.get(geoid)
139 if not city:
140 return
141 return Response(content_type=self.content_type,
142 body=self.citypage.render(**city))
143 except ValueError:
144 pass
109 145
110 146
111 147
112 def main(args=sys.argv[1:]): 148 def main(args=sys.argv[1:]):
113 """CLI""" 149 """CLI"""