-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6f9919f
feat!: add a count to block_types indexer
jnptk 31eaec1
chore: expose block_types index to metadata
jnptk 320ad1e
feat: add endpoint to access block_types indexer
jnptk 78d066e
feat: aggregate block type counts when no specific block type is prov…
jnptk 477a4ea
fix: revert & fix last commit
jnptk 4356103
fix: don't set default
jnptk 7e6dabe
return summary as dict to make it more accessible in the frontend
jnptk 0d311ce
Merge branch 'main' into blocktypes
jnptk 411a273
protect endpoint (wip)
jnptk bedfe22
fix ComponentLookupError
jnptk d68cee5
Merge branch 'main' into blocktypes
jnptk e6deb93
return array if type is provided
jnptk 57a4029
add upgrade step
jnptk d16b588
Merge branch 'main' into blocktypes
jnptk 998fb58
Add tests for blocktypes endpoint
jnptk f7a6249
Update tests to have the same content available in portal, add doc st…
jnptk 44f19f1
Add changelog entries
jnptk 2e3ff48
Add docs in README.md
jnptk f5aec50
Update upgrade step
jnptk 0e8f92e
Update checklog entry for block_types metadata column
jnptk 690525e
Update upgrade step
jnptk 290790d
Make use of searchResults in case of custom permissions
jnptk b25f101
Here too
jnptk 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
This file contains hidden or 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
This file contains hidden or 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 @@ | ||
| Added a `block_types` metadata column to the catalog to include a count for each type. @jnptk |
This file contains hidden or 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 @@ | ||
| Add `/@blocktypes` endpoint to expose `block_types` index. @jnptk |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,8 @@ | ||
| <configure xmlns="http://namespaces.zope.org/zope"> | ||
|
|
||
| <permission | ||
| id="plone.volto.service.BlockTypes" | ||
| title="Plone Site Setup: Block Types" | ||
| /> | ||
|
|
||
| </configure> |
This file contains hidden or 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
This file contains hidden or 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,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> |
Empty file.
Empty file.
This file contains hidden or 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="plone.volto.service.BlockTypes" | ||
| name="@blocktypes" | ||
| /> | ||
|
|
||
| </configure> |
This file contains hidden or 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,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.searchResults(**query) | ||
|
|
||
| for brain in brains: | ||
| result.append( | ||
| { | ||
| "@id": brain.getURL(), | ||
| "title": brain.Title, | ||
| "count": brain.block_types[self.block_type], | ||
| } | ||
| ) | ||
| else: | ||
| result = {} | ||
| brains = catalog.searchResults(**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 | ||
|
|
||
davisagli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return result | ||
This file contains hidden or 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,8 @@ | ||
| <configure | ||
| xmlns="http://namespaces.zope.org/zope" | ||
| xmlns:zcml="http://namespaces.zope.org/zcml" | ||
| > | ||
|
|
||
| <include package=".blocktypes" /> | ||
|
|
||
| </configure> |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,56 @@ | ||
| from plone import api | ||
|
|
||
| import pytest | ||
| import transaction | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class") | ||
| def portal(portal_class): | ||
| yield portal_class | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class") | ||
| def contents(portal): | ||
| with api.env.adopt_roles(["Manager"]): | ||
| doc = api.content.create(portal, type="Document", id="lorem-ipsum") | ||
| doc.blocks = { | ||
| "1": {"@type": "title"}, | ||
| "2": {"@type": "teaser"}, | ||
| "3": { | ||
| "@type": "gridBlock", | ||
| "blocks": { | ||
| "1": {"@type": "teaser"}, | ||
| "2": {"@type": "teaser"}, | ||
| "3": {"@type": "teaser"}, | ||
| }, | ||
| }, | ||
| } | ||
| doc.reindexObject(idxs=["block_types"]) | ||
| transaction.commit() | ||
|
|
||
|
|
||
| class TestBlockTypesGet: | ||
| @pytest.fixture(autouse=True) | ||
| def _setup(self, contents, portal, api_manager_request): | ||
| self.portal = portal | ||
| self.api_session = api_manager_request | ||
|
|
||
| def test_response_type(self): | ||
| response = self.api_session.get("/@blocktypes") | ||
| data = response.json() | ||
| assert isinstance(data, dict) | ||
|
|
||
| def test_response_type_with_id(self): | ||
| response = self.api_session.get("/@blocktypes/title") | ||
| data = response.json() | ||
| assert isinstance(data, list) | ||
|
|
||
| def test_filtered(self): | ||
| response = self.api_session.get("/@blocktypes?path=/plone/lorem-ipsum") | ||
| data = response.json() | ||
| assert len(data) == 3 | ||
|
|
||
| def test_filtered_with_id(self): | ||
| response = self.api_session.get("/@blocktypes/teaser?path=/plone/lorem-ipsum") | ||
| data = response.json() | ||
| assert len(data) == 1 |
This file contains hidden or 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,52 @@ | ||
| from plone.app.testing import SITE_OWNER_NAME | ||
| from plone.app.testing import SITE_OWNER_PASSWORD | ||
| from plone.restapi.testing import RelativeSession | ||
| from zope.component.hooks import site | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def portal(functional): | ||
| """Provide the Plone portal instance for functional tests.""" | ||
| yield functional["portal"] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class") | ||
| def portal_class(functional_class): | ||
| """Provide the Plone portal instance for class-scoped functional tests.""" | ||
| if hasattr(functional_class, "testSetUp"): | ||
| functional_class.testSetUp() | ||
| portal = functional_class["portal"] | ||
| with site(portal): | ||
| yield portal | ||
| if hasattr(functional_class, "testTearDown"): | ||
| functional_class.testTearDown() | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def request_api_factory(portal): | ||
| """Provide a factory function for creating Plone REST API session objects.""" | ||
|
|
||
| def factory(): | ||
| url = portal.absolute_url() | ||
| api_session = RelativeSession(f"{url}/++api++") | ||
| return api_session | ||
|
|
||
| return factory | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def api_anon_request(request_api_factory): | ||
| """Provide an unauthenticated REST API session for anonymous requests.""" | ||
| request = request_api_factory() | ||
| yield request | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def api_manager_request(request_api_factory): | ||
| """Provide an authenticated REST API session with manager privileges.""" | ||
| request = request_api_factory() | ||
| request.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) | ||
| yield request | ||
| request.auth = () |
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.
Uh oh!
There was an error while loading. Please reload this page.