Apply ignore filter rules

This commit is contained in:
Dana Lambert 2021-11-10 14:15:25 +13:00
parent 6540fe640b
commit b3c576a214
3 changed files with 32 additions and 25 deletions

View file

@ -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,6 +18,9 @@ def coordinate_filter(request, queryset):
soillayer__geom__intersects=pnt).values_list('id', flat=True)
# Filter by ecological regions and soil orders
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()
@ -36,15 +39,9 @@ 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)
def zone_filter(zone_json, queryset):
return queryset.filter(zones__id__contains=zone_json['id']).distinct()
return queryset
def soil_order_coordinate_filter(coordinates):
pnt = get_point_from_coordinates(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

View file

@ -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):

View file

@ -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')