From bcf9dd127f7b34dc7802b8dabe3c724e8673f4fc Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Tue, 17 Sep 2024 10:14:55 +0200 Subject: [PATCH] fix validation --- .../restapi/deserializers/dxcontent.py | 28 ++++++++++--------- .../tests/test_custom_validations.py | 6 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/iosanita/contenttypes/restapi/deserializers/dxcontent.py b/src/iosanita/contenttypes/restapi/deserializers/dxcontent.py index 3a085e4..16d0a1a 100644 --- a/src/iosanita/contenttypes/restapi/deserializers/dxcontent.py +++ b/src/iosanita/contenttypes/restapi/deserializers/dxcontent.py @@ -34,10 +34,10 @@ def __call__( def validate_data_iosanita(self, data, create): if IoSanitaMigrationMarker.providedBy(self.request): return - if not create: - portal_type = self.context.portal_type - else: + if create: portal_type = data.get("@type", "") + else: + portal_type = self.context.portal_type self.validate_a_chi_si_rivolge( portal_type=portal_type, data=data, create=create @@ -60,16 +60,17 @@ def validate_a_chi_si_rivolge(self, portal_type, data, create): if not has_behaviors: # skip check return - if not create: - if ( - "a_chi_si_rivolge" not in data - and "a_chi_si_rivolge_tassonomia" not in data # noqa - ): - return a_chi_si_rivolge = data.get("a_chi_si_rivolge", {}) a_chi_si_rivolge_tassonomia = data.get("a_chi_si_rivolge_tassonomia", []) + if not create: + if not a_chi_si_rivolge: + a_chi_si_rivolge = getattr(self.context, "a_chi_si_rivolge", {}) + if not a_chi_si_rivolge_tassonomia: + a_chi_si_rivolge_tassonomia = getattr( + self.context, "a_chi_si_rivolge_tassonomia", [] + ) if self.is_empty_blocks(a_chi_si_rivolge) and not a_chi_si_rivolge_tassonomia: msg = api.portal.translate( _( @@ -149,7 +150,8 @@ def is_empty_blocks(self, blocks_field): if not blocks: return True blocks_data = list(blocks.values()) - if len(blocks_data) == 1: - if blocks_data[0] == {"@type": "slate"}: - return True - return False + for block in blocks_data: + if block.get("plaintext", ""): + # there is some text + return False + return True diff --git a/src/iosanita/contenttypes/tests/test_custom_validations.py b/src/iosanita/contenttypes/tests/test_custom_validations.py index ae42cee..e44bf85 100644 --- a/src/iosanita/contenttypes/tests/test_custom_validations.py +++ b/src/iosanita/contenttypes/tests/test_custom_validations.py @@ -50,7 +50,7 @@ def test_event_raise_badrequest_if_missing_organizzato_da(self): "blocks": { "b8850749-4d5f-4b5f-b842-c050bcf7fd6f": { "@type": "slate", - "text": "xxx", + "plaintext": "xxx", } }, }, @@ -103,7 +103,7 @@ def test_event_raise_badrequest_if_missing_organizzato_da(self): "blocks": { "b8850749-4d5f-4b5f-b842-c050bcf7fd6f": { "@type": "slate", - "text": "xxx", + "plaintext": "xxx", } }, } @@ -171,7 +171,7 @@ def test_raise_bad_request_if_missing_a_chi_si_rivolge_fields(self): "blocks": { "b8850749-4d5f-4b5f-b842-c050bcf7fd6f": { "@type": "slate", - "text": "xxx", + "plaintext": "xxx", } }, }