Skip to content

Commit 6e569d0

Browse files
cekkplone@umbar.hetzner.redturtle.itmamico
authored
Do not break serializers for tipologia_notizia utility (#306)
* Do not break serializers if there is not tipologia_notizia taxonomy utility. * convert getUtility in queryUtility * [mauro] logica booleana su condizione apertura_bando * changelog * fix changelog --------- Co-authored-by: [email protected] <[email protected]> Co-authored-by: Mauro Amico <[email protected]>
1 parent 34607bb commit 6e569d0

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

CHANGES.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Changelog
44
6.3.8 (unreleased)
55
------------------
66

7-
- Nothing changed yet.
8-
7+
- Do not break serializers if there is not tipologia_notizia taxonomy utility.
8+
[cekk]
9+
- Fix operator order in condition in summary for apertura_bando
10+
[mamico]
911

1012
6.3.7 (2025-04-28)
1113
------------------
@@ -38,7 +40,6 @@ Changelog
3840
Add "Dichiarazioni di insussistenza e incompatibilità" to Persona
3941
[lucabel]
4042

41-
4243
6.3.4 (2025-03-07)
4344
------------------
4445

src/design/plone/contenttypes/patches/baseserializer.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from plone.restapi.serializer.dxcontent import SerializeToJson
2222
from Products.CMFCore.utils import getToolByName
2323
from zope.component import getMultiAdapter
24-
from zope.component import getUtility
24+
from zope.component import queryUtility
2525
from zope.i18n import translate
2626

2727

@@ -37,16 +37,21 @@ def design_italia_serialize_to_json_call(self, version=None, include_items=True)
3737
ttool[self.context.portal_type].Title(), context=self.request
3838
)
3939
if self.context.portal_type == "News Item":
40-
if getattr(self.context, "tipologia_notizia", ""):
41-
taxonomy = getUtility(
40+
tipologia_notizia = getattr(self.context, "tipologia_notizia", "")
41+
if tipologia_notizia:
42+
taxonomy = queryUtility(
4243
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
4344
)
44-
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
45-
46-
title = taxonomy_voc.inv_data.get(self.context.tipologia_notizia, None)
47-
48-
if title and title.startswith(PATH_SEPARATOR):
49-
result["design_italia_meta_type"] = title.replace(PATH_SEPARATOR, "", 1)
45+
if taxonomy:
46+
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
47+
48+
title = taxonomy_voc.inv_data.get(self.context.tipologia_notizia, None)
49+
if title and title.startswith(PATH_SEPARATOR):
50+
result["design_italia_meta_type"] = title.replace(
51+
PATH_SEPARATOR, "", 1
52+
)
53+
else:
54+
result["design_italia_meta_type"] = tipologia_notizia
5055
return result
5156

5257

src/design/plone/contenttypes/restapi/serializers/dxcontent.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212
from plone.restapi.serializer.dxcontent import SerializeToJson as BaseSerializer
1313
from zope.component import adapter
14-
from zope.component import getUtility
14+
from zope.component import queryUtility
1515
from zope.i18n import translate
1616
from zope.interface import implementer
1717

@@ -21,9 +21,11 @@ def get_design_meta_type(self):
2121
ttool = api.portal.get_tool("portal_types")
2222
tipologia_notizia = getattr(self.context, "tipologia_notizia", "")
2323
if self.context.portal_type == "News Item" and tipologia_notizia:
24-
taxonomy = getUtility(
24+
taxonomy = queryUtility(
2525
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
2626
)
27+
if not taxonomy:
28+
return tipologia_notizia
2729
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
2830
if isinstance(tipologia_notizia, list):
2931
token = tipologia_notizia[0]

src/design/plone/contenttypes/restapi/serializers/summary.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
from zope.component import adapter
2424
from zope.component import getMultiAdapter
25-
from zope.component import getUtility
25+
from zope.component import queryUtility
2626
from zope.component import queryMultiAdapter
2727
from zope.globalrequest import getRequest
2828
from zope.i18n import translate
@@ -42,10 +42,22 @@ def get_taxonomy_information(field_name, context, res):
4242
"""
4343
Get the proper values for taxonomy fields
4444
"""
45+
value = getattr(context, field_name, None)
46+
if not value:
47+
return res
48+
4549
request = getRequest()
46-
taxonomy = getUtility(ITaxonomy, name=f"collective.taxonomy.{field_name}")
47-
taxonomy_voc = taxonomy.makeVocabulary(request.get("LANGUAGE"))
4850

51+
taxonomy = queryUtility(ITaxonomy, name=f"collective.taxonomy.{field_name}")
52+
if not taxonomy:
53+
# utility not found, return default
54+
if isinstance(value, list):
55+
res[field_name] = [{"token": token, "title": token} for token in value]
56+
else:
57+
res[field_name] = {"token": value, "title": value}
58+
return res
59+
60+
taxonomy_voc = taxonomy.makeVocabulary(request.get("LANGUAGE"))
4961
# il summary di un fullobject torna un value
5062
# il summary di un brain torna una lista (collective.taxonomy ha motivi per
5163
# fare così).
@@ -238,9 +250,12 @@ def get_design_meta_type(self):
238250
if self.context.portal_type == "News Item":
239251
tipologia_notizia = getattr(self.context, "tipologia_notizia", "")
240252
if tipologia_notizia:
241-
taxonomy = getUtility(
253+
taxonomy = queryUtility(
242254
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
243255
)
256+
if not taxonomy:
257+
# utility not found, return default
258+
return tipologia_notizia
244259
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
245260
if isinstance(tipologia_notizia, list):
246261
token = tipologia_notizia[0]

0 commit comments

Comments
 (0)