Add zone endpoint and modify zone/image/habitat serializers

This commit is contained in:
Dana Lambert 2021-12-06 10:24:51 +13:00
parent c2328646f5
commit 0075e8b303
4 changed files with 23 additions and 49 deletions

View file

@ -4,11 +4,7 @@ import right_tree.api.models as models
class ZoneAdmin(admin.ModelAdmin): class ZoneAdmin(admin.ModelAdmin):
ordering = ['name', 'variant', 'refined_variant', 'id'] ordering = ['name', 'variant', 'refined_variant', 'id']
search_fields = ['name', 'variant', 'refined_variant', 'id'] search_fields = ['name', 'habitat__name', 'variant', 'refined_variant', 'id']
class ZoneImageSegmentAdmin(admin.ModelAdmin):
autocomplete_fields = ['zone']
admin.site.register(models.Plant) admin.site.register(models.Plant)
@ -21,5 +17,4 @@ admin.site.register(models.ToleranceLevel)
admin.site.register(models.HabitatImage) admin.site.register(models.HabitatImage)
admin.site.register(models.Habitat) admin.site.register(models.Habitat)
admin.site.register(models.Zone, ZoneAdmin) admin.site.register(models.Zone, ZoneAdmin)
admin.site.register(models.ZoneImageSegment, ZoneImageSegmentAdmin)
admin.site.register(models.ChristchurchRegion) admin.site.register(models.ChristchurchRegion)

View file

@ -39,49 +39,11 @@ class SoilVariantSerializer(serializers.HyperlinkedModelSerializer):
model = SoilVariant model = SoilVariant
fields = ['name'] 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): class HabitatImageSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField() id = serializers.ReadOnlyField()
image_segments = ZoneImageSegmentSerializer(many=True)
class Meta: class Meta:
model = HabitatImage model = HabitatImage
fields = ['id', 'name', 'image_filename', 'image_segments'] fields = ['id', 'name', 'image_filename']
class HabitatSerializer(serializers.HyperlinkedModelSerializer): class HabitatSerializer(serializers.HyperlinkedModelSerializer):
@ -92,7 +54,15 @@ class HabitatSerializer(serializers.HyperlinkedModelSerializer):
model = Habitat model = Habitat
fields = ['id', 'name', 'images'] 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): class LocationDetailsSerializer(serializers.Serializer):
ecologic_1 = serializers.CharField(max_length=50) ecologic_1 = serializers.CharField(max_length=50)
ecologic_2 = serializers.CharField(max_length=50) ecologic_2 = serializers.CharField(max_length=50)

View file

@ -1,11 +1,11 @@
from django.http import HttpResponseBadRequest, HttpResponse, FileResponse 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 import viewsets
from rest_framework.response import Response from rest_framework.response import Response
from wsgiref.util import FileWrapper from wsgiref.util import FileWrapper
from right_tree.api.models import Habitat, HabitatImage, Plant, EcologicalDistrictLayer, SoilOrder from right_tree.api.models import Habitat, HabitatImage, Plant, EcologicalDistrictLayer, SoilOrder, Zone
from right_tree.api.serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer from right_tree.api.serializers import HabitatImageSerializer, HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer, ZoneSerializer
from .filters import * from .filters import *
from .wms_utils import get_address_from_coordinates from .wms_utils import get_address_from_coordinates
@ -89,6 +89,13 @@ class HabitatViewSet(viewsets.ModelViewSet):
queryset = Habitat.objects.all() queryset = Habitat.objects.all()
class ZoneViewSet(viewsets.ModelViewSet):
""" Viewset for all habitats.
"""
serializer_class = ZoneSerializer
queryset = Zone.objects.all()
class HabitatImageViewSet(viewsets.ViewSet): class HabitatImageViewSet(viewsets.ViewSet):
""" Viewset for a habitat image. """ Viewset for a habitat image.
""" """
@ -118,11 +125,12 @@ class CSVDownloadView(viewsets.ViewSet):
response['Content-Disposition'] = 'attachment; filename="plants.csv"' response['Content-Disposition'] = 'attachment; filename="plants.csv"'
return response return response
class PDFDownloadView(viewsets.ViewSet): class PDFDownloadView(viewsets.ViewSet):
""" Viewset for a downloading a PDF planting guide with appended filter and plant list info. """ 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) filtered_plants = get_filtered_plants(request)
create_planting_guide_pdf(request, filtered_plants) create_planting_guide_pdf(request, filtered_plants)

View file

@ -26,6 +26,7 @@ router.register(r'ecologicaldistrict', views.EcologicalDistrictViewSet, basename
router.register(r'address', views.LINZPropertyViewSet, basename='address') router.register(r'address', views.LINZPropertyViewSet, basename='address')
router.register(r'region', views.AuckCHCHRegionInformation, basename='region') router.register(r'region', views.AuckCHCHRegionInformation, basename='region')
router.register(r'habitats', views.HabitatViewSet, basename='habitats') 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'habitatimage', views.HabitatImageViewSet, basename='habitatimage')
router.register(r'download/csv', views.CSVDownloadView, basename='downloadcsv') router.register(r'download/csv', views.CSVDownloadView, basename='downloadcsv')