From 6e53eed10d119640e19bc8bbd90c6bfeb736e19e Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Mon, 30 Sep 2024 12:02:12 +0200 Subject: [PATCH] add search_sections field and add more fields in search-filters endpoint --- src/iosanita/policy/interfaces.py | 10 +- .../services/iosanita_settings/configure.zcml | 1 + .../restapi/services/iosanita_settings/get.py | 11 ++- .../restapi/services/search_filters/get.py | 91 +++++++++++++++++++ 4 files changed, 108 insertions(+), 5 deletions(-) diff --git a/src/iosanita/policy/interfaces.py b/src/iosanita/policy/interfaces.py index 00e0b06..41ebb97 100644 --- a/src/iosanita/policy/interfaces.py +++ b/src/iosanita/policy/interfaces.py @@ -42,7 +42,15 @@ class IIoSanitaSettings(Interface): ], value_type=TextLine(), ) - + search_sections = SourceText( + title=_("search_sections_label", default="Sezioni ricerca"), + description=_( + "search_sections_help", + default="Inserire una lista di sezioni per la ricerca.", + ), + default="", + required=False, + ) quick_search = SourceText( title=_("quick_search_label", default="Scorcatoie di ricerca"), description=_( diff --git a/src/iosanita/policy/restapi/services/iosanita_settings/configure.zcml b/src/iosanita/policy/restapi/services/iosanita_settings/configure.zcml index 1e2d276..095791b 100644 --- a/src/iosanita/policy/restapi/services/iosanita_settings/configure.zcml +++ b/src/iosanita/policy/restapi/services/iosanita_settings/configure.zcml @@ -10,6 +10,7 @@ factory=".get.IoSanitaSettingsGet" for="zope.interface.Interface" permission="zope2.View" + layer="iosanita.policy.interfaces.IIosanitaPolicyLayer" name="@iosanita-settings-data" /> diff --git a/src/iosanita/policy/restapi/services/iosanita_settings/get.py b/src/iosanita/policy/restapi/services/iosanita_settings/get.py index 978945d..ba4dda8 100644 --- a/src/iosanita/policy/restapi/services/iosanita_settings/get.py +++ b/src/iosanita/policy/restapi/services/iosanita_settings/get.py @@ -34,10 +34,13 @@ def __call__(self, expand=False): return result def get_field_from_registry(self, field_name): - value = ( - api.portal.get_registry_record(field_name, interface=IIoSanitaSettings) - or "" - ) + try: + value = ( + api.portal.get_registry_record(field_name, interface=IIoSanitaSettings) + or "" + ) + except KeyError: + return None if value: value = json.loads(value) else: diff --git a/src/iosanita/policy/restapi/services/search_filters/get.py b/src/iosanita/policy/restapi/services/search_filters/get.py index 204f163..4548032 100644 --- a/src/iosanita/policy/restapi/services/search_filters/get.py +++ b/src/iosanita/policy/restapi/services/search_filters/get.py @@ -1,7 +1,16 @@ # -*- coding: utf-8 -*- +from AccessControl.unauthorized import Unauthorized +from collective.taxonomy import PATH_SEPARATOR +from collective.taxonomy.interfaces import ITaxonomy from iosanita.policy.interfaces import IIoSanitaSettings from plone import api +from plone.registry.interfaces import IRegistry +from plone.restapi.interfaces import ISerializeToJsonSummary from plone.restapi.services import Service +from Products.CMFPlone.interfaces import ISearchSchema +from zope.component import getMultiAdapter +from zope.component import getUtility +from zope.i18n import translate import json @@ -10,6 +19,12 @@ class SearchFiltersGET(Service): def reply(self): return { "quick_search": self.get_data_from_registry(field_id="quick_search"), + "sections": self.get_sections(), + "portal_types": self.get_portal_types(), + "a_chi_si_rivolge_tassonomia": self.get_taxonomy_data( + name="a_chi_si_rivolge_tassonomia" + ), + "parliamo_di": self.get_taxonomy_data(name="parliamo_di"), } def get_data_from_registry(self, field_id): @@ -20,3 +35,79 @@ def get_data_from_registry(self, field_id): except KeyError: return [] return json.loads(values or "[]") + + def get_sections(self): + utils = api.portal.get_tool(name="plone_utils") + + sections = [] + for setting in self.get_data_from_registry(field_id="search_sections"): + items = [] + for section_settings in setting.get("items") or []: + for uid in section_settings.get("linkUrl") or []: + try: + section = api.content.get(UID=uid) + except Unauthorized: + # private folder + continue + if not section: + continue + item_infos = getMultiAdapter( + (section, self.request), + ISerializeToJsonSummary, + )() + if section_settings.get("expandItems", True): + children = section.listFolderContents( + contentFilter={"portal_type": utils.getUserFriendlyTypes()} + ) + item_infos["items"] = [ + getMultiAdapter( + (x, self.request), + ISerializeToJsonSummary, + )() + for x in children + ] + else: + # do not expand childrens, the only item is the section/container itself + item_infos["items"] = [ + getMultiAdapter( + (section, self.request), + ISerializeToJsonSummary, + )() + ] + item_infos["title"] = section_settings.get("title", "") + items.append(item_infos) + if items: + sections.append( + { + "rootPath": setting.get("rootPath", ""), + "items": items, + } + ) + return sections + + def get_portal_types(self): + ttool = api.portal.get_tool("portal_types") + ptool = api.portal.get_tool("plone_utils") + registry = getUtility(IRegistry) + search_settings = registry.forInterface(ISearchSchema, prefix="plone") + types_not_searched = search_settings.types_not_searched + types = [ + { + "label": translate(ttool[t].Title(), context=self.request), + "id": t, + } + for t in ptool.getUserFriendlyTypes() + if t not in types_not_searched + ] + return sorted(types, key=lambda k: k["label"]) + + def get_taxonomy_data(self, name): + taxonomy = getUtility(ITaxonomy, name=f"collective.taxonomy.{name}") + taxonomy_voc = taxonomy.makeVocabulary(self.request.get("LANGUAGE")) + data = [] + for label, value in taxonomy_voc.iterEntries(): + if label.startswith(PATH_SEPARATOR): + label = label.replace(PATH_SEPARATOR, "", 1) + + data.append({"label": label.split(PATH_SEPARATOR)[-1], "value": value}) + return data