From b3c576a214c868b71481f05bbe6755fb3d7c3278 Mon Sep 17 00:00:00 2001 From: Dana Lambert Date: Wed, 10 Nov 2021 14:15:25 +1300 Subject: [PATCH] Apply ignore filter rules --- backend/right_tree/api/filters.py | 42 ++++++++++++++++++--------- backend/right_tree/api/serializers.py | 2 +- backend/right_tree/api/views.py | 13 ++------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/backend/right_tree/api/filters.py b/backend/right_tree/api/filters.py index f7bf4f4..ee2cfd4 100644 --- a/backend/right_tree/api/filters.py +++ b/backend/right_tree/api/filters.py @@ -3,11 +3,11 @@ import json from django.http import Http404 from django.db.models import Q -from .models import EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant +from .models import Plant, EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant from .utils import get_point_from_coordinates -def coordinate_filter(request, queryset): +def coordinate_filter(request, queryset, ignore_soil_order=False): coordinates = request.query_params.get('coordinates') if coordinates is not None: @@ -18,9 +18,12 @@ def coordinate_filter(request, queryset): soillayer__geom__intersects=pnt).values_list('id', flat=True) # Filter by ecological regions and soil orders - return queryset.filter( - Q(ecological_regions__in=filtered_regions) & - Q(soil_order__in=filtered_soil_orders)).distinct() + if ignore_soil_order: + return queryset.filter(ecological_regions__in=filtered_regions).distinct() + else: + return queryset.filter( + Q(ecological_regions__in=filtered_regions) & + Q(soil_order__in=filtered_soil_orders)).distinct() return queryset @@ -36,14 +39,8 @@ def soil_variant_filter(request, queryset): return queryset -def zone_filter(request, queryset): - zone = request.query_params.get('zone') - - if zone != None: - zone_json = json.loads(zone) - return queryset.filter(zones__id__contains=zone_json['id']).distinct() - - return queryset +def zone_filter(zone_json, queryset): + return queryset.filter(zones__id__contains=zone_json['id']).distinct() def soil_order_coordinate_filter(coordinates): @@ -61,3 +58,22 @@ def ecological_district_coordinate_filter(coordinates): except EcologicalDistrictLayer.DoesNotExist: raise Http404( f"Ecological district layer cannot be found for point {pnt}") + + +def get_filtered_plants(request): + filtered_plants = Plant.objects.all() + + zone = request.query_params.get('zone') + if zone != None: + zone_json = json.loads(zone) + filtered_plants = zone_filter(zone_json, filtered_plants) + + if not zone_json['ignore_location_filter']: + filtered_plants = coordinate_filter( + request, filtered_plants, ignore_soil_order=zone_json['ignore_soil_order_filter']) + else: + filtered_plants = coordinate_filter(request, filtered_plants) + + filtered_plants = soil_variant_filter(request, filtered_plants) + + return filtered_plants diff --git a/backend/right_tree/api/serializers.py b/backend/right_tree/api/serializers.py index 5d6eda8..2fd847c 100644 --- a/backend/right_tree/api/serializers.py +++ b/backend/right_tree/api/serializers.py @@ -55,7 +55,7 @@ class ZoneSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Zone fields = ['id', 'name', 'variant', - 'refined_variant', 'redirect_habitat'] + 'refined_variant', 'redirect_habitat', 'ignore_soil_order_filter', 'ignore_location_filter'] class ZoneImageSegmentSerializer(serializers.HyperlinkedModelSerializer): diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py index 63519f7..6bcce23 100644 --- a/backend/right_tree/api/views.py +++ b/backend/right_tree/api/views.py @@ -23,12 +23,7 @@ class PlantViewSet(viewsets.ModelViewSet): """ Filtering plant query set by query parameters in the URL. (May want to eventually use django filters to break up the logic...) """ - 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 + return get_filtered_plants(self.request) class SoilOrderViewSet(viewsets.ModelViewSet): @@ -106,11 +101,7 @@ class HabitatImageViewSet(viewsets.ViewSet): class CSVDownloadView(viewsets.ViewSet): def list(self, request, *args, **kwargs): - filtered_plants = Plant.objects.all() - filtered_plants = coordinate_filter(request, filtered_plants) - filtered_plants = soil_variant_filter(request, filtered_plants) - filtered_plants = zone_filter(request, filtered_plants) - + filtered_plants = get_filtered_plants(request) create_plant_csv_file(request, filtered_plants) csv_file = open(get_plant_csv_filepath(), 'rb')