Created
December 9, 2011 18:33
-
-
Save thruflo/1452726 to your computer and use it in GitHub Desktop.
Distance query snippet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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