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

Rename index and column blog to blog_uid #49

Merged
merged 3 commits into from
Nov 28, 2023
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
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions news/48.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rename blog index and metadata to blog_uid [@ericof]
5 changes: 5 additions & 0 deletions src/collective/blog/content/author.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collective.blog import utils
from plone.dexterity.content import Container
from zope.interface import implementer
from zope.interface import Interface
Expand All @@ -10,3 +11,7 @@ class IAuthor(Interface):
@implementer(IAuthor)
class Author(Container):
"""An Author."""

def blog_uid(self):
"""Return the uid of the nearest blog object."""
return utils.find_blog_container_uid(self)
5 changes: 5 additions & 0 deletions src/collective/blog/content/post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collective.blog import _
from collective.blog import utils
from plone.app.z3cform.widget import AjaxSelectFieldWidget
from plone.autoform import directives
from plone.dexterity.content import Container
Expand Down Expand Up @@ -70,3 +71,7 @@ class IPost(Interface):
@implementer(IPost)
class Post(Container):
"""A Blog Post."""

def blog_uid(self):
"""Return the uid of the nearest blog object."""
return utils.find_blog_container_uid(self)
14 changes: 0 additions & 14 deletions src/collective/blog/indexers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
from Acquisition import aq_parent
from collective.blog.behaviors.blog import IBlogInfo
from plone import api
from plone.dexterity.content import DexterityContent
from Products.CMFPlone.Portal import IPloneSiteRoot


def _find_blog_uid(obj: DexterityContent) -> str:
"""Find a blog."""
if IBlogInfo.providedBy(obj):
return api.content.get_uuid(obj)
else:
# Up one level (get parent) if not reached the root of the site
return "" if IPloneSiteRoot.providedBy(obj) else _find_blog_uid(aq_parent(obj))
5 changes: 1 addition & 4 deletions src/collective/blog/indexers/author.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from Acquisition import aq_parent
from collective.blog.content.author import Author
from collective.blog.content.author import IAuthor
from collective.blog.indexers import _find_blog_uid
from plone.indexer import indexer


@indexer(IAuthor)
def blog_author_indexer(obj: Author):
"""Return blog uuid."""
parent = aq_parent(obj)
return _find_blog_uid(parent)
return obj.blog_uid()
4 changes: 2 additions & 2 deletions src/collective/blog/indexers/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<!-- Indexers/Metadata -->
<adapter
factory=".author.blog_author_indexer"
name="blog"
name="blog_uid"
/>

<adapter
factory=".post.blog_post_indexer"
name="blog"
name="blog_uid"
/>

</configure>
5 changes: 1 addition & 4 deletions src/collective/blog/indexers/post.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from Acquisition import aq_parent
from collective.blog.content.post import IPost
from collective.blog.content.post import Post
from collective.blog.indexers import _find_blog_uid
from plone.indexer import indexer


@indexer(IPost)
def blog_post_indexer(obj: Post):
"""Return blog uuid."""
parent = aq_parent(obj)
return _find_blog_uid(parent)
return obj.blog_uid()
15 changes: 14 additions & 1 deletion src/collective/blog/profiles/default/catalog.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<object name="portal_catalog">

<!-- Remove old blog index and column-->
<index meta_type="FieldIndex"
name="blog"
remove="true"
>
<indexed_attr value="blog" />
</index>

<column value="blog" />
<column remove="true"
value="blog"
/>

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

<column value="blog_uid" />

</object>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
>
<value purge="false">
<element>Author</element>
<element>Blog</element>
<element>BlogFolder</element>
</value>
</record>

Expand Down
4 changes: 2 additions & 2 deletions src/collective/blog/profiles/default/types/BlogFolder.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<object xmlns:i18n="http://xml.zope.org/namespaces/i18n"
meta_type="Dexterity FTI"
name="BlogFolder"
name="Blog"
i18n:domain="collective.blog"
>

<!-- Basic properties -->
<property name="title"
i18n:translate=""
>BlogFolder</property>
>Blog</property>
<property name="description"
i18n:translate=""
>A folderish content item representing a Blog</property>
Expand Down
8 changes: 8 additions & 0 deletions src/collective/blog/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
title="Rename Blog fti as BlogFolder"
import_steps="typeinfo"
/>
<genericsetup:upgradeDepends
title="Catalog: Add blog_uid column and index"
import_steps="catalog"
/>
<genericsetup:upgradeStep
title="Update old Blog objects with new portal_type"
handler=".v1002.recatalog_portal_type"
/>
<genericsetup:upgradeStep
title="Recatalog Author and Post items"
handler=".v1002.recatalog_blog_uid"
/>
</genericsetup:upgradeSteps>


Expand Down
21 changes: 14 additions & 7 deletions src/collective/blog/upgrades/v1002.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ def _migrate_existing_content(old_pt: str, new_pt: str) -> int:
for brain in brains:
obj = brain.getObject()
obj.portal_type = new_pt
obj.reindexObject(
idxs=[
"portal_type",
"Type",
],
)
obj.reindexObject()
return total


Expand All @@ -30,4 +25,16 @@ def recatalog_portal_type(setup_tool: SetupTool):
for old_pt, new_pt in TYPES_MAPPING:
items = _migrate_existing_content(old_pt, new_pt)
logger.info(f"Converted {items} {old_pt} instances")
logger.info("Upgrade complete")
logger.info("Converted old Blog instances")


def recatalog_blog_uid(setup_tool: SetupTool):
"""Recatalog Post and Author blog_uid."""
with api.env.adopt_roles(["Manager"]):
brains = api.content.find(portal_type=["Author", "Post"])
total = len(brains)
logger.info(f"Recatalog {total} items")
for brain in brains:
obj = brain.getObject()
obj.reindexObject(idxs=["blog_uid"])
logger.info("Reindexed blog_uid")
21 changes: 21 additions & 0 deletions src/collective/blog/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from Acquisition import aq_parent
from collective.blog.behaviors.blog import IBlogInfo
from plone import api
from plone.dexterity.content import DexterityContent
from Products.CMFPlone.Portal import IPloneSiteRoot


_BLOG_TYPES = "BlogFolder"


def find_blog_container_uid(obj: DexterityContent) -> str:
"""Find a blog."""
if IBlogInfo.providedBy(obj) or obj.portal_type in _BLOG_TYPES:
return api.content.get_uuid(obj)
else:
# Up one level (get parent) if not reached the root of the site
return (
""
if IPloneSiteRoot.providedBy(obj)
else find_blog_container_uid(aq_parent(obj))
)
5 changes: 3 additions & 2 deletions tests/content/test_author.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ def test_indexer_blog(self, portal, authors_payload):
blog_uid = api.content.get_uuid(blog)
payload = authors_payload[0]
# Check there is no Author connected to this blog
brains = api.content.find(portal_type=CONTENT_TYPE, blog=blog_uid)
brains = api.content.find(portal_type=CONTENT_TYPE, blog_uid=blog_uid)
assert len(brains) == 0
# Create an Author inside the blog
with api.env.adopt_roles(["Manager"]):
content = api.content.create(container=authors_folder, **payload)

brains = api.content.find(portal_type=CONTENT_TYPE, blog=blog_uid)
brains = api.content.find(portal_type=CONTENT_TYPE, blog_uid=blog_uid)
assert len(brains) == 1
assert brains[0].Title == content.title
assert brains[0].blog_uid == blog_uid
1 change: 1 addition & 0 deletions tests/content/test_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_create(self, blogs_payload):
def test_create_valid_ids(self, blogs_payload, content_id, expected):
payload = deepcopy(blogs_payload[0])
payload["id"] = content_id
payload["safe_id"] = True
with api.env.adopt_roles(["Manager"]):
content = api.content.create(container=self.portal, **payload)
assert content.id == expected
Expand Down
2 changes: 1 addition & 1 deletion tests/setup/test_setup_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def _init(self, portal):
"portal_type",
[
"Author",
"Blog",
"BlogFolder",
"Collection",
"Document",
"Event",
Expand Down
29 changes: 28 additions & 1 deletion tests/setup/upgrades/test_upgrades_to_v1002.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,31 @@ def test_old_blogs_migrated(self, do_upgrade):
brains = api.content.find(portal_type="BlogFolder")
assert len(brains) == 1
assert self.old_blog.portal_type == "BlogFolder"
assert self.old_blog.Type() == "BlogFolder"
assert self.old_blog.Type() == "Blog"

def test_catalog_removed_indexes_columns(self, do_upgrade):
do_upgrade(self.dest)
catalog = api.portal.get_tool("portal_catalog")
indexes = list(catalog.indexes())
columns = list(catalog.schema())
assert "blog" not in indexes
assert "blog" not in columns

def test_catalog_added_indexes_columns(self, do_upgrade):
do_upgrade(self.dest)
catalog = api.portal.get_tool("portal_catalog")
indexes = list(catalog.indexes())
columns = list(catalog.schema())
assert "blog_uid" in indexes
assert "blog_uid" in columns

def test_reindex_blog_uid(self, authors_payload, do_upgrade):
author_payload = authors_payload[0]
with api.env.adopt_roles(["Manager"]):
container = self.old_blog["authors"]
author = api.content.create(container=container, **author_payload)
author_blog_uid = author.blog_uid()
do_upgrade(self.dest)
with api.env.adopt_roles(["Manager"]):
brains = api.content.find(portal_type="Author")
assert brains[0].blog_uid == author_blog_uid