From 94b8e5bb9fc9e57a7625051034a93c32468b5c3f Mon Sep 17 00:00:00 2001 From: J-E Castagnede Date: Tue, 2 Jul 2024 14:59:26 +0200 Subject: [PATCH 1/2] Allow send empty string on not required in custom contribution API --- docs/changelog.rst | 3 +++ georiviere/portal/serializers/contribution.py | 3 ++- .../portal/tests/test_views/test_contribution.py | 13 +++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 48482e5e..19a413bc 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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) diff --git a/georiviere/portal/serializers/contribution.py b/georiviere/portal/serializers/contribution.py index 8cd62a6d..a97806d4 100644 --- a/georiviere/portal/serializers/contribution.py +++ b/georiviere/portal/serializers/contribution.py @@ -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 diff --git a/georiviere/portal/tests/test_views/test_contribution.py b/georiviere/portal/tests/test_views/test_contribution.py index 1682505f..bf61a220 100644 --- a/georiviere/portal/tests/test_views/test_contribution.py +++ b/georiviere/portal/tests/test_views/test_contribution.py @@ -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", @@ -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" From 83a00c959266af4fe95025c4c06974714ffd57f4 Mon Sep 17 00:00:00 2001 From: J-E Castagnede Date: Tue, 2 Jul 2024 15:19:08 +0200 Subject: [PATCH 2/2] Allow send empty string on not required in custom contribution API --- georiviere/portal/serializers/contribution.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/georiviere/portal/serializers/contribution.py b/georiviere/portal/serializers/contribution.py index a97806d4..1e152c4b 100644 --- a/georiviere/portal/serializers/contribution.py +++ b/georiviere/portal/serializers/contribution.py @@ -239,8 +239,11 @@ def __init__(self, *args, **kwargs): 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=required, allow_blank=not required + **kwargs ) # station is required if defined at custom_type level. Geom is not required because replace by station geom