2021-10-22 16:01:38 +13:00
|
|
|
import json
|
|
|
|
|
|
|
|
from django.contrib.gis.geos import Point
|
|
|
|
from django.contrib.gis.db import models
|
|
|
|
from django.http import HttpResponseBadRequest
|
|
|
|
|
2021-10-07 08:59:57 +13:00
|
|
|
from rest_framework import viewsets
|
2021-10-22 16:01:38 +13:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2021-11-05 11:16:53 +13:00
|
|
|
from right_tree.api.models import Habitat, Plant, EcologicalDistrictLayer, SoilOrder
|
|
|
|
from right_tree.api.serializers import HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer
|
2021-10-22 16:01:38 +13:00
|
|
|
|
|
|
|
from .filters import *
|
|
|
|
from .utils import get_address_from_coordinates
|
2021-10-07 08:59:57 +13:00
|
|
|
|
2021-11-05 11:16:53 +13:00
|
|
|
|
2021-10-07 08:59:57 +13:00
|
|
|
class PlantViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
2021-10-20 12:36:48 +13:00
|
|
|
Filtered viewset for plants.
|
2021-10-07 08:59:57 +13:00
|
|
|
"""
|
|
|
|
queryset = Plant.objects.all()
|
|
|
|
serializer_class = PlantSerializer
|
2021-10-19 16:06:21 +13:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
""" 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)
|
2021-10-20 12:36:48 +13:00
|
|
|
queryset = soil_variant_filter(self.request, queryset)
|
2021-10-19 16:06:21 +13:00
|
|
|
|
|
|
|
return queryset
|
2021-10-22 16:01:38 +13:00
|
|
|
|
2021-11-05 11:16:53 +13:00
|
|
|
|
2021-10-22 16:01:38 +13:00
|
|
|
class SoilOrderViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
Filtered viewset for soil details.
|
|
|
|
"""
|
|
|
|
serializer_class = SoilOrderSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
""" Filtering soil order query set by coordinate parameters in the URL.
|
|
|
|
"""
|
|
|
|
coordinates = self.request.query_params.get('coordinates')
|
|
|
|
if coordinates is not None:
|
|
|
|
return soil_order_coordinate_filter(coordinates)
|
|
|
|
|
|
|
|
return SoilOrder.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class EcologicalDistrictViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
Filtered viewset for ecological district/region details.
|
|
|
|
"""
|
|
|
|
serializer_class = EcologicalDistrictLayerSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
""" Filtering ecological district/region query set by coordinate parameters in the URL.
|
|
|
|
"""
|
|
|
|
coordinates = self.request.query_params.get('coordinates')
|
|
|
|
if coordinates is not None:
|
|
|
|
return ecological_district_coordinate_filter(coordinates)
|
|
|
|
|
|
|
|
return EcologicalDistrictLayer.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class LINZPropertyViewSet(viewsets.ViewSet):
|
|
|
|
"""
|
|
|
|
Filtered viewset for ecological district/region details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def list(self, request):
|
|
|
|
coordinates = self.request.query_params.get('coordinates')
|
|
|
|
if coordinates is not None:
|
|
|
|
address_data = get_address_from_coordinates(coordinates)
|
|
|
|
serializer = AddressSerializer(address_data)
|
2021-11-05 11:16:53 +13:00
|
|
|
return Response(serializer.data)
|
|
|
|
else:
|
2021-10-22 16:01:38 +13:00
|
|
|
return HttpResponseBadRequest("No coordinate given.")
|
2021-11-05 11:16:53 +13:00
|
|
|
|
|
|
|
|
|
|
|
class HabitatViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
Viewset for all habitats.
|
|
|
|
"""
|
|
|
|
serializer_class = HabitatSerializer
|
|
|
|
queryset = Habitat.objects.all()
|