Skip to content

Commit

Permalink
Add indexer to index authors from a blog post (#52)
Browse files Browse the repository at this point in the history
* add indexer to index authors from a blog post

* update install test

* news
  • Loading branch information
jnptk authored Jan 29, 2024
1 parent 697f2eb commit 908690d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 2 deletions.
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)

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"

0 comments on commit 908690d

Please sign in to comment.