mirror of
https://github.com/nikdoof/vapemap.git
synced 2025-12-20 21:29:21 +00:00
Initial Import.
This commit is contained in:
42
app/stores/views/search.py
Normal file
42
app/stores/views/search.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from django.views.generic import ListView
|
||||
from haystack.query import SearchQuerySet
|
||||
from haystack.utils.geo import Point, D
|
||||
from ..models import Store
|
||||
from ..utils import caching_geo_lookup
|
||||
|
||||
|
||||
class DistanceSearchView(ListView):
|
||||
|
||||
template_name = 'stores/store_search.html'
|
||||
distance = 25
|
||||
|
||||
def get_location(self):
|
||||
# TODO: geopy the location based on kwargs
|
||||
location = self.request.GET.get('location')
|
||||
lat = self.request.GET.get('lat')
|
||||
lng = self.request.GET.get('lng')
|
||||
if location:
|
||||
name, geo = caching_geo_lookup(location)
|
||||
elif lat and lng:
|
||||
name, geo = caching_geo_lookup('%s,%s' % (lat, lng))
|
||||
print name
|
||||
self.location_geo = geo
|
||||
|
||||
return Point(geo[1], geo[0])
|
||||
|
||||
def get_distance(self):
|
||||
return D(km=self.request.GET.get('distance', self.distance))
|
||||
|
||||
def get_queryset(self):
|
||||
location = self.get_location()
|
||||
distance = self.get_distance()
|
||||
print location, distance
|
||||
return SearchQuerySet().dwithin('location', location, distance).distance('location', location).order_by('-distance')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super(DistanceSearchView, self).get_context_data(**kwargs)
|
||||
ctx.update({
|
||||
'location': self.request.GET.get('location'),
|
||||
'location_geo': self.location_geo,
|
||||
})
|
||||
return ctx
|
||||
Reference in New Issue
Block a user