From 2cbeef5820de0c485117238bb654cb4de2f68f90 Mon Sep 17 00:00:00 2001 From: jonaspiterek Date: Fri, 17 May 2024 16:56:40 +0200 Subject: [PATCH] Add endpoint that uses plone.volto indexer --- .../restapi/services/blocktypes/__init__.py | 0 .../services/blocktypes/configure.zcml | 15 +++++++ src/plone/restapi/services/blocktypes/get.py | 39 +++++++++++++++++++ src/plone/restapi/services/configure.zcml | 1 + 4 files changed, 55 insertions(+) create mode 100644 src/plone/restapi/services/blocktypes/__init__.py create mode 100644 src/plone/restapi/services/blocktypes/configure.zcml create mode 100644 src/plone/restapi/services/blocktypes/get.py diff --git a/src/plone/restapi/services/blocktypes/__init__.py b/src/plone/restapi/services/blocktypes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/plone/restapi/services/blocktypes/configure.zcml b/src/plone/restapi/services/blocktypes/configure.zcml new file mode 100644 index 0000000000..56f2127305 --- /dev/null +++ b/src/plone/restapi/services/blocktypes/configure.zcml @@ -0,0 +1,15 @@ + + + + + diff --git a/src/plone/restapi/services/blocktypes/get.py b/src/plone/restapi/services/blocktypes/get.py new file mode 100644 index 0000000000..c4f942e5c7 --- /dev/null +++ b/src/plone/restapi/services/blocktypes/get.py @@ -0,0 +1,39 @@ +from plone import api +from plone.restapi.services import Service +from collections import Counter +from plone.dexterity.content import get_assignable + + +class BlockTypesGet(Service): + def reply(self): + catalog = api.portal.get_tool(name="portal_catalog") + request_body = self.request.form + result = {} + + if request_body.get("blocktypes") != "": + blocktypes = request_body.get("blocktypes").split(",") + + for blocktype in blocktypes: + brains = catalog.searchResults(block_types=blocktype) + result[blocktype] = Counter() + + for brain in brains: + obj = brain.getObject() + assignable = get_assignable(obj) + + hasBlocksBehavior = bool( + { + behavior + for behavior in assignable.enumerateBehaviors() + if behavior.name == "volto.blocks" + } + ) + + if hasBlocksBehavior: + url = brain.getURL() # or brain.getPath() + + for block in obj.blocks.values(): + if block["@type"] == blocktype: + result[blocktype].update({url: 1}) + + return result diff --git a/src/plone/restapi/services/configure.zcml b/src/plone/restapi/services/configure.zcml index ed31ccf121..0a443df55f 100644 --- a/src/plone/restapi/services/configure.zcml +++ b/src/plone/restapi/services/configure.zcml @@ -13,6 +13,7 @@ +