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 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): class ToleranceLevelSerializer(serializers.HyperlinkedModelSerializer):
@ -15,7 +32,8 @@ class EcologicalRegionSerializer(serializers.HyperlinkedModelSerializer):
class EcologicalDistrictLayerSerializer(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( ecological_region = serializers.CharField(
max_length=50, source='ecologic_2') max_length=50, source='ecologic_2')
@ -39,6 +57,16 @@ class SoilVariantSerializer(serializers.HyperlinkedModelSerializer):
fields = ['name'] 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): 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)
@ -55,6 +83,7 @@ class PlantSerializer(serializers.HyperlinkedModelSerializer):
ecological_regions = EcologicalRegionSerializer(many=True, read_only=True) ecological_regions = EcologicalRegionSerializer(many=True, read_only=True)
soil_order = SoilOrderSerializer(many=True, read_only=True) soil_order = SoilOrderSerializer(many=True, read_only=True)
soil_variants = SoilVariantSerializer(many=True, read_only=True) soil_variants = SoilVariantSerializer(many=True, read_only=True)
zones = ZoneSerializer(many=True, read_only=True)
class Meta: class Meta:
model = Plant model = Plant

View file

@ -7,12 +7,13 @@ from django.http import HttpResponseBadRequest
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.response import Response from rest_framework.response import Response
from right_tree.api.models import Plant, EcologicalDistrictLayer, SoilOrder from right_tree.api.models import Habitat, Plant, EcologicalDistrictLayer, SoilOrder
from right_tree.api.serializers import PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer from right_tree.api.serializers import HabitatSerializer, PlantSerializer, SoilOrderSerializer, EcologicalDistrictLayerSerializer, AddressSerializer
from .filters import * from .filters import *
from .utils import get_address_from_coordinates from .utils import get_address_from_coordinates
class PlantViewSet(viewsets.ModelViewSet): class PlantViewSet(viewsets.ModelViewSet):
""" """
Filtered viewset for plants. Filtered viewset for plants.
@ -30,6 +31,7 @@ class PlantViewSet(viewsets.ModelViewSet):
return queryset return queryset
class SoilOrderViewSet(viewsets.ModelViewSet): class SoilOrderViewSet(viewsets.ModelViewSet):
""" """
Filtered viewset for soil details. Filtered viewset for soil details.
@ -72,6 +74,14 @@ class LINZPropertyViewSet(viewsets.ViewSet):
if coordinates is not None: if coordinates is not None:
address_data = get_address_from_coordinates(coordinates) address_data = get_address_from_coordinates(coordinates)
serializer = AddressSerializer(address_data) serializer = AddressSerializer(address_data)
return Response(serializer.data) return Response(serializer.data)
else: else:
return HttpResponseBadRequest("No coordinate given.") 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'soil', views.SoilOrderViewSet, basename='soil')
router.register(r'ecologicaldistrict', views.EcologicalDistrictViewSet, basename='ecologicaldistrict') router.register(r'ecologicaldistrict', views.EcologicalDistrictViewSet, basename='ecologicaldistrict')
router.register(r'address', views.LINZPropertyViewSet, basename='address') router.register(r'address', views.LINZPropertyViewSet, basename='address')
router.register(r'habitats', views.HabitatViewSet, basename='habitats')
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),