-
-
Notifications
You must be signed in to change notification settings - Fork 6
Add endpoint to access block_types indexer #197
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
base: main
Are you sure you want to change the base?
Changes from all commits
6f9919f
31eaec1
320ad1e
78d066e
477a4ea
4356103
7e6dabe
0d311ce
411a273
bedfe22
d68cee5
e6deb93
57a4029
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,8 @@ | ||
| <configure xmlns="http://namespaces.zope.org/zope"> | ||
|
|
||
| <permission | ||
| id="plone.volto.service.BlockTypes" | ||
| title="Plone Site Setup: Block Types" | ||
| /> | ||
|
|
||
| </configure> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <rolemap> | ||
| <permissions> | ||
| <permission acquire="True" | ||
| name="Plone Site Setup: Block Types" | ||
| > | ||
| <role name="Manager" /> | ||
| <role name="Site Administrator" /> | ||
| </permission> | ||
| </permissions> | ||
| </rolemap> |
| 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="plone.volto.service.BlockTypes" | ||
| name="@blocktypes" | ||
| /> | ||
|
|
||
| </configure> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from plone import api | ||
| from plone.restapi.behaviors import IBlocks | ||
| from plone.restapi.services import Service | ||
| from zope.interface import implementer | ||
| from zope.publisher.interfaces import IPublishTraverse | ||
|
|
||
| import collections | ||
|
|
||
|
|
||
| @implementer(IPublishTraverse) | ||
| class BlockTypesGet(Service): | ||
| def __init__(self, context, request): | ||
| super().__init__(context, request) | ||
| self.block_type = None | ||
|
|
||
| def publishTraverse(self, request, name): | ||
| self.block_type = name | ||
| return self | ||
|
|
||
| def reply(self): | ||
| catalog = api.portal.get_tool(name="portal_catalog") | ||
| request_body = self.request.form | ||
| type = self.block_type | ||
|
|
||
| query = { | ||
| "object_provides": IBlocks.__identifier__, | ||
| } | ||
|
|
||
| if request_body.get("path"): | ||
| query["path"] = request_body["path"] | ||
|
|
||
| if type: | ||
| result = [] | ||
| query["block_types"] = self.block_type | ||
| brains = catalog.unrestrictedSearchResults(**query) | ||
|
|
||
| for brain in brains: | ||
| result.append( | ||
| { | ||
| "@id": brain.getURL(), | ||
| "title": brain.Title, | ||
| "count": brain.block_types[self.block_type], | ||
| } | ||
| ) | ||
| else: | ||
| result = {} | ||
| brains = catalog.unrestrictedSearchResults(**query) | ||
| block_types_total = collections.Counter() | ||
|
|
||
| for brain in brains: | ||
| block_types_total.update(brain.block_types) | ||
|
|
||
| for block_type, count in block_types_total.items(): | ||
| result[block_type] = count | ||
|
|
||
|
Member
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. To do: Add the code to get total counts for all blocks if
Member
Author
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. @davisagli I've updated the code to return the block types + a total count when no specific block type is provided. e.g.: {
"slate": 22,
"teaser": 1,
"title": 3
}also I changed that we don't return inside an "items" dict since we're very likely loading the items inside an "items" dict in the redux store, this way we can avoid having to access the items in the frontend like this:
Member
Author
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. nvm. that was not a good idea, I'll handle that in the frontend via the action... 😅
Member
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. Like we discussed this morning, I think it was a good idea after all :) |
||
| return result | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <configure | ||
| xmlns="http://namespaces.zope.org/zope" | ||
| xmlns:zcml="http://namespaces.zope.org/zcml" | ||
| > | ||
|
|
||
| <include package=".blocktypes" /> | ||
|
|
||
| </configure> |
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.
We need an upgrade step for this
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.
Since we modified an indexer here I guess we have to re-index the catalog, right? Is this what we would want:
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.
@jnptk yes, but...
We try to avoid upgrade steps which need to reindex all content, because it can take a long time in a large site. Actually for this reason, running the "catalog" import step updates the indexes and metadata but doesn't automatically reindex anything.
In this case we only have to reindex the items that provide IBlocks. So I think we should add a Python upgrade step which finds all blocks content (
portal_catalog.unrestrictedSearchResults(object_provides=IBlocks.__identifier__)) and reindexes just the new index.In case someone really wants to avoid it, they can run the upgrade steps in portal_setup where there is a checkbox to disable each step if needed.
We should mention this upgrade step in the release notes.