Skip to content

Commit

Permalink
Rename tiles behavior and fields to blocks, migration step
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Oct 29, 2019
1 parent 0b68389 commit db34b4e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 9 deletions.
2 changes: 2 additions & 0 deletions news/821.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rename tiles behavior and fields to blocks, migration step.
[timo, sneridagh]
15 changes: 7 additions & 8 deletions src/plone/restapi/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
/>

<genericsetup:registerProfile
name="tiles"
title="plone.restapi tiles"
directory="profiles/tiles"
description="Enables tiles on the Document content type"
name="blocks"
title="Volto Blocks"
directory="profiles/blocks"
description="Enables blocks on the Document content type"
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

Expand Down Expand Up @@ -83,11 +83,10 @@
<include package="plone.behavior" file="meta.zcml"/>

<plone:behavior
name="plone.tiles"
title="Tiles"
description="Existing tiles on the object and their layout"
name="volto.blocks"
title="Blocks"
description="Enables Volto Blocks support"
provides=".behaviors.IBlocks"
for="plone.dexterity.interfaces.IDexterityContent"
/>

</configure>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plone/restapi/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<metadata>
<version>0004</version>
<version>0005</version>
</metadata>
7 changes: 7 additions & 0 deletions src/plone/restapi/tests/test_upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ def test_run_migration_profile_to_0004(self):
portal_setup = getToolByName(self.portal, "portal_setup")
assign_get_users_permission(portal_setup)
self.assertTrue(True)

def test_run_migration_profile_to_0005(self):
from plone.restapi.upgrades.to0005 import rename_tiles_to_blocks

portal_setup = getToolByName(self.portal, "portal_setup")
rename_tiles_to_blocks(portal_setup)
self.assertTrue(True)
9 changes: 9 additions & 0 deletions src/plone/restapi/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:upgradeStep
title="Rename tiles and tiles_layout fields from Tiles behavior to blocks"
description=""
source="0004"
destination="0005"
handler="plone.restapi.upgrades.to0005.rename_tiles_to_blocks"
profile="plone.restapi:default"
/>

</configure>
51 changes: 51 additions & 0 deletions src/plone/restapi/upgrades/to0005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
from plone import api
from zope.component import queryUtility
from plone.dexterity.interfaces import IDexterityFTI

import logging

logger = logging.getLogger(__name__)

OLD_BEHAVIOR_NAME = "plone.restapi.behaviors.ITiles"
SHORT_OLD_BEHAVIOR_NAME = "plone.tiles"
NEW_BEHAVIOR_NAME = "plone.restapi.behaviors.IBlocks"


def rename_tiles_to_blocks(setup_context):
"""Rename tiles and tiles_layout fields from Tiles behavior to blocks and blocks_layout
"""
pt = api.portal.get_tool("portal_types")

types_with_tiles_behavior = []

for _type in pt.objectIds():
fti = queryUtility(IDexterityFTI, name=_type)
if fti and OLD_BEHAVIOR_NAME in fti.behaviors:
types_with_tiles_behavior.append(_type)
new_fti = [
currentbehavior
for currentbehavior in fti.behaviors
if currentbehavior != OLD_BEHAVIOR_NAME
]
new_fti.append(NEW_BEHAVIOR_NAME)
fti.behaviors = tuple(new_fti)
logger.info("Migrated behavior of {} type".format(_type))

# In case we used the short behavior name
if fti and SHORT_OLD_BEHAVIOR_NAME in fti.behaviors:
types_with_tiles_behavior.append(_type)
new_fti = [
currentbehavior
for currentbehavior in fti.behaviors
if currentbehavior != SHORT_OLD_BEHAVIOR_NAME
]
new_fti.append(NEW_BEHAVIOR_NAME)
fti.behaviors = tuple(new_fti)
logger.info("Migrated behavior of {} type".format(_type))

for brain in api.content.find(portal_type=types_with_tiles_behavior):
obj = brain.getObject()
obj.blocks = getattr(obj, "tiles", {})
obj.blocks_layout = getattr(obj, "tiles_layout", {"items": []})
logger.info("Migrated fields of content object: {}".format(obj.absolute_url()))

0 comments on commit db34b4e

Please sign in to comment.