Skip to content

Instantly share code, notes, and snippets.

@thruflo
Created December 9, 2011 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thruflo/1452726 to your computer and use it in GitHub Desktop.
Save thruflo/1452726 to your computer and use it in GitHub Desktop.
Distance query snippet.
# Get a query for all `Message`s.
query = Message.query
# Get the query parameters (in this example from a `request` object).
params = request.params
lat = params['latitude']
lng = params['longitude']
distance = params.get('distance', None)
# Generate the `within` clause to filter by, either using a `distance`
# provided, or by working it out using our `get_distance` class method.
if not distance:
distance = Message.get_distance(query, lat, lng)
within = Message.within(lat, lng, distance)
# Filter by the `within` clause.
query = query.filter(within)
# Sort the results so the most recent come first.
query = query.order_by(Message.created.desc())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment