From dab5558343482b96866d4a41d565014fd8404572 Mon Sep 17 00:00:00 2001 From: Mauro Amico Date: Thu, 27 Jun 2024 19:08:45 +0200 Subject: [PATCH] backport #65 --- CHANGES.rst | 6 ++-- .../policy/restapi/search_filters/get.py | 33 +++++++++++-------- .../policy/tests/test_search_filters_api.py | 30 +++++++++++++++-- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 2eaa382..3aa70dc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,8 +4,10 @@ Changelog 4.0.10 (unreleased) ------------------- -- Nothing changed yet. - +- `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] 4.0.9 (2024-04-11) ------------------ diff --git a/src/design/plone/policy/restapi/search_filters/get.py b/src/design/plone/policy/restapi/search_filters/get.py index 5ab5ec7..1faaff2 100644 --- a/src/design/plone/policy/restapi/search_filters/get.py +++ b/src/design/plone/policy/restapi/search_filters/get.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- from AccessControl.unauthorized import Unauthorized -from design.plone.contenttypes.controlpanels.settings import ( - IDesignPloneSettings, -) +from design.plone.contenttypes.controlpanels.settings import IDesignPloneSettings from plone import api from plone.registry.interfaces import IRegistry from plone.restapi.interfaces import ISerializeToJsonSummary @@ -13,6 +11,7 @@ from zope.i18n import translate from zope.interface import implementer from zope.publisher.interfaces import IPublishTraverse + import json @@ -59,16 +58,24 @@ def reply(self): (section, self.request), ISerializeToJsonSummary, )() - children = section.listFolderContents() - if children: - item_infos["items"] = [] - for children in section.listFolderContents(): - item_infos["items"].append( - getMultiAdapter( - (children, self.request), - ISerializeToJsonSummary, - )() - ) + if section_settings.get("expandItems", True): + children = section.listFolderContents() + if children: + item_infos["items"] = [] + for children in section.listFolderContents(): + item_infos["items"].append( + getMultiAdapter( + (children, self.request), + ISerializeToJsonSummary, + )() + ) + else: + item_infos["items"] = [ + getMultiAdapter( + (section, self.request), + ISerializeToJsonSummary, + )() + ] if section_settings.get("title", ""): item_infos["title"] = section_settings["title"] items.append(item_infos) 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 d07bdc2..4d96daf 100644 --- a/src/design/plone/policy/tests/test_search_filters_api.py +++ b/src/design/plone/policy/tests/test_search_filters_api.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- -from design.plone.policy.testing import ( - DESIGN_PLONE_POLICY_API_FUNCTIONAL_TESTING, -) +from design.plone.contenttypes.controlpanels.settings import IDesignPloneSettings +from design.plone.policy.testing import DESIGN_PLONE_POLICY_API_FUNCTIONAL_TESTING from plone import api from plone.app.testing import setRoles from plone.app.testing import SITE_OWNER_NAME @@ -13,6 +12,7 @@ from transaction import commit from zope.component import getUtility +import json import unittest @@ -99,3 +99,27 @@ def test_endpoint_return_list_of_searchable_types(self): self.assertIn("portal_types", response) types = [x["id"] for x in response["portal_types"]] self.assertNotIn("Document", types) + + 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)