Skip to content

Commit

Permalink
tries to fix the serializer (#2034)
Browse files Browse the repository at this point in the history
  • Loading branch information
george-silva authored Dec 23, 2024
1 parent dd1c086 commit 4c1ddb1
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/planscape/planning/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from typing import List, Optional
from numpy import require
from rest_framework import serializers
from rest_framework_gis import serializers as gis_serializers
from django.conf import settings
Expand Down Expand Up @@ -238,8 +239,16 @@ class Meta:

class ConfigurationSerializer(serializers.Serializer):
question_id = serializers.IntegerField(allow_null=True, required=False)
weights = serializers.ListField(child=serializers.IntegerField(), allow_empty=True)
est_cost = serializers.FloatField(min_value=1)
weights = serializers.ListField(
child=serializers.IntegerField(),
allow_empty=True,
required=False,
)
est_cost = serializers.FloatField(
min_value=1,
default=2470,
required=False,
)
max_budget = serializers.FloatField(
allow_null=True,
required=False,
Expand All @@ -256,38 +265,47 @@ class ConfigurationSerializer(serializers.Serializer):
allow_null=True,
required=False,
)
stand_size = serializers.ChoiceField(choices=StandSizeChoices.choices)
stand_size = serializers.ChoiceField(
choices=StandSizeChoices.choices,
default=StandSizeChoices.LARGE,
required=False,
)
excluded_areas = serializers.ListField(
child=serializers.CharField(max_length=256),
allow_empty=True,
min_length=0,
required=False,
)
stand_thresholds = serializers.ListField(
child=serializers.CharField(max_length=512),
allow_empty=True,
min_length=0,
required=False,
)
global_thresholds = serializers.ListField(
child=serializers.CharField(max_length=512),
allow_empty=True,
min_length=0,
required=False,
)
scenario_priorities = serializers.ListField(
child=serializers.CharField(max_length=256),
min_length=1,
required=False,
)
scenario_output_fields = serializers.ListField(
child=serializers.CharField(max_length=256),
min_length=1,
required=False,
)
max_treatment_area_ratio = serializers.FloatField(
min_value=100,
required=False,
)

def validate(self, data):
budget = data.get("max_budget")
max_area = data.get("max_treatment_area_ratio")
def validate(self, attrs):
budget = attrs.get("max_budget")
max_area = attrs.get("max_treatment_area_ratio")

if budget and max_area:
raise serializers.ValidationError(
Expand All @@ -298,7 +316,7 @@ def validate(self, data):
raise serializers.ValidationError(
"You should provide one of `max_budget` or `max_treatment_area_ratio`."
)
return data
return attrs


class ListScenarioSerializer(serializers.ModelSerializer):
Expand Down

0 comments on commit 4c1ddb1

Please sign in to comment.