Skip to content

Commit

Permalink
Merge pull request #2362 from velrest/float_step
Browse files Browse the repository at this point in the history
feat(float): add step configuration
  • Loading branch information
luytena authored Jan 28, 2025
2 parents 361df5f + a8c265a commit f59a64c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions caluma/caluma_form/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ def color(self):
def color(self, value):
self.configuration["color"] = value

@property
def step(self):
return self.configuration.get("step")

@step.setter
def step(self, value):
self.configuration["step"] = value

@property
def validate_on_enter(self):
return self.configuration.get("validate_on_enter")
Expand Down
1 change: 1 addition & 0 deletions caluma/caluma_form/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions caluma/caluma_form/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -351,13 +352,18 @@ def validate(self, data):
f"max_value {max_value} is smaller than {min_value}"
)

step = data.get("step")
if step and step < 0:
raise exceptions.ValidationError(f"step {step} is smaller than 0")

data["type"] = models.Question.TYPE_FLOAT
return super().validate(data)

class Meta(SaveQuestionSerializer.Meta):
fields = SaveQuestionSerializer.Meta.fields + [
"min_value",
"max_value",
"step",
"placeholder",
"hint_text",
]
Expand Down
23 changes: 23 additions & 0 deletions caluma/caluma_form/tests/__snapshots__/test_question.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
'__typename': 'FloatQuestion',
'floatMaxValue': 1.0,
'floatMinValue': 0.0,
'floatStep': 0.2,
'hintText': '',
'id': 'RmxvYXRRdWVzdGlvbjplbnZpcm9ubWVudGFsLXRlbg==',
'infoText': '',
Expand All @@ -269,6 +270,7 @@
'__typename': 'FloatQuestion',
'floatMaxValue': None,
'floatMinValue': None,
'floatStep': None,
'hintText': '',
'id': 'RmxvYXRRdWVzdGlvbjplbnZpcm9ubWVudGFsLXRlbg==',
'infoText': '',
Expand Down Expand Up @@ -881,6 +883,27 @@
}),
})
# ---
# name: test_save_float_question[float-question__configuration2-0.3-True]
dict({
'saveFloatQuestion': dict({
'clientMutationId': 'testid',
'question': dict({
'__typename': 'FloatQuestion',
'defaultAnswer': dict({
'value': 0.3,
}),
'hintText': 'test',
'id': 'RmxvYXRRdWVzdGlvbjplbnZpcm9ubWVudGFsLXRlbg==',
'label': 'Bonnie Moreno',
'maxValue': None,
'meta': dict({
}),
'minValue': None,
'slug': 'environmental-ten',
}),
}),
})
# ---
# name: test_save_form_question[form]
dict({
'saveFormQuestion': dict({
Expand Down
10 changes: 9 additions & 1 deletion caluma/caluma_form/tests/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
"question__type,question__configuration,question__data_source,question__format_validators",
[
(models.Question.TYPE_INTEGER, {"max_value": 10, "min_value": 0}, None, []),
(models.Question.TYPE_FLOAT, {"max_value": 1.0, "min_value": 0.0}, None, []),
(
models.Question.TYPE_FLOAT,
{"max_value": 1.0, "min_value": 0.0, "step": 0.2},
None,
[],
),
(models.Question.TYPE_FLOAT, {}, None, []),
(models.Question.TYPE_DATE, {}, None, []),
(models.Question.TYPE_TEXT, {"min_length": 10}, None, ["email"]),
Expand Down Expand Up @@ -93,6 +98,7 @@ def test_query_all_questions(
... on FloatQuestion {
floatMinValue: minValue
floatMaxValue: maxValue
floatStep: step
placeholder
hintText
}
Expand Down Expand Up @@ -396,6 +402,8 @@ def test_save_textarea_question(db, question, answer, schema_executor):
[
(models.Question.TYPE_FLOAT, {"max_value": 10.0, "min_value": 0.0}, 0.3, True),
(models.Question.TYPE_FLOAT, {"max_value": 1.0, "min_value": 10.0}, 0.3, False),
(models.Question.TYPE_FLOAT, {"step": 1.0}, 0.3, True),
(models.Question.TYPE_FLOAT, {"step": -0.01}, 0.3, False),
],
)
def test_save_float_question(db, snapshot, question, schema_executor, answer, success):
Expand Down
2 changes: 2 additions & 0 deletions caluma/tests/__snapshots__/test_schema.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@
id: ID!
minValue: Float
maxValue: Float
step: Float
}

type Flow implements Node {
Expand Down Expand Up @@ -2671,6 +2672,7 @@
isArchived: Boolean
minValue: Float
maxValue: Float
step: Float
placeholder: String
hintText: String
clientMutationId: String
Expand Down

0 comments on commit f59a64c

Please sign in to comment.