Skip to content

Commit

Permalink
add search_sections field and add more fields in search-filters endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Sep 30, 2024
1 parent 548149d commit 6e53eed
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/iosanita/policy/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=_(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
factory=".get.IoSanitaSettingsGet"
for="zope.interface.Interface"
permission="zope2.View"
layer="iosanita.policy.interfaces.IIosanitaPolicyLayer"
name="@iosanita-settings-data"
/>

Expand Down
11 changes: 7 additions & 4 deletions src/iosanita/policy/restapi/services/iosanita_settings/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
91 changes: 91 additions & 0 deletions src/iosanita/policy/restapi/services/search_filters/get.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand All @@ -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

0 comments on commit 6e53eed

Please sign in to comment.