2023-02-21 16:43:46 +13:00
|
|
|
from rest_framework import serializers, exceptions
|
2023-04-20 15:12:22 +12:00
|
|
|
|
|
|
|
from .models import (
|
|
|
|
ToleranceLevel,
|
|
|
|
EcologicalRegion,
|
|
|
|
EcologicalDistrictLayer,
|
|
|
|
SoilOrder,
|
|
|
|
SoilVariant,
|
|
|
|
HabitatImage,
|
|
|
|
Habitat,
|
|
|
|
Zone,
|
|
|
|
Plant,
|
|
|
|
Questionnaire,
|
|
|
|
ActivationKey,
|
|
|
|
)
|
2021-11-05 11:16:53 +13:00
|
|
|
|
|
|
|
|
2021-10-20 12:37:45 +13:00
|
|
|
class ToleranceLevelSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ToleranceLevel
|
|
|
|
fields = ['level']
|
|
|
|
|
|
|
|
|
|
|
|
class EcologicalRegionSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = EcologicalRegion
|
|
|
|
fields = ['name']
|
|
|
|
|
|
|
|
|
2021-10-22 16:01:38 +13:00
|
|
|
class EcologicalDistrictLayerSerializer(serializers.HyperlinkedModelSerializer):
|
2021-11-05 11:16:53 +13:00
|
|
|
ecological_district = serializers.CharField(
|
|
|
|
max_length=50, source='ecologic_1')
|
2021-10-22 16:01:38 +13:00
|
|
|
ecological_region = serializers.CharField(
|
|
|
|
max_length=50, source='ecologic_2')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = EcologicalDistrictLayer
|
|
|
|
fields = ['ecological_district', 'ecological_region']
|
|
|
|
|
|
|
|
|
2021-10-20 12:37:45 +13:00
|
|
|
class SoilOrderSerializer(serializers.HyperlinkedModelSerializer):
|
2021-10-22 16:01:38 +13:00
|
|
|
soil_name = serializers.CharField(max_length=50, source='name')
|
|
|
|
soil_code = serializers.CharField(max_length=1, source='code')
|
|
|
|
|
2021-10-20 12:37:45 +13:00
|
|
|
class Meta:
|
|
|
|
model = SoilOrder
|
2021-10-22 16:01:38 +13:00
|
|
|
fields = ['soil_name', 'soil_code']
|
2021-10-20 12:37:45 +13:00
|
|
|
|
|
|
|
|
|
|
|
class SoilVariantSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = SoilVariant
|
|
|
|
fields = ['name']
|
|
|
|
|
2023-04-20 15:12:22 +12:00
|
|
|
|
2021-12-06 10:24:51 +13:00
|
|
|
class HabitatImageSerializer(serializers.HyperlinkedModelSerializer):
|
2021-11-05 14:22:07 +13:00
|
|
|
id = serializers.ReadOnlyField()
|
2023-04-20 15:12:22 +12:00
|
|
|
|
2021-11-05 14:22:07 +13:00
|
|
|
class Meta:
|
|
|
|
model = HabitatImage
|
2021-12-06 10:24:51 +13:00
|
|
|
fields = ['id', 'name', 'image_filename']
|
2021-11-11 13:26:04 +13:00
|
|
|
|
2021-12-06 10:24:51 +13:00
|
|
|
|
|
|
|
class HabitatSerializer(serializers.HyperlinkedModelSerializer):
|
2021-11-11 13:26:04 +13:00
|
|
|
id = serializers.ReadOnlyField()
|
2021-12-06 10:24:51 +13:00
|
|
|
images = HabitatImageSerializer(many=True)
|
2021-11-11 13:26:04 +13:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Habitat
|
|
|
|
fields = ['id', 'name', 'images']
|
2021-11-05 14:22:07 +13:00
|
|
|
|
2023-04-20 15:12:22 +12:00
|
|
|
|
2021-11-05 11:16:53 +13:00
|
|
|
class ZoneSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
id = serializers.ReadOnlyField()
|
2021-12-06 10:24:51 +13:00
|
|
|
habitat = HabitatSerializer()
|
|
|
|
redirect_habitat = HabitatSerializer()
|
2021-11-05 11:16:53 +13:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Zone
|
|
|
|
fields = ['id', 'name', 'variant',
|
2021-12-13 11:56:14 +13:00
|
|
|
'refined_variant', 'habitat', 'related_svg_segment', 'redirect_habitat', 'ignore_soil_order_filter', 'ignore_location_filter', 'tooltip_display_text']
|
2023-04-20 15:12:22 +12:00
|
|
|
|
|
|
|
|
2021-10-22 16:01:38 +13:00
|
|
|
class LocationDetailsSerializer(serializers.Serializer):
|
|
|
|
ecologic_1 = serializers.CharField(max_length=50)
|
|
|
|
ecologic_2 = serializers.CharField(max_length=50)
|
|
|
|
code = serializers.CharField(max_length=1)
|
|
|
|
name = serializers.CharField(max_length=50)
|
|
|
|
|
|
|
|
|
2021-10-07 08:59:57 +13:00
|
|
|
class PlantSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
id = serializers.ReadOnlyField()
|
2021-10-20 12:37:45 +13:00
|
|
|
water_tolerance = ToleranceLevelSerializer()
|
|
|
|
drought_tolerance = ToleranceLevelSerializer()
|
|
|
|
frost_tolerance = ToleranceLevelSerializer()
|
|
|
|
salinity_tolerance = ToleranceLevelSerializer()
|
|
|
|
ecological_regions = EcologicalRegionSerializer(many=True, read_only=True)
|
|
|
|
soil_order = SoilOrderSerializer(many=True, read_only=True)
|
|
|
|
soil_variants = SoilVariantSerializer(many=True, read_only=True)
|
2021-11-05 11:16:53 +13:00
|
|
|
zones = ZoneSerializer(many=True, read_only=True)
|
2021-10-07 08:59:57 +13:00
|
|
|
|
2021-11-08 15:51:05 +13:00
|
|
|
display_name = serializers.CharField(max_length=300)
|
|
|
|
display_growth_form = serializers.CharField(max_length=300)
|
|
|
|
moisture_preferences = serializers.CharField(max_length=50)
|
|
|
|
plant_tolerances = serializers.CharField(max_length=50)
|
|
|
|
ecosystem_services = serializers.CharField(max_length=200)
|
|
|
|
carbon_sequestration = serializers.CharField(max_length=50)
|
|
|
|
|
2021-10-07 08:59:57 +13:00
|
|
|
class Meta:
|
|
|
|
model = Plant
|
2021-10-20 12:37:45 +13:00
|
|
|
fields = '__all__'
|
2021-10-22 16:01:38 +13:00
|
|
|
|
|
|
|
|
|
|
|
class AddressSerializer(serializers.Serializer):
|
|
|
|
full_address = serializers.CharField(max_length=500)
|
2023-02-21 16:43:46 +13:00
|
|
|
|
|
|
|
|
|
|
|
class QuestionnaireSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
soil_variant = serializers.CharField(max_length=10)
|
2023-03-06 18:10:25 +13:00
|
|
|
key = serializers.CharField(max_length=20, write_only=True)
|
2023-02-21 16:43:46 +13:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Questionnaire
|
2023-04-20 14:59:00 +12:00
|
|
|
exclude = ['id']
|
2023-02-21 16:43:46 +13:00
|
|
|
|
|
|
|
def validate_soil_variant(self, value):
|
|
|
|
try:
|
|
|
|
return SoilVariant.objects.get(name__startswith=value)
|
|
|
|
except SoilVariant.DoesNotExist as e:
|
|
|
|
raise exceptions.ValidationError(e)
|
2023-03-06 18:10:25 +13:00
|
|
|
|
|
|
|
def validate_key(self, value):
|
|
|
|
try:
|
|
|
|
if (ak := ActivationKey.objects.get(key=value)).remaining_activations > 0:
|
|
|
|
return ak
|
|
|
|
raise exceptions.ValidationError("no remaining activations")
|
|
|
|
except ActivationKey.DoesNotExist as e:
|
|
|
|
raise exceptions.ValidationError(e)
|