# HG changeset patch # User Jeff Hammel # Date 1498418916 25200 # Node ID 21ed15391e8aac1af72092153350d7a3737f6de4 # Parent 638fad06e556567bd545a33df7e0d3b6afbe36f7 add a placeholder view for a city based on geoid diff -r 638fad06e556 -r 21ed15391e8a MANIFEST.in --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MANIFEST.in Sun Jun 25 12:28:36 2017 -0700 @@ -0,0 +1,2 @@ +graft globalneighbors/static +graft globalneighbors/templates \ No newline at end of file diff -r 638fad06e556 -r 21ed15391e8a globalneighbors/grid.py --- a/globalneighbors/grid.py Sun Jun 25 11:37:52 2017 -0700 +++ b/globalneighbors/grid.py Sun Jun 25 12:28:36 2017 -0700 @@ -59,4 +59,12 @@ def neighbor_points(self, i, j): """return all points in a lat-lon box""" - raise NotImplementedError('TODO') # -> record TODO items + geoids = set() + geoids.update(self[(i,j)]) + for ii, jj in self.neighbors(i,j): + geoids.update(self[ii,jj]) + return geoids + +class GriddedLocations(object): + + def __init__(self, diff -r 638fad06e556 -r 21ed15391e8a globalneighbors/templates/city.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globalneighbors/templates/city.html Sun Jun 25 12:28:36 2017 -0700 @@ -0,0 +1,9 @@ + + + + {{ name }} + + +

{{ name }}

+ + diff -r 638fad06e556 -r 21ed15391e8a globalneighbors/templates/index.html --- a/globalneighbors/templates/index.html Sun Jun 25 11:37:52 2017 -0700 +++ b/globalneighbors/templates/index.html Sun Jun 25 12:28:36 2017 -0700 @@ -10,7 +10,7 @@ source: "/cities", autoFocus: true, minLength: 3, - delay: 3000 + delay: 1500 }); }); diff -r 638fad06e556 -r 21ed15391e8a globalneighbors/web.py --- a/globalneighbors/web.py Sun Jun 25 11:37:52 2017 -0700 +++ b/globalneighbors/web.py Sun Jun 25 12:28:36 2017 -0700 @@ -21,6 +21,29 @@ from .template import template_dir from .template import TemplateLoader +here = os.path.dirname(os.path.abspath(__file__)) +static_dir = os.path.join(here, 'static') + + +class PassthroughFileserver(object): + + """serve files if they exist""" + + + def __init__(self, app, directory=static_dir): + self.app = app + self.directory = directory + self.fileserver = StaticURLParser(self.directory) + + def __call__(self, environ, start_response): + + path = environ['PATH_INFO'].strip('/') + + if path and os.path.exists(os.path.join(self.directory, path)) and '..' not in path: + + return self.fileserver(environ, start_response) + return self.app(environ, start_response) + def autocomplete(cities, startswith=None, limit=None): """autocomplete function for city names""" @@ -64,7 +87,7 @@ def cities(self, startswith=None): """return list of cities""" - return autocomplete(self._cities, + return autocomplete(self._cities.values(), startswith=startswith) def GET(self, request): @@ -87,6 +110,10 @@ fields=fields) self.locations = locations(self.cities) + # convert cities to a dict for lookup + self.cities = {city['geonameid'] : city + for city in self.cities} + # declare handlers self.handlers = {'/cities': CitiesHandler(self.locations, @@ -95,6 +122,7 @@ # template loader self.loader = TemplateLoader(template_dir) self.index = self.loader.load('index.html') + self.citypage = self.loader.load('city.html') def GET(self, request): @@ -102,10 +130,18 @@ # Landing page return Response(content_type=self.content_type, body=self.index.render(n_cities=len(self.cities))) + elif request.path_info in self.handlers: + return request.get_response(self.handlers[request.path_info]) else: - handler = self.handlers.get(request.path_info) - if handler: - return request.get_response(handler) + try: + geoid = int(request.path.strip('/')) + city = self.cities.get(geoid) + if not city: + return + return Response(content_type=self.content_type, + body=self.citypage.render(**city)) + except ValueError: + pass diff -r 638fad06e556 -r 21ed15391e8a setup.py --- a/setup.py Sun Jun 25 11:37:52 2017 -0700 +++ b/setup.py Sun Jun 25 12:28:36 2017 -0700 @@ -5,8 +5,10 @@ import os version = "0.0" -dependencies = ['gunicorn', +dependencies = ['Flask', + 'gunicorn', 'jinja2', + 'paste', 'webob']