diff --git a/CHANGES.rst b/CHANGES.rst index 0ddb000..0e315cb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,10 +4,13 @@ Changelog 5.0.10 (unreleased) ------------------- +- `search_sections` in IDesignPloneSettings has a new filed in each section: + `expandItems` that is a boolean to decide if the items of the section should + be expanded or not (default is True). + [mamico] - Add a profile to limit addables on site root [lucabel] - 5.0.9 (2024-04-12) ------------------ diff --git a/src/design/plone/policy/restapi/search_filters/get.py b/src/design/plone/policy/restapi/search_filters/get.py index 6b8f0d8..78486bc 100644 --- a/src/design/plone/policy/restapi/search_filters/get.py +++ b/src/design/plone/policy/restapi/search_filters/get.py @@ -59,16 +59,25 @@ def reply(self): (section, self.request), ISerializeToJsonSummary, )() - children = section.listFolderContents( - contentFilter={"portal_type": utils.getUserFriendlyTypes()} - ) - item_infos["items"] = [ - getMultiAdapter( - (x, self.request), - ISerializeToJsonSummary, - )() - for x in children - ] + 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: diff --git a/src/design/plone/policy/tests/test_search_filters_api.py b/src/design/plone/policy/tests/test_search_filters_api.py index 6cc4b67..479c52e 100644 --- a/src/design/plone/policy/tests/test_search_filters_api.py +++ b/src/design/plone/policy/tests/test_search_filters_api.py @@ -10,7 +10,8 @@ from Products.CMFPlone.interfaces import ISearchSchema from transaction import commit from zope.component import getUtility - +from design.plone.contenttypes.controlpanels.settings import IDesignPloneSettings +import json import unittest @@ -126,3 +127,27 @@ def test_endpoint_do_not_return_contents_that_are_not_searchable(self): self.assertEqual("Amministrazione", amministrazione["title"]) self.assertEqual(7, len(amministrazione["items"])) + + def test_not_expand_items(self): + # first section has 7 children + response = self.api_session.get("/@search-filters").json() + self.assertEqual(len(response["sections"][0]["items"][0]["items"]), 7) + self.assertEqual(len(response["sections"][0]["items"][1]["items"]), 15) + + # change expandItems to False for the first section + settings = json.loads( + api.portal.get_registry_record( + "search_sections", interface=IDesignPloneSettings + ) + ) + settings[0]["items"][0]["expandItems"] = False + api.portal.set_registry_record( + "search_sections", json.dumps(settings), interface=IDesignPloneSettings + ) + commit() + + response = self.api_session.get("/@search-filters").json() + # first item now has only 1 child + self.assertEqual(len(response["sections"][0]["items"][0]["items"]), 1) + # second item still has 15 children + self.assertEqual(len(response["sections"][0]["items"][1]["items"]), 15)