Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Sep 17, 2024
1 parent 2bc4c00 commit bcf9dd1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
28 changes: 15 additions & 13 deletions src/iosanita/contenttypes/restapi/deserializers/dxcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
_(
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/iosanita/contenttypes/tests/test_custom_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
},
},
Expand Down Expand Up @@ -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",
}
},
}
Expand Down Expand Up @@ -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",
}
},
}
Expand Down

0 comments on commit bcf9dd1

Please sign in to comment.