Skip to content

Commit

Permalink
Allow send empty string on not required in custom contribution API
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed Jul 2, 2024
1 parent f55ecad commit 94b8e5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ CHANGELOG
1.4.1+dev (XXXX-XX-XX)
----------------------

**Bug fix**

- Allow send empty string on not required in custom contribution API


1.4.1 (2024-07-02)
Expand Down
3 changes: 2 additions & 1 deletion georiviere/portal/serializers/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ def __init__(self, *args, **kwargs):
output_field = serializers.DateField
elif field.get("type") == "string" and field.get("format") == "date-time":
output_field = serializers.DateTimeField
required = key in schema.get("required", [])
# make field required or not
self.fields[key] = output_field(
label=field.get("title"), required=key in schema.get("required", [])
label=field.get("title"), required=required, allow_blank=not required
)

# station is required if defined at custom_type level. Geom is not required because replace by station geom
Expand Down
13 changes: 9 additions & 4 deletions georiviere/portal/tests/test_views/test_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,14 @@ def setUpTestData(cls):
cls.station = StationFactory()
cls.custom_contribution_type = CustomContributionTypeFactory()
cls.custom_contribution_type.stations.add(cls.station)
CustomContributionTypeStringFieldFactory(
cls.string_field = CustomContributionTypeStringFieldFactory(
custom_type=cls.custom_contribution_type,
label="Field string",
)
cls.string_field_blank = CustomContributionTypeStringFieldFactory(
custom_type=cls.custom_contribution_type,
label="Field string empty",
)
CustomContributionTypeBooleanFieldFactory(
custom_type=cls.custom_contribution_type,
label="Field boolean",
Expand Down Expand Up @@ -376,11 +380,12 @@ def test_create(self):
data = response.json()
self.assertEqual(response.status_code, 201, data)

def test_null_values_on_not_required(self):
"""Null values should be accepted on non required fields"""
def test_no_values_on_not_required(self):
"""Null and empty values should be accepted on non required fields"""
data = {
"station": self.station.pk,
"field_string": "string",
self.string_field.key: None,
self.string_field_blank.key: "",
"field_boolean": None,
"field_float": 1.1,
"contributed_at": "2020-01-01T00:00"
Expand Down

0 comments on commit 94b8e5b

Please sign in to comment.