Add /habitats endpoint and fix plants zone serializer

This commit is contained in:
Dana Lambert 2021-11-05 11:16:53 +13:00
parent b5b39a5c18
commit 06e6f4322c
3 changed files with 46 additions and 6 deletions

View file

@ -1,5 +1,22 @@
from rest_framework import serializers
from right_tree.api.models import Plant, ToleranceLevel, SoilOrder, SoilVariant, EcologicalRegion, EcologicalDistrictLayer
from right_tree.api.models import *
class HabitatImageSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
class Meta:
model = HabitatImage
fields = ['id', 'name', 'image_filename']
class HabitatSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
images = HabitatImageSerializer(many=True)
class Meta:
model = Habitat
fields = ['id', 'name', 'images']
class ToleranceLevelSerializer(serializers.HyperlinkedModelSerializer):
@ -15,7 +32,8 @@ class EcologicalRegionSerializer(serializers.HyperlinkedModelSerializer):
class EcologicalDistrictLayerSerializer(serializers.HyperlinkedModelSerializer):
ecological_district = serializers.CharField(max_length=50, source='ecologic_1')
ecological_district = serializers.CharField(
max_length=50, source='ecologic_1')
ecological_region = serializers.CharField(
max_length=50, source='ecologic_2')
@ -39,6 +57,16 @@ class SoilVariantSerializer(serializers.HyperlinkedModelSerializer):
fields = ['name']
class ZoneSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
redirect_habitat = HabitatSerializer()
class Meta:
model = Zone
fields = ['id', 'name', 'variant',
'refined_variant', 'redirect_habitat']
class LocationDetailsSerializer(serializers.Serializer):
ecologic_1 = serializers.CharField(max_length=50)
ecologic_2 = serializers.CharField(max_length=50)
@ -55,6 +83,7 @@ class PlantSerializer(serializers.HyperlinkedModelSerializer):
ecological_regions = EcologicalRegionSerializer(many=True, read_only=True)
soil_order = SoilOrderSerializer(many=True, read_only=True)
soil_variants = SoilVariantSerializer(many=True, read_only=True)
zones = ZoneSerializer(many=True, read_only=True)
class Meta:
model = Plant

View file

@ -7,12 +7,13 @@ from django.http import HttpResponseBadRequest
from rest_framework import viewsets
from rest_framework.response import Response
from right_tree.api.models import Plant, EcologicalDistrictLayer, SoilOrder
from right_tree.api.serializers import PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer
from right_tree.api.models import Habitat, Plant, EcologicalDistrictLayer, SoilOrder
from right_tree.api.serializers import HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer
from .filters import *
from .utils import get_address_from_coordinates
class PlantViewSet(viewsets.ModelViewSet):
"""
Filtered viewset for plants.
@ -30,6 +31,7 @@ class PlantViewSet(viewsets.ModelViewSet):
return queryset
class SoilOrderViewSet(viewsets.ModelViewSet):
"""
Filtered viewset for soil details.
@ -75,3 +77,11 @@ class LINZPropertyViewSet(viewsets.ViewSet):
return Response(serializer.data)
else:
return HttpResponseBadRequest("No coordinate given.")
class HabitatViewSet(viewsets.ModelViewSet):
"""
Viewset for all habitats.
"""
serializer_class = HabitatSerializer
queryset = Habitat.objects.all()

View file

@ -24,6 +24,7 @@ router.register(r'plants', views.PlantViewSet)
router.register(r'soil', views.SoilOrderViewSet, basename='soil')
router.register(r'ecologicaldistrict', views.EcologicalDistrictViewSet, basename='ecologicaldistrict')
router.register(r'address', views.LINZPropertyViewSet, basename='address')
router.register(r'habitats', views.HabitatViewSet, basename='habitats')
urlpatterns = [
path('admin/', admin.site.urls),