Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jonaspiterek
Copy link
Member

ToDo

  • Add page title data to response

@mister-roboto
Copy link

@jonaspiterek thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

Copy link

netlify bot commented May 17, 2024

Deploy Preview for plone-restapi canceled.

Name Link
🔨 Latest commit 097b624
🔍 Latest deploy log https://app.netlify.com/sites/plone-restapi/deploys/66a3b341a30b3c00080318ac

Comment on lines 16 to 17
for blocktype in blocktypes:
brains = catalog.searchResults(block_types=blocktype)
Copy link
Member

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.

result[blocktype] = Counter()

for brain in brains:
obj = brain.getObject()
Copy link
Member

@jensens jensens May 20, 2024

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 the volto.blocks behavior. This would save several an object wake ups.

Copy link
Member Author

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 the volto.blocks behavior like so: brains = catalog(object_provides=IBlocks.__identifier__) which returns brains just like the block_types index. And so I am asking my self: Doesn't this make the block_types index completely unnecessary? Because from the brains I get the object and from there I get the block values.

/cc @davisagli

Copy link
Member

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.

Copy link
Member

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)

Copy link
Member

@davisagli davisagli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonaspiterek Is there a compelling reason to add a new service for this? It should be possible to query the block_types index using the existing @querystring-search service (we might need to add a criterion for it in the querystring registry settings)

@jonaspiterek
Copy link
Member Author

@davisagli no there is no particular reason for this to be an extra service. I can take a look on how to do that via the @querystring-search service if an extra service for the indexer is not wanted.

@jonaspiterek
Copy link
Member Author

jonaspiterek commented Jul 26, 2024

@davisagli The blocktypes_indexer does not return as much information as I want to have so using the @querystring-search endpoint doesn't do the trick. I used functions from the indexer to make the endpoint similar to the indexer.

Making a request like: http://localhost:8080/Plone/@blocktypes?blocktypes=slate,image,introduction,separator,girdBlock

returns:

{
    "image": {
        "Event": {},
        "News Item": {
            "/Plone/news-item": 1
        },
        "Page": {
            "/Plone/page": 1
        },
        "Site": {}
    },
    "introduction": {
        "Event": {},
        "News Item": {},
        "Page": {
            "/Plone/page": 2
        },
        "Site": {}
    },
    "separator": {
        "Event": {},
        "News Item": {},
        "Page": {
            "/Plone/page": 4
        },
        "Site": {}
    },
    "slate": {
        "Event": {
            "/Plone/event": 1
        },
        "News Item": {
            "/Plone/news-item": 13
        },
        "Page": {
            "/Plone/page": 11
        },
        "Site": {
            "/Plone": 3
        }
    }
}

Note: the structure is not final, also I'm not sure if those are all information I want to display on the controlpanel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants