Skip to content

Commit

Permalink
Use IBlocks identifier to get Objects with blocks, make use of visit_…
Browse files Browse the repository at this point in the history
…blocks function to get nested blocks
  • Loading branch information
jonaspiterek committed Jul 26, 2024
1 parent 66da46a commit 097b624
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/plone/restapi/services/blocktypes/get.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from plone import api
from plone.restapi.services import Service
from collections import Counter
from plone.dexterity.content import get_assignable
from plone.restapi.behaviors import IBlocks
from plone.restapi.blocks import visit_blocks


class BlockTypesGet(Service):
Expand All @@ -14,26 +15,17 @@ def reply(self):
blocktypes = request_body.get("blocktypes").split(",")

for blocktype in blocktypes:
brains = catalog.searchResults(block_types=blocktype)
brains = catalog(object_provides=IBlocks.__identifier__)
result[blocktype] = Counter()

for brain in brains:
obj = brain.getObject()
assignable = get_assignable(obj)
url = brain.getPath() # or .getURL()
title = obj.title
result[blocktype][title] = Counter()

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})
for block in visit_blocks(obj, obj.blocks):
if block["@type"] == blocktype:
result[blocktype][title].update({url: 1})

return result

0 comments on commit 097b624

Please sign in to comment.