-
-
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
Open
jonaspiterek
wants to merge
3
commits into
main
Choose a base branch
from
blocktypes-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from plone import api | ||
from plone.restapi.services import Service | ||
from collections import Counter | ||
from plone.restapi.behaviors import IBlocks | ||
from plone.restapi.blocks import visit_blocks | ||
|
||
|
||
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(object_provides=IBlocks.__identifier__) | ||
result[blocktype] = Counter() | ||
|
||
for brain in brains: | ||
obj = brain.getObject() | ||
url = brain.getPath() # or .getURL() | ||
title = obj.title | ||
result[blocktype][title] = Counter() | ||
|
||
for block in visit_blocks(obj, obj.blocks): | ||
if block["@type"] == blocktype: | ||
result[blocktype][title].update({url: 1}) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The above query could ask the
object_provides
index for the (marker) interface of thevolto.blocks
behavior. This would save several an object wake ups.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 just tried the
object_provides
index for the marker interface of thevolto.blocks
behavior like so:brains = catalog(object_provides=IBlocks.__identifier__)
which returns brains just like theblock_types
index. And so I am asking my self: Doesn't this make theblock_types
index completely unnecessary? Because from the brains I get the object and from there I get the block values./cc @davisagli
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.
@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 comment
The 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):
api.portal.get_tool("portal_catalog").Indexes["block_types"].uniqueValues(withLengths=1)