Skip to content

Commit

Permalink
Add endpoint that uses plone.volto indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaspiterek committed May 17, 2024
1 parent 9bbb1d7 commit 2cbeef5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
Empty file.
15 changes: 15 additions & 0 deletions src/plone/restapi/services/blocktypes/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:plone="http://namespaces.plone.org/plone"
xmlns:zcml="http://namespaces.zope.org/zcml"
>

<plone:service
method="GET"
factory=".get.BlockTypesGet"
for="zope.interface.Interface"
permission="zope2.View"
name="@blocktypes"
/>

</configure>
39 changes: 39 additions & 0 deletions src/plone/restapi/services/blocktypes/get.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/plone/restapi/services/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<include package=".actions" />
<include package=".addons" />
<include package=".aliases" />
<include package=".blocktypes" />
<include package=".breadcrumbs" />
<include package=".content" />
<include package=".controlpanels" />
Expand Down

0 comments on commit 2cbeef5

Please sign in to comment.