right-tree/backend/right_tree/api/views.py
Matthew Northcott 3f9f816a7e [#40] Bulk PDF export
- backend changes
2023-02-22 15:08:30 +13:00

161 lines
5.7 KiB
Python

from django.http import HttpResponseBadRequest, FileResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.text import slugify
from rest_framework import viewsets, permissions
from rest_framework.response import Response
from .models import Habitat, HabitatImage, Plant, EcologicalDistrictLayer, SoilOrder, Zone, Questionnaire
from .serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer, ZoneSerializer, QuestionnaireSerializer
from .filters import *
from .wms_utils import get_address_from_coordinates, search_address
from .resource_generation_utils import generate_csv, get_filter_values, serialize_plants_queryset, create_planting_guide_pdf, PLANTING_GUIDE_PDF_FILENAME, CSV_FILENAME, storage
class PlantViewSet(viewsets.ModelViewSet):
""" Filtered viewset for plants.
"""
queryset = Plant.objects.all()
serializer_class = PlantSerializer
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...)
"""
return get_filtered_plants(self.request)
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')
address = self.request.query_params.get('search')
if address is not None:
results = search_address(address)
return Response(results)
elif coordinates is not None:
address_data = get_address_from_coordinates(coordinates)
serializer = AddressSerializer(address_data)
return Response(serializer.data)
else:
return HttpResponseBadRequest("No parameters given.")
class AuckCHCHRegionInformation(viewsets.ViewSet):
""" Filtered viewset defining if coordinate falls inside auckland and chch regions.
"""
def list(self, request):
coordinates = self.request.query_params.get('coordinates')
if coordinates is not None:
in_chch = is_in_christchurch(coordinates)
in_auckland = is_in_auckland(coordinates)
region_details = {"in_chch": in_chch, "in_auckland": in_auckland}
return Response(region_details)
else:
return HttpResponseBadRequest("No coordinate given.")
class HabitatViewSet(viewsets.ModelViewSet):
""" Viewset for all habitats.
"""
serializer_class = HabitatSerializer
queryset = Habitat.objects.all()
class ZoneViewSet(viewsets.ModelViewSet):
""" Viewset for all habitats.
"""
serializer_class = ZoneSerializer
queryset = Zone.objects.all()
class HabitatImageViewSet(viewsets.ViewSet):
""" Viewset for a habitat image.
"""
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)
class CSVDownloadView(viewsets.ViewSet):
""" Viewset for a downloading a CSV plant list and filters.
"""
def list(self, request, *args, **kwargs):
filtered_plants = get_filtered_plants(request)
plant_data = serialize_plants_queryset(filtered_plants)
filename = f"plants_{slugify(timezone.now())}.csv"
generate_csv(plant_data, filename)
return FileResponse(
storage.open(filename, 'rb'),
filename='plants.csv',
content_type='text/csv',
)
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):
filter_data = get_filter_values(request.query_params)
filtered_plants = get_filtered_plants(request)
plant_data = serialize_plants_queryset(filtered_plants)
filename = f"planting_guide_{slugify(timezone.now())}.pdf"
create_planting_guide_pdf(filter_data, plant_data, filename)
return FileResponse(
storage.open(filename, 'rb'),
filename=PLANTING_GUIDE_PDF_FILENAME,
content_type='application/pdf',
)
class QuestionnaireViewSet(viewsets.ModelViewSet):
serializer_class = QuestionnaireSerializer
queryset = Questionnaire.objects.all()
http_method_names = ("post",)
permission_classes = [permissions.AllowAny]