From bee861135b53fbc68e391e7447eac247f45450c2 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Wed, 20 Nov 2024 10:19:18 +0100 Subject: [PATCH] Add NewsItem summary serializer, to return always metadata infos about tipologia_notizia and tipologia_notizia_metadata. --- CHANGES.rst | 3 ++- .../restapi/serializers/configure.zcml | 1 + .../restapi/serializers/summary.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index b83a6b5..cf10e96 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,8 @@ Changelog 1.0.2 (unreleased) ------------------ -- Nothing changed yet. +- Add NewsItem summary serializer, to return always metadata infos about tipologia_notizia and tipologia_notizia_metadata. + [cekk] 1.0.1 (2024-11-19) diff --git a/src/iosanita/contenttypes/restapi/serializers/configure.zcml b/src/iosanita/contenttypes/restapi/serializers/configure.zcml index 6870d6f..b78cd47 100644 --- a/src/iosanita/contenttypes/restapi/serializers/configure.zcml +++ b/src/iosanita/contenttypes/restapi/serializers/configure.zcml @@ -6,6 +6,7 @@ + diff --git a/src/iosanita/contenttypes/restapi/serializers/summary.py b/src/iosanita/contenttypes/restapi/serializers/summary.py index 6d69415..6fbe948 100644 --- a/src/iosanita/contenttypes/restapi/serializers/summary.py +++ b/src/iosanita/contenttypes/restapi/serializers/summary.py @@ -9,6 +9,9 @@ from zope.component import adapter from zope.interface import implementer from zope.interface import Interface +from plone.app.contenttypes.interfaces import INewsItem +from zope.component import queryMultiAdapter +from plone.indexer.interfaces import IIndexableObject import logging @@ -27,6 +30,7 @@ def default_metadata_fields(self): "a_chi_si_rivolge_tassonomia_metadata", "id", "tipologia_notizia", + "tipologia_notizia_metadata", "start", "end", "recurrence", @@ -116,3 +120,17 @@ def __call__(self): data["contatti"] = getattr(self.context, "contatti", []) return data + + +@implementer(ISerializeToJsonSummary) +@adapter(INewsItem, IIosanitaContenttypesLayer) +class NewsItemJSONSummarySerializer(IOSanitaJSONSummarySerializer): + def __call__(self): + data = super().__call__() + + catalog = api.portal.get_tool(name="portal_catalog") + adapter = queryMultiAdapter((self.context, catalog), IIndexableObject) + + for metadata in ["tipologia_notizia", "tipologia_notizia_metadata"]: + data[metadata] = getattr(adapter, metadata, data.get(metadata, "")) + return data