Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow send empty string on not required in custom contribution API #265

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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