0
from geopy import distance as geopy_distance
0
from django.db import models
0
from django.conf import settings
0
from django.utils.translation import ugettext_lazy as _
0
-from geo import fields as custom_fields
0
-from geo import geocoding, misc
0
+from geo import geocoding, managers, fields as custom_fields
0
from geo.dateutil.relativedelta import relativedelta
0
-class LocationManager(models.Manager):
0
- def by_proximity_to_location(self, origin_location, radius_miles=None):
0
- """Returns a list of all self.model objects (excluding the origin_location)
0
- within radius_miles miles of the passed location if specified (otherwise
0
- returns all other objects), ordered by ascending proximity to it."""
0
- if radius_miles is not None:
0
- # It is assumed that 1 degree of latitude and longitude is equal to
0
- # 75 miles (this is actually overestimated)
0
- 'minimum': origin_location.latitude - (radius_miles / 75),
0
- 'maximum': origin_location.latitude + (radius_miles / 75),
0
- 'minimum': origin_location.longitude - (radius_miles / 75),
0
- 'maximum': origin_location.longitude + (radius_miles / 75),
0
- results = self.model.objects.filter(latitude__range=(coord_set['latitude']['minimum'], coord_set['latitude']['maximum'])).filter(longitude__range=(coord_set['longitude']['minimum'], coord_set['longitude']['maximum']))
0
- results = self.model.objects.all()
0
- # Exclude any locations with exactly the same co-ordinates (GeoPy doesn't play nice with these)
0
- results = list(results.exclude(latitude__exact=origin_location.latitude).exclude(longitude__exact=origin_location.longitude))
0
- # And any that don't actually fall within the radius (accurately calculated)
0
- if radius_miles is not None:
0
- for result in results:
0
- if geopy_distance.distance((result.latitude, result.longitude), (origin_location.latitude, origin_location.longitude)).miles > radius_miles:
0
- results.remove(result)
0
- def proximity_cmp(current, previous, location=origin_location):
0
- return misc.base_cmp_by_proximity(current, previous, location.coords_tuple)
0
- results.sort(proximity_cmp)
0
- # Backwards-compatibility
0
- by_prox = by_proximity_to_location
0
- """Returns all self.model objects which have is_public set as True (convenience function)."""
0
- return self.model.objects.filter(is_public=True)
0
- """Returns all self.model objects which have expired (convenience function)."""
0
- return self.model.objects.filter(refreshed__lte=(datetime.datetime.now() - relativedelta.relativedelta(**settings.MAX_LOCATION_CACHE_AGE)))
0
- def within_bounds(self, north_west, south_east):
0
- """Returns a QuerySet of self.models within the supplied lat/long two-tuples (the northwest
0
- and southwest-most corners bounding the segment of the earth in which to search)."""
0
- return self.model.objects.filter(latitude__range=(north_west[0], south_east[0])).filter(longitude__range=(north_west[1], south_east[1]))
0
class Location(models.Model):
0
"""A Location on the earth."""
0
query = models.CharField(_('Location'), max_length=250, blank=False, null=False, unique=True)
0
@@ -77,7 +21,7 @@ class Location(models.Model):
0
created = models.DateTimeField(editable=False, blank=True, null=True, default=datetime.datetime.now())
0
is_public = models.BooleanField(default=True)
0
- objects =
LocationManager()
0
+ objects =
managers.LocationManager()
0
list_display = ('__str__', 'latitude', 'longitude', 'created', 'refreshed')
Comments
No one has commented yet.