Permalink
Browse files

And adding basic geo search to prove it works

  • Loading branch information...
1 parent 9659b17 commit fa2dcf8f6bae44f80b5b75d93d518b212c96a62e Lisa van Gelder committed Dec 20, 2011
Showing with 63 additions and 4 deletions.
  1. +5 −1 venues/forms.py
  2. +2 −2 venues/geosearch.py
  3. +6 −0 venues/models.py
  4. +38 −0 venues/templates/search.html
  5. +12 −1 venues/views.py
View
@@ -10,4 +10,8 @@ class VenueForm(wtf.Form):
street = wtf.TextField('Street', validators=[validators.Required()])
number = wtf.TextField('Number', validators=[validators.Required()])
lat = wtf.TextField('Lat', validators=[validators.Required()])
- long = wtf.TextField('Long', validators=[validators.Required()])
+ long = wtf.TextField('Long', validators=[validators.Required()])
+
+
+class SearchForm(wtf.Form):
+ location = wtf.TextField('Postcode or City', validators=[validators.Required()])
View
@@ -25,7 +25,7 @@ def get_geo_loc_twice(postcode):
return get_geolocation_including_city(postcode)
def get_geolocation(postcode):
- url = "http://maps.google.com/maps/geo?q="+postcode+",UK&output=json&sensor=false&key="+settings.GOOGLE_MAPS_KEY
+ url = "http://maps.google.com/maps/geo?q="+postcode+",UK&output=json&sensor=false"
location = get_from_geo(url)
status = location["Status"]["code"]
if status != 200:
@@ -41,7 +41,7 @@ def get_geolocation(postcode):
def get_geolocation_including_city(postcode):
- url = "http://maps.google.com/maps/geo?q="+postcode+",UK&output=json&sensor=false&key="+settings.GOOGLE_MAPS_KEY
+ url = "http://maps.google.com/maps/geo?q="+postcode+",UK&output=json&sensor=false"
location = get_from_geo(url)
status = location["Status"]["code"]
if status != 200:
View
@@ -22,3 +22,9 @@ class Venue(GeoModel):
street = db.StringProperty(required=True)
number = db.StringProperty(required=True)
+def get_venues_by_bounding_box(lat, long, bbox_side_in_miles=50, max_results=250):
+
+ bounding_box = geohelpers.square_bounding_box_centered_at(float(lat), float(long), bbox_side_in_miles)
+ base_query = Venue.all().order('-name')
+ return Venue.bounding_box_fetch(base_query, bounding_box, max_results = max_results)
+
@@ -0,0 +1,38 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+
+
+<ul>
+ {% if results %}
+ {% for venue in results %}
+ <li>
+ {{ venue.name }}
+
+ {{ venue.description }}
+ </li>
+ {% endfor %}
+ {% endif %}
+</ul>
+
+{% if not results %}
+<form action="{{ url_for('search') }}" method="post" accept-charset="utf-8">
+ {{ form.csrf_token }}
+ <p>
+ <label for="location">{{ form.location.label }}</label>
+
+ {{ form.location|safe }}
+
+ {% if form.location.errors %}
+ <ul class="errors">
+ {% for error in form.location.errors %}
+ <li>{{ error }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+ </p>
+ <p><input type="submit" value="Search"/></p>
+</form>
+ {% endif %}
+{% endblock %}
View
@@ -1,8 +1,9 @@
from flask import render_template, flash, url_for, redirect
from flask.helpers import flash
from venues import app
-from forms import VenueForm
+from forms import VenueForm, SearchForm
import models
+import geosearch
from google.appengine.ext import db
import simplejson
@@ -17,6 +18,16 @@ def list_venues():
venues = models.Venue.all()
return render_template('list_venues.html', venues=venues)
+
+@app.route('/search', methods=['GET','POST'])
+def search():
+ form = SearchForm()
+ if form.validate_on_submit():
+ longitude, latitude, city = geosearch.get_geo_loc_twice(form.location.data)
+ results = models.get_venues_by_bounding_box(latitude, longitude)
+ return render_template('search.html', results=results, form=form)
+ return render_template('search.html', form=form, results=None)
+
@app.route('/venues/new', methods=['GET', 'POST'])
def new_venue():
"""createVenue view."""

0 comments on commit fa2dcf8

Please sign in to comment.