Skip to content

Commit

Permalink
Merge pull request #265 from Georiviere/fix_not_required_empty_strings
Browse files Browse the repository at this point in the history
Allow send empty string on not required in custom contribution API
  • Loading branch information
submarcos authored Jul 2, 2024
2 parents f55ecad + 83a00c9 commit 8a051d0
Show file tree
Hide file tree
Showing 3 changed files with 17 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
6 changes: 5 additions & 1 deletion georiviere/portal/serializers/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,13 @@ 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
kwargs = {"label": field.get("title"), "required": required}
if output_field == serializers.CharField:
kwargs["allow_blank"] = not required
self.fields[key] = output_field(
label=field.get("title"), required=key in schema.get("required", [])
**kwargs
)

# 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 8a051d0

Please sign in to comment.