Skip to content

Commit ae0f3e6

Browse files
committed
Do not break serializers if there is not tipologia_notizia taxonomy utility.
1 parent 1ae9f82 commit ae0f3e6

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

CHANGES.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Changelog
1818
- Add "Emolumenti a carico della finanza pubblica" to Persona
1919
Add "Dichiarazioni di insussistenza e incompatibilità" to Persona
2020
[lucabel]
21-
21+
- Do not break serializers if there is not tipologia_notizia taxonomy utility.
22+
[cekk]
2223

2324
6.3.4 (2025-03-07)
2425
------------------

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from zope.component import getMultiAdapter
2424
from zope.component import getUtility
2525
from zope.i18n import translate
26+
from zope.interface.interfaces import ComponentLookupError
2627

2728

2829
original_serialize_to_json__call__ = SerializeToJson.__call__
@@ -37,16 +38,21 @@ def design_italia_serialize_to_json_call(self, version=None, include_items=True)
3738
ttool[self.context.portal_type].Title(), context=self.request
3839
)
3940
if self.context.portal_type == "News Item":
40-
if getattr(self.context, "tipologia_notizia", ""):
41-
taxonomy = getUtility(
42-
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
43-
)
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)
41+
tipologia_notizia = getattr(self.context, "tipologia_notizia", "")
42+
if tipologia_notizia:
43+
try:
44+
taxonomy = getUtility(
45+
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
46+
)
47+
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
48+
49+
title = taxonomy_voc.inv_data.get(self.context.tipologia_notizia, None)
50+
if title and title.startswith(PATH_SEPARATOR):
51+
result["design_italia_meta_type"] = title.replace(
52+
PATH_SEPARATOR, "", 1
53+
)
54+
except ComponentLookupError:
55+
result["design_italia_meta_type"] = tipologia_notizia
5056
return result
5157

5258

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414
from zope.component import getUtility
1515
from zope.i18n import translate
1616
from zope.interface import implementer
17+
from zope.interface.interfaces import ComponentLookupError
1718

1819

1920
class MetaTypeSerializer(object):
2021
def get_design_meta_type(self):
2122
ttool = api.portal.get_tool("portal_types")
2223
tipologia_notizia = getattr(self.context, "tipologia_notizia", "")
2324
if self.context.portal_type == "News Item" and tipologia_notizia:
24-
taxonomy = getUtility(
25-
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
26-
)
25+
try:
26+
taxonomy = getUtility(
27+
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
28+
)
29+
except ComponentLookupError:
30+
return tipologia_notizia
2731
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
2832
if isinstance(tipologia_notizia, list):
2933
token = tipologia_notizia[0]

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

+22-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from zope.i18n import translate
2929
from zope.interface import implementer
3030
from zope.interface import Interface
31+
from zope.interface.interfaces import ComponentLookupError
3132
from zope.schema import getFieldsInOrder
3233

3334
import logging
@@ -42,10 +43,22 @@ def get_taxonomy_information(field_name, context, res):
4243
"""
4344
Get the proper values for taxonomy fields
4445
"""
46+
value = getattr(context, field_name, None)
47+
if not value:
48+
return res
49+
4550
request = getRequest()
46-
taxonomy = getUtility(ITaxonomy, name=f"collective.taxonomy.{field_name}")
47-
taxonomy_voc = taxonomy.makeVocabulary(request.get("LANGUAGE"))
4851

52+
try:
53+
taxonomy = getUtility(ITaxonomy, name=f"collective.taxonomy.{field_name}")
54+
taxonomy_voc = taxonomy.makeVocabulary(request.get("LANGUAGE"))
55+
except ComponentLookupError:
56+
# utility not found, return default
57+
if isinstance(value, list):
58+
res[field_name] = [{"token": token, "title": token} for token in value]
59+
else:
60+
res[field_name] = {"token": value, "title": value}
61+
return res
4962
# il summary di un fullobject torna un value
5063
# il summary di un brain torna una lista (collective.taxonomy ha motivi per
5164
# fare così).
@@ -241,9 +254,13 @@ def get_design_meta_type(self):
241254
if self.context.portal_type == "News Item":
242255
tipologia_notizia = getattr(self.context, "tipologia_notizia", "")
243256
if tipologia_notizia:
244-
taxonomy = getUtility(
245-
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
246-
)
257+
try:
258+
taxonomy = getUtility(
259+
ITaxonomy, name="collective.taxonomy.tipologia_notizia"
260+
)
261+
except ComponentLookupError:
262+
# utility not found, return default
263+
return tipologia_notizia
247264
taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE"))
248265
if isinstance(tipologia_notizia, list):
249266
token = tipologia_notizia[0]

0 commit comments

Comments
 (0)