Add zone filter on backend

This commit is contained in:
Dana Lambert 2021-11-05 16:17:29 +13:00
parent 98a679eaf3
commit de0b5abb7b
2 changed files with 10 additions and 1 deletions

View file

@ -6,6 +6,7 @@ from django.db.models import Q
from .models import EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant from .models import EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant
from .utils import get_point_from_coordinates from .utils import get_point_from_coordinates
def coordinate_filter(request, queryset): def coordinate_filter(request, queryset):
coordinates = request.query_params.get('coordinates') coordinates = request.query_params.get('coordinates')
@ -35,6 +36,12 @@ def soil_variant_filter(request, queryset):
return queryset return queryset
def zone_filter(request, queryset):
zone = request.query_params.get('zone')
zone_json = json.loads(zone)
return queryset.filter(zones__id__contains=zone_json['id']).distinct()
def soil_order_coordinate_filter(coordinates): def soil_order_coordinate_filter(coordinates):
pnt = get_point_from_coordinates(coordinates) pnt = get_point_from_coordinates(coordinates)
try: try:
@ -48,4 +55,5 @@ def ecological_district_coordinate_filter(coordinates):
try: try:
return EcologicalDistrictLayer.objects.filter(geom__intersects=pnt) return EcologicalDistrictLayer.objects.filter(geom__intersects=pnt)
except EcologicalDistrictLayer.DoesNotExist: except EcologicalDistrictLayer.DoesNotExist:
raise Http404(f"Ecological district layer cannot be found for point {pnt}") raise Http404(
f"Ecological district layer cannot be found for point {pnt}")

View file

@ -29,6 +29,7 @@ class PlantViewSet(viewsets.ModelViewSet):
queryset = Plant.objects.all() queryset = Plant.objects.all()
queryset = coordinate_filter(self.request, queryset) queryset = coordinate_filter(self.request, queryset)
queryset = soil_variant_filter(self.request, queryset) queryset = soil_variant_filter(self.request, queryset)
queryset = zone_filter(self.request, queryset)
return queryset return queryset