Add soil variant filter

This commit is contained in:
Dana Lambert 2021-10-20 12:36:48 +13:00
parent cb348b0f5d
commit cc3c3e7e9a
2 changed files with 16 additions and 4 deletions

View file

@ -3,7 +3,7 @@ import json
from django.contrib.gis.geos import Point
from django.db.models import Q
from .models import EcologicalRegion, EcologicalDistrictLayer, SoilOrder
from .models import EcologicalRegion, EcologicalDistrictLayer, SoilOrder, SoilVariant
def coordinate_filter(request, queryset):
@ -23,6 +23,17 @@ def coordinate_filter(request, queryset):
# Filter by ecological regions and soil orders
return queryset.filter(
Q(ecological_regions__in=filtered_regions) &
Q(soil_order__in=filtered_soil_orders))
Q(soil_order__in=filtered_soil_orders)).distinct()
return queryset
def soil_variant_filter(request, queryset):
soil_variant = request.query_params.get('soilVariant')
if soil_variant in {"D", "W", "M"}:
soil_variant_ids = SoilVariant.objects.filter(Q(name__startswith=soil_variant) | Q(
name__startswith="M")).values_list('id', flat=True).distinct()
return queryset.filter(soil_variants__in=soil_variant_ids).distinct()
return queryset

View file

@ -1,11 +1,11 @@
from rest_framework import viewsets
from right_tree.api.models import Plant
from right_tree.api.serializers import PlantSerializer
from .filters import coordinate_filter
from .filters import coordinate_filter, soil_variant_filter
class PlantViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows hours to be grouped or edited.
Filtered viewset for plants.
"""
queryset = Plant.objects.all()
serializer_class = PlantSerializer
@ -16,6 +16,7 @@ class PlantViewSet(viewsets.ModelViewSet):
"""
queryset = Plant.objects.all()
queryset = coordinate_filter(self.request, queryset)
queryset = soil_variant_filter(self.request, queryset)
return queryset