Skip to content

Instantly share code, notes, and snippets.

@pySilver
Forked from readevalprint/serializer.py
Created August 3, 2014 00:06
Show Gist options
  • Save pySilver/b66cf9af4becbf569e9a to your computer and use it in GitHub Desktop.
Save pySilver/b66cf9af4becbf569e9a to your computer and use it in GitHub Desktop.
==== SNIP ====
class DistanceSerializer(serializers.Serializer):
km = serializers.FloatField()
m = serializers.FloatField()
mi = serializers.FloatField()
ft = serializers.FloatField()
class SearchResultSerializer(serializers.Serializer):
text = serializers.CharField()
pub_date = serializers.DateTimeField()
distance = fields.SerializerMethodField('_distance')
content_type = fields.CharField(source='model_name')
content_object = fields.SerializerMethodField('_content_object')
def _content_object(self, obj):
if obj.model_name == 'foo':
return FoSerializer(obj.object, many=False, context=self.context).data
if obj.model_name == 'bar':
return BarSerializer(obj.object, many=False, context=self.context).data
return {}
def __init__(self, *args, **kwargs):
self.unit = kwargs.pop('unit', None)
return super(SearchResultSerializer, self).__init__(*args, **kwargs)
def _distance(self, obj):
if self.unit:
return {self.unit: getattr(obj.distance, self.unit)}
try:
return DistanceSerializer(obj.distance, many=False).data
except Exception as e:
## Log this
return {}
=== SNIP ===
class SearchView(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = SearchResultSerializer
def get_queryset(self, *args, **kwargs):
# This will return a dict of the first known
# unit of distance found in the query
request = self.request
results = EmptySearchQuerySet()
if request.GET.get('q'):
form = ModelSearchForm(request.QUERY_PARAMS, searchqueryset=None, load_all=True)
if form.is_valid():
query = form.cleaned_data['q']
results = form.search()
else:
form = ModelSearchForm(searchqueryset=None, load_all=True)
if results.query.backend.include_spelling:
context['suggestion'] = form.get_suggestion()
distance = None
unit = None
try:
k,v = ((k,v) for k,v in request.QUERY_PARAMS.items() if k in D.UNITS.keys()).next()
distance = {k:v}
unit = k
except Exception as e:
logging.error(e)
point = None
try:
point = Point(float(request.QUERY_PARAMS['latitude']), float(request.QUERY_PARAMS['longitude']))
except Exception as e:
logging.error(e)
if distance and point:
results = results or SearchQuerySet()
results = results.dwithin('location', point, D(**distance)).distance('location', point)
return results
#return Response(SearchResultSerializer(sqs, many=True, unit=unit).data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment