def filter(self, qs, value): try: longitude = float(value[0]) latitude = float(value[1]) ahjs_to_search=qs.values_list('AHJID', flat=True) return ahj_gis_utils.filter_ahjs_by_location(longitude, latitude, ahjs_to_search=ahjs_to_search) except ValueError: return qs
def filter(self, qs, value): try: longitude = float(value[0]) latitude = float(value[1]) return ahj_gis_utils.filter_ahjs_by_location(longitude, latitude, ahjs_to_search=qs) except ValueError: return qs
def filter(self, qs, value): address = '' for string in value: address += string geocode_result = gmaps.geocode(address) if len(geocode_result) == 0: return qs ahj_qs = AHJ.objects.none() for x in range(len(geocode_result)): coordinates = geocode_result[x]['geometry']['location'] longitude = coordinates['lng'] latitude = coordinates['lat'] ahjs_to_search=qs.values_list('AHJID', flat=True) ahj_qs |= ahj_gis_utils.filter_ahjs_by_location(longitude, latitude, ahjs_to_search=ahjs_to_search) return ahj_qs
def list(self, request, *args, **kwargs): """ ListModelMixin: https://www.django-rest-framework.org/api-guide/generic-views/#listmodelmixin sends a response of a paginated list of AHJs with this JSON structure: - count: number of AHJs across all pages - next: the link to get the next page of AHJs (value null if no next page) - prev: the link to get the previous page (value null if no prev page) - results: list (or object containing the list) of serialized AHJ objects Default implementation: https://github.com/encode/django-rest-framework/blob/8351747d98b97907e6bb096914bf287a22c5314b/rest_framework/mixins.py#L33 Filtering of AHJs by address happens here; other filtering is done in filters.py This method was overridden to include latlng results of the address provided. """ queryset = self.filter_queryset(self.get_queryset()) location = get_location(request) # filter by latlng if given if location['Latitude']['Value'] is not None and location['Longitude'][ 'Value'] is not None: # mpoly of an AHJ refers to a Polygon object; mpoly of a Polygon refers to a MULTIPOLYGON # mpoly__mpoly__intersects looks for what AHJ's Polygon's MULTIPOLYGONS contain this point queryset = filter_ahjs_by_location( location['Longitude']['Value'], location['Latitude']['Value'], ahjs_to_search=queryset.values_list('AHJID', flat=True)) # construct the paginated response page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) # passing data object including latlng for results return self.get_paginated_response({ 'Location': location, 'ahjlist': serializer.data }) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data)