diff --git a/backend/right_tree/api/serializers.py b/backend/right_tree/api/serializers.py index eb75888..1d343cf 100644 --- a/backend/right_tree/api/serializers.py +++ b/backend/right_tree/api/serializers.py @@ -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 diff --git a/backend/right_tree/api/views.py b/backend/right_tree/api/views.py index f09458b..3d9335f 100644 --- a/backend/right_tree/api/views.py +++ b/backend/right_tree/api/views.py @@ -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. @@ -72,6 +74,14 @@ class LINZPropertyViewSet(viewsets.ViewSet): if coordinates is not None: address_data = get_address_from_coordinates(coordinates) serializer = AddressSerializer(address_data) - return Response(serializer.data) - else: + 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() diff --git a/backend/right_tree/urls.py b/backend/right_tree/urls.py index 496e92d..c8e5b4a 100644 --- a/backend/right_tree/urls.py +++ b/backend/right_tree/urls.py @@ -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),