-
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add endpoint that uses plone.volto block_types
indexer (wip)
#1789
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above query could ask the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried the /cc @davisagli There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonaspiterek You can do that but then you end up loading the full object for all the content items that have blocks, and loading the full object is slower than loading the metadata that is stored as part of the catalog. The block_types index exists for the use case where you want to find all the items that have a particular type of block, without actually loading the full objects. Also the block_types index properly finds blocks that are nested inside other blocks, which your code doesn't do yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonaspiterek Btw if you just want to find out how many items have each type of block, you can also get that efficiently from the index like this (if I recall correctly): |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sure this can be optimized to one query with removal of the for loop.