From f885a389029a10e225dae9d6c86d854fc5acbbf4 Mon Sep 17 00:00:00 2001 From: Jonas Cosandey Date: Mon, 20 Jan 2025 15:03:39 +0100 Subject: [PATCH] feat(float): add step configuration --- caluma/caluma_form/models.py | 8 ++++++++ caluma/caluma_form/schema.py | 1 + caluma/caluma_form/serializers.py | 9 +++++++++ caluma/caluma_form/tests/test_question.py | 1 + 4 files changed, 19 insertions(+) diff --git a/caluma/caluma_form/models.py b/caluma/caluma_form/models.py index f1bd59b8e..3ae22c140 100644 --- a/caluma/caluma_form/models.py +++ b/caluma/caluma_form/models.py @@ -236,6 +236,14 @@ def color(self): def color(self, value): self.configuration["color"] = value + @property + def step(self): + return self.configuration.get("step") or 0.001 + + @step.setter + def step(self, value): + self.configuration["step"] = value + @property def validate_on_enter(self): return self.configuration.get("validate_on_enter") diff --git a/caluma/caluma_form/schema.py b/caluma/caluma_form/schema.py index dde23b1f0..97a9dcd88 100644 --- a/caluma/caluma_form/schema.py +++ b/caluma/caluma_form/schema.py @@ -457,6 +457,7 @@ class Meta: class FloatQuestion(QuestionQuerysetMixin, FormDjangoObjectType): min_value = graphene.Float() max_value = graphene.Float() + step = graphene.Float() placeholder = graphene.String() hint_text = graphene.String() default_answer = graphene.Field("caluma.caluma_form.schema.FloatAnswer") diff --git a/caluma/caluma_form/serializers.py b/caluma/caluma_form/serializers.py index 5a32b8e8a..86bc10f41 100644 --- a/caluma/caluma_form/serializers.py +++ b/caluma/caluma_form/serializers.py @@ -335,6 +335,7 @@ class Meta(SaveQuestionSerializer.Meta): class SaveFloatQuestionSerializer(SaveQuestionSerializer): min_value = FloatField(required=False, allow_null=True) max_value = FloatField(required=False, allow_null=True) + step = FloatField(required=False, allow_null=True) def validate(self, data): min_value = ( @@ -351,6 +352,13 @@ def validate(self, data): f"max_value {max_value} is smaller than {min_value}" ) + step = data.get("step") + step = ( + step + if step is not None + else 0.001 + ) + data["type"] = models.Question.TYPE_FLOAT return super().validate(data) @@ -358,6 +366,7 @@ class Meta(SaveQuestionSerializer.Meta): fields = SaveQuestionSerializer.Meta.fields + [ "min_value", "max_value", + "step", "placeholder", "hint_text", ] diff --git a/caluma/caluma_form/tests/test_question.py b/caluma/caluma_form/tests/test_question.py index 921a56f89..f20c7f3b1 100644 --- a/caluma/caluma_form/tests/test_question.py +++ b/caluma/caluma_form/tests/test_question.py @@ -93,6 +93,7 @@ def test_query_all_questions( ... on FloatQuestion { floatMinValue: minValue floatMaxValue: maxValue + floatStep: step placeholder hintText }