Skip to content

Commit

Permalink
add indexer to index authors from a blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
jnptk committed Jan 25, 2024
1 parent 697f2eb commit 5126df5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
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)

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")

0 comments on commit 5126df5

Please sign in to comment.