Add soil variant filter
This commit is contained in:
parent
cb348b0f5d
commit
cc3c3e7e9a
2 changed files with 16 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
Loading…
Reference in a new issue