From 0075e8b3030f39f5962f2332bb235cb40394dfb6 Mon Sep 17 00:00:00 2001 From: Dana Lambert Date: Mon, 6 Dec 2021 10:24:51 +1300 Subject: [PATCH] Add zone endpoint and modify zone/image/habitat serializers --- backend/right_tree/api/admin.py | 7 +--- backend/right_tree/api/serializers.py | 48 +++++---------------------- backend/right_tree/api/views.py | 16 ++++++--- backend/right_tree/urls.py | 1 + 4 files changed, 23 insertions(+), 49 deletions(-) diff --git a/backend/right_tree/api/admin.py b/backend/right_tree/api/admin.py index 0af29dd..f72b920 100644 --- a/backend/right_tree/api/admin.py +++ b/backend/right_tree/api/admin.py @@ -4,11 +4,7 @@ import right_tree.api.models as models class ZoneAdmin(admin.ModelAdmin): ordering = ['name', 'variant', 'refined_variant', 'id'] - search_fields = ['name', 'variant', 'refined_variant', 'id'] - - -class ZoneImageSegmentAdmin(admin.ModelAdmin): - autocomplete_fields = ['zone'] + search_fields = ['name', 'habitat__name', 'variant', 'refined_variant', 'id'] admin.site.register(models.Plant) @@ -21,5 +17,4 @@ admin.site.register(models.ToleranceLevel) admin.site.register(models.HabitatImage) admin.site.register(models.Habitat) admin.site.register(models.Zone, ZoneAdmin) -admin.site.register(models.ZoneImageSegment, ZoneImageSegmentAdmin) admin.site.register(models.ChristchurchRegion) diff --git a/backend/right_tree/api/serializers.py b/backend/right_tree/api/serializers.py index b0ca8f4..6ff0ec4 100644 --- a/backend/right_tree/api/serializers.py +++ b/backend/right_tree/api/serializers.py @@ -39,49 +39,11 @@ class SoilVariantSerializer(serializers.HyperlinkedModelSerializer): model = SoilVariant fields = ['name'] -class SimpleHabitatImageSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - - class Meta: - model = HabitatImage - fields = ['id'] - -class SimpleHabitatSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - images = SimpleHabitatImageSerializer(many=True) - - class Meta: - model = Habitat - fields = ['id', 'name', 'images'] - - -class ZoneSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - redirect_habitat = SimpleHabitatSerializer() - - class Meta: - model = Zone - fields = ['id', 'name', 'variant', - 'refined_variant', 'redirect_habitat', 'ignore_soil_order_filter', 'ignore_location_filter'] - - -class ZoneImageSegmentSerializer(serializers.HyperlinkedModelSerializer): - id = serializers.ReadOnlyField() - zone = ZoneSerializer() - - class Meta: - model = ZoneImageSegment - fields = ['id', 'zone', - 'segment_order', 'segment_percentage_width'] - - class HabitatImageSerializer(serializers.HyperlinkedModelSerializer): id = serializers.ReadOnlyField() - image_segments = ZoneImageSegmentSerializer(many=True) - class Meta: model = HabitatImage - fields = ['id', 'name', 'image_filename', 'image_segments'] + fields = ['id', 'name', 'image_filename'] class HabitatSerializer(serializers.HyperlinkedModelSerializer): @@ -92,7 +54,15 @@ class HabitatSerializer(serializers.HyperlinkedModelSerializer): model = Habitat fields = ['id', 'name', 'images'] +class ZoneSerializer(serializers.HyperlinkedModelSerializer): + id = serializers.ReadOnlyField() + habitat = HabitatSerializer() + redirect_habitat = HabitatSerializer() + class Meta: + model = Zone + fields = ['id', 'name', 'variant', + 'refined_variant', 'habitat', 'related_svg_segment', 'redirect_habitat', 'ignore_soil_order_filter', 'ignore_location_filter'] class LocationDetailsSerializer(serializers.Serializer): ecologic_1 = serializers.CharField(max_length=50) ecologic_2 = serializers.CharField(max_length=50) diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py index e48b11e..118b8b8 100644 --- a/backend/right_tree/api/views.py +++ b/backend/right_tree/api/views.py @@ -1,11 +1,11 @@ from django.http import HttpResponseBadRequest, HttpResponse, FileResponse -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404 from rest_framework import viewsets from rest_framework.response import Response from wsgiref.util import FileWrapper -from right_tree.api.models import Habitat, HabitatImage, Plant, EcologicalDistrictLayer, SoilOrder -from right_tree.api.serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer +from right_tree.api.models import Habitat, HabitatImage, Plant, EcologicalDistrictLayer, SoilOrder, Zone +from right_tree.api.serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer, ZoneSerializer from .filters import * from .wms_utils import get_address_from_coordinates @@ -89,6 +89,13 @@ class HabitatViewSet(viewsets.ModelViewSet): 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. """ @@ -118,11 +125,12 @@ class CSVDownloadView(viewsets.ViewSet): response['Content-Disposition'] = 'attachment; filename="plants.csv"' return response + 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): + def list(self, request, *args, **kwargs): filtered_plants = get_filtered_plants(request) create_planting_guide_pdf(request, filtered_plants) diff --git a/backend/right_tree/urls.py b/backend/right_tree/urls.py index cd13a1e..d29fe59 100644 --- a/backend/right_tree/urls.py +++ b/backend/right_tree/urls.py @@ -26,6 +26,7 @@ router.register(r'ecologicaldistrict', views.EcologicalDistrictViewSet, basename router.register(r'address', views.LINZPropertyViewSet, basename='address') router.register(r'region', views.AuckCHCHRegionInformation, basename='region') router.register(r'habitats', views.HabitatViewSet, basename='habitats') +router.register(r'zones', views.ZoneViewSet, basename='zones') router.register(r'habitatimage', views.HabitatImageViewSet, basename='habitatimage') router.register(r'download/csv', views.CSVDownloadView, basename='downloadcsv')