2021-11-18 11:06:21 +13:00
|
|
|
from django.http import HttpResponseBadRequest, HttpResponse, FileResponse
|
|
|
|
from django.shortcuts import get_object_or_404
|
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-18 11:06:21 +13:00
|
|
|
from wsgiref.util import FileWrapper
|
2021-10-22 16:01:38 +13:00
|
|
|
|
2021-11-05 14:22:07 +13:00
|
|
|
from right_tree.api.models import Habitat, HabitatImage, Plant, EcologicalDistrictLayer, SoilOrder
|
|
|
|
from right_tree.api.serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer
|
2021-10-22 16:01:38 +13:00
|
|
|
|
|
|
|
from .filters import *
|
2021-11-18 11:06:21 +13:00
|
|
|
from .wms_utils import get_address_from_coordinates
|
|
|
|
from .resource_generation_utils import create_plant_csv_file, get_plant_resource_filepath, create_planting_guide_pdf, PLANTING_GUIDE_PDF_FILENAME
|
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-11-18 11:06:21 +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...)
|
|
|
|
"""
|
2021-11-10 14:15:25 +13:00
|
|
|
return get_filtered_plants(self.request)
|
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):
|
2021-11-18 11:06:21 +13:00
|
|
|
""" Filtered viewset for soil details.
|
2021-10-22 16:01:38 +13:00
|
|
|
"""
|
|
|
|
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):
|
2021-11-18 11:06:21 +13:00
|
|
|
""" Filtered viewset for ecological district/region details.
|
2021-10-22 16:01:38 +13:00
|
|
|
"""
|
|
|
|
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):
|
2021-11-18 11:06:21 +13:00
|
|
|
""" Filtered viewset for ecological district/region details.
|
2021-10-22 16:01:38 +13:00
|
|
|
"""
|
|
|
|
|
|
|
|
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):
|
2021-11-18 11:06:21 +13:00
|
|
|
""" Viewset for all habitats.
|
2021-11-05 11:16:53 +13:00
|
|
|
"""
|
|
|
|
serializer_class = HabitatSerializer
|
|
|
|
queryset = Habitat.objects.all()
|
2021-11-05 14:22:07 +13:00
|
|
|
|
|
|
|
|
|
|
|
class HabitatImageViewSet(viewsets.ViewSet):
|
2021-11-18 11:06:21 +13:00
|
|
|
""" Viewset for a habitat image.
|
2021-11-05 14:22:07 +13:00
|
|
|
"""
|
|
|
|
|
|
|
|
def list(self, request):
|
|
|
|
queryset = HabitatImage.objects.all()
|
|
|
|
serializer = HabitatImageSerializer(queryset, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
def retrieve(self, request, pk=None):
|
|
|
|
queryset = HabitatImage.objects.all()
|
|
|
|
habitat_image = get_object_or_404(queryset, pk=pk)
|
|
|
|
serializer = HabitatImageSerializer(habitat_image)
|
|
|
|
return Response(serializer.data)
|
2021-11-08 17:00:04 +13:00
|
|
|
|
|
|
|
|
|
|
|
class CSVDownloadView(viewsets.ViewSet):
|
2021-11-18 11:06:21 +13:00
|
|
|
""" Viewset for a downloading a CSV plant list and filters.
|
|
|
|
"""
|
2021-11-08 17:00:04 +13:00
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
2021-11-10 14:15:25 +13:00
|
|
|
filtered_plants = get_filtered_plants(request)
|
2021-11-08 17:00:04 +13:00
|
|
|
create_plant_csv_file(request, filtered_plants)
|
|
|
|
|
2021-11-18 11:06:21 +13:00
|
|
|
csv_file = open(get_plant_resource_filepath(), 'rb')
|
2021-11-08 17:00:04 +13:00
|
|
|
response = HttpResponse(csv_file, content_type='text/csv')
|
2021-11-10 10:33:34 +13:00
|
|
|
response['Content-Disposition'] = 'attachment; filename="plants.csv"'
|
2021-11-08 17:00:04 +13:00
|
|
|
return response
|
2021-11-18 11:06:21 +13:00
|
|
|
|
|
|
|
class PDFDownloadView(viewsets.ViewSet):
|
|
|
|
""" Viewset for a downloading a PDF planting guide with appended filter and plant list info.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
filtered_plants = get_filtered_plants(request)
|
|
|
|
|
|
|
|
create_planting_guide_pdf(request, filtered_plants)
|
|
|
|
pdf_file = open(get_plant_resource_filepath(PLANTING_GUIDE_PDF_FILENAME), 'rb')
|
|
|
|
|
|
|
|
response = HttpResponse(FileWrapper(pdf_file), content_type='application/pdf')
|
|
|
|
return response
|