experiENS/avisstage/api.py
2017-04-16 00:03:04 +02:00

38 lines
1.2 KiB
Python

# coding: utf-8
from tastypie.resources import ModelResource
from avisstage.models import Lieu
from django.contrib.gis import geos
class LieuResource(ModelResource):
class Meta:
queryset = Lieu.objects.all()
resource_name = "lieu"
def build_filters(self, filters=None):
if filters is None:
filters = {}
orm_filters = super(LieuResource, self).build_filters()
if "lng" in filters and "lat" in filters:
lat = float(filters['lat'])
lng = float(filters['lng'])
pt = geos.Point((lng,lat), srid=4326)
self.reference_point = pt
orm_filters['coord__distance_lte'] = (pt, 50)
print orm_filters
return orm_filters
def dehydrate(self, bundle):
bundle = super(LieuResource, self).dehydrate(bundle)
obj = bundle.obj
bundle.data['coord'] = {'lat': float(obj.coord.y),
'lng': float(obj.coord.x)}
if "lat" in bundle.request.GET and "lng" in bundle.request.GET:
bundle.data['distance'] = self.reference_point.distance(bundle.obj.coord)
return bundle