Apply ignore filter rules
This commit is contained in:
parent
6540fe640b
commit
b3c576a214
3 changed files with 32 additions and 25 deletions
|
@ -3,11 +3,11 @@ import json
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.db.models import Q
|
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
|
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')
|
coordinates = request.query_params.get('coordinates')
|
||||||
|
|
||||||
if coordinates is not None:
|
if coordinates is not None:
|
||||||
|
@ -18,9 +18,12 @@ def coordinate_filter(request, queryset):
|
||||||
soillayer__geom__intersects=pnt).values_list('id', flat=True)
|
soillayer__geom__intersects=pnt).values_list('id', flat=True)
|
||||||
|
|
||||||
# Filter by ecological regions and soil orders
|
# Filter by ecological regions and soil orders
|
||||||
return queryset.filter(
|
if ignore_soil_order:
|
||||||
Q(ecological_regions__in=filtered_regions) &
|
return queryset.filter(ecological_regions__in=filtered_regions).distinct()
|
||||||
Q(soil_order__in=filtered_soil_orders)).distinct()
|
else:
|
||||||
|
return queryset.filter(
|
||||||
|
Q(ecological_regions__in=filtered_regions) &
|
||||||
|
Q(soil_order__in=filtered_soil_orders)).distinct()
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
@ -36,14 +39,8 @@ def soil_variant_filter(request, queryset):
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
def zone_filter(request, queryset):
|
def zone_filter(zone_json, queryset):
|
||||||
zone = request.query_params.get('zone')
|
return queryset.filter(zones__id__contains=zone_json['id']).distinct()
|
||||||
|
|
||||||
if zone != None:
|
|
||||||
zone_json = json.loads(zone)
|
|
||||||
return queryset.filter(zones__id__contains=zone_json['id']).distinct()
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
|
|
||||||
def soil_order_coordinate_filter(coordinates):
|
def soil_order_coordinate_filter(coordinates):
|
||||||
|
@ -61,3 +58,22 @@ def ecological_district_coordinate_filter(coordinates):
|
||||||
except EcologicalDistrictLayer.DoesNotExist:
|
except EcologicalDistrictLayer.DoesNotExist:
|
||||||
raise Http404(
|
raise Http404(
|
||||||
f"Ecological district layer cannot be found for point {pnt}")
|
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
|
||||||
|
|
|
@ -55,7 +55,7 @@ class ZoneSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Zone
|
model = Zone
|
||||||
fields = ['id', 'name', 'variant',
|
fields = ['id', 'name', 'variant',
|
||||||
'refined_variant', 'redirect_habitat']
|
'refined_variant', 'redirect_habitat', 'ignore_soil_order_filter', 'ignore_location_filter']
|
||||||
|
|
||||||
|
|
||||||
class ZoneImageSegmentSerializer(serializers.HyperlinkedModelSerializer):
|
class ZoneImageSegmentSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
|
|
@ -23,12 +23,7 @@ class PlantViewSet(viewsets.ModelViewSet):
|
||||||
""" Filtering plant query set by query parameters in the URL.
|
""" Filtering plant query set by query parameters in the URL.
|
||||||
(May want to eventually use django filters to break up the logic...)
|
(May want to eventually use django filters to break up the logic...)
|
||||||
"""
|
"""
|
||||||
queryset = Plant.objects.all()
|
return get_filtered_plants(self.request)
|
||||||
queryset = coordinate_filter(self.request, queryset)
|
|
||||||
queryset = soil_variant_filter(self.request, queryset)
|
|
||||||
queryset = zone_filter(self.request, queryset)
|
|
||||||
|
|
||||||
return queryset
|
|
||||||
|
|
||||||
|
|
||||||
class SoilOrderViewSet(viewsets.ModelViewSet):
|
class SoilOrderViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -106,11 +101,7 @@ class HabitatImageViewSet(viewsets.ViewSet):
|
||||||
class CSVDownloadView(viewsets.ViewSet):
|
class CSVDownloadView(viewsets.ViewSet):
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
filtered_plants = Plant.objects.all()
|
filtered_plants = get_filtered_plants(request)
|
||||||
filtered_plants = coordinate_filter(request, filtered_plants)
|
|
||||||
filtered_plants = soil_variant_filter(request, filtered_plants)
|
|
||||||
filtered_plants = zone_filter(request, filtered_plants)
|
|
||||||
|
|
||||||
create_plant_csv_file(request, filtered_plants)
|
create_plant_csv_file(request, filtered_plants)
|
||||||
|
|
||||||
csv_file = open(get_plant_csv_filepath(), 'rb')
|
csv_file = open(get_plant_csv_filepath(), 'rb')
|
||||||
|
|
Loading…
Reference in a new issue