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 indexer to index authors from a blog post #52

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/53.enhancement
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add indexer for indexing authors from a blog post [@jonaspiterek]
5 changes: 5 additions & 0 deletions src/collective/blog/indexers/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
name="blog_uid"
/>

<adapter
factory=".post.post_authors"
name="post_authors"
/>

</configure>
17 changes: 17 additions & 0 deletions src/collective/blog/indexers/post.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
from collective.blog.content.post import IPost
from collective.blog.content.post import Post
from plone import api
from plone.indexer import indexer


@indexer(IPost)
def blog_post_indexer(obj: Post):
"""Return blog uuid."""
return obj.blog_uid()


@indexer(IPost)
def post_authors(obj: Post):
"""Returns the authors of a blog post."""
authors = []
uids = obj.creators
for uid in uids:
searchResult = api.content.find(portal_type="Author", UID=uid)
if len(searchResult) > 0:
author = searchResult[0].getObject()

if author is not None:
authors.append(author.title)
Copy link
Member

@davisagli davisagli Jan 30, 2024

Choose a reason for hiding this comment

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

@jonaspiterek @ericof What problem are you trying to solve by adding an index of author names? It may be necessary, but keep in mind that if the author is edited later to fix a typo in the person's name, that will not automatically reindex all their blog posts.

Also: You don't need to use getObject here, which does unnecessary work. The item's title is available from the catalog brain object that is part of the results. (searchResult[0].Title)


return authors
10 changes: 10 additions & 0 deletions src/collective/blog/profiles/default/catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@

<column value="blog_uid" />

<!-- Add new index and column-->
<index meta_type="KeywordIndex"
name="post_authors"
>
<indexed_attr value="post_authors" />
</index>

<column value="post_authors" />


</object>
2 changes: 1 addition & 1 deletion src/collective/blog/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<version>1002</version>
<version>1003</version>
<dependencies>
<!-- <dependency>profile-plone.volto:default</dependency> -->
</dependencies>
Expand Down
14 changes: 14 additions & 0 deletions src/collective/blog/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,19 @@
/>
</genericsetup:upgradeSteps>

<genericsetup:upgradeSteps
profile="collective.blog:default"
source="1002"
destination="1003"
>
<genericsetup:upgradeDepends
title="Catalog: Add post_authors column and index"
import_steps="catalog"
/>
<genericsetup:upgradeStep
title="Recatalog Posts"
handler=".v1003.recatalog_post_authors"
/>
</genericsetup:upgradeSteps>

</configure>
15 changes: 15 additions & 0 deletions src/collective/blog/upgrades/v1003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from collective.blog import logger
from plone import api
from Products.GenericSetup.tool import SetupTool


def recatalog_post_authors(setup_tool: SetupTool):
"""Recatalog Posts."""
with api.env.adopt_roles(["Manager"]):
brains = api.content.find(portal_type=["Post"])
total = len(brains)
logger.info(f"Recatalog {total} items")
for brain in brains:
obj = brain.getObject()
obj.reindexObject(idxs=["post_authors"])
logger.info("Reindexed post_authors")
2 changes: 1 addition & 1 deletion tests/setup/test_setup_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def test_browserlayer(self, browser_layers):

def test_latest_version(self, profile_last_version):
"""Test latest version of default profile."""
assert profile_last_version(f"{PACKAGE_NAME}:default") == "1002"
assert profile_last_version(f"{PACKAGE_NAME}:default") == "1003"
Loading