changeset 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 638fad06e556
children d1b99c695511
files MANIFEST.in globalneighbors/grid.py globalneighbors/templates/city.html globalneighbors/templates/index.html globalneighbors/web.py setup.py
diffstat 6 files changed, 64 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- /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
--- 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, 
--- /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 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>{{ name }}</title>
+  </head>
+  <body>
+    <h1>{{ name }}</h1>
+  </body>
+</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
       });
       });
     </script>
--- 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
 
 
 
--- 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']