From de0b5abb7bf3ae3db59dd3392346a80aed9cb96e Mon Sep 17 00:00:00 2001 From: Dana Lambert Date: Fri, 5 Nov 2021 16:17:29 +1300 Subject: [PATCH] Add zone filter on backend --- backend/right_tree/api/filters.py | 10 +++++++++- backend/right_tree/api/views.py | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/right_tree/api/filters.py b/backend/right_tree/api/filters.py index 21d291d..28ad173 100644 --- a/backend/right_tree/api/filters.py +++ b/backend/right_tree/api/filters.py @@ -6,6 +6,7 @@ from django.db.models import Q from .models import EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant from .utils import get_point_from_coordinates + def coordinate_filter(request, queryset): coordinates = request.query_params.get('coordinates') @@ -35,6 +36,12 @@ def soil_variant_filter(request, 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): pnt = get_point_from_coordinates(coordinates) try: @@ -48,4 +55,5 @@ def ecological_district_coordinate_filter(coordinates): try: return EcologicalDistrictLayer.objects.filter(geom__intersects=pnt) 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}") diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py index 8cad196..c318f39 100644 --- a/backend/right_tree/api/views.py +++ b/backend/right_tree/api/views.py @@ -29,6 +29,7 @@ class PlantViewSet(viewsets.ModelViewSet): queryset = Plant.objects.all() queryset = coordinate_filter(self.request, queryset) queryset = soil_variant_filter(self.request, queryset) + queryset = zone_filter(self.request, queryset) return queryset