Skip to content

Commit

Permalink
Add migration for button block width (#32)
Browse files Browse the repository at this point in the history
* Add migration for button block width

* Fix the transaction commit problem

* Fix vscode
  • Loading branch information
sneridagh authored Mar 13, 2024
1 parent 176e07f commit a0caaf2
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 6 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dist/
docs/_build
__pycache__/
.tox
.vscode/
node_modules/

# venv / buildout related
Expand Down
26 changes: 24 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ showcontent = true
##

[tool.isort]
profile = "plone"
profile = "black"
force_alphabetical_sort = true
force_single_line = true
lines_after_imports = 2
line_length = 88

##
# Add extra configuration options in .meta.toml:
Expand All @@ -59,7 +63,25 @@ profile = "plone"
##

[tool.black]
target-version = ["py38"]
line-length = 88
target-version = ['py311']
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
)
'''

##
# Add extra configuration options in .meta.toml:
Expand Down
105 changes: 105 additions & 0 deletions scripts/migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from plone import api
from plone.restapi.blocks import visit_blocks
from zope.component.hooks import setSite
from zope.lifecycleevent import modified

import logging
import transaction


logger = logging.getLogger("migrate_to_4")
logger.setLevel(logging.INFO)

# If you updated or extended the colors mappings, you should update this with the new values
COLOR_MAP = {
"grey": {
"--background-color": "#ecebeb",
},
"transparent": {
"--background-color": "transparent",
},
}

WIDTH_MAP = {
"narrow": {
"--block-width": "var(--narrow-container-width)",
},
"wide": {
"--block-width": "var(--default-container-width)",
},
}


def migrate_backgroundColor(portal):
i = 0
output = ""
for brain in portal.portal_catalog(
object_provides="plone.restapi.behaviors.IBlocks"
):
obj = brain.getObject()
blocks = obj.blocks
logger.info(f"Processing {obj.absolute_url()}")
for block in visit_blocks(obj, blocks):
if block.get("styles", False) and block["styles"].get(
"backgroundColor", False
):
new_block = block.copy()
color = block["styles"]["backgroundColor"]
new_block["styles"]["backgroundColor:noprefix"] = COLOR_MAP[color]
del new_block["styles"]["backgroundColor"]
block.clear()
block.update(new_block)
logger.info(
f'{obj.absolute_url()} - Updated "backgroundColor" to "backgroundColor:noprefix"'
)

obj.blocks = blocks
modified(obj)
i += 1
if not i % 100:
logger.info(i)
transaction.commit()
transaction.commit()
return output


def migrate_button_block_width(portal):
i = 0
output = ""
for brain in portal.portal_catalog(
object_provides="plone.restapi.behaviors.IBlocks"
):
obj = brain.getObject()
blocks = obj.blocks
logger.info(f"Processing {obj.absolute_url()}")
for block in visit_blocks(obj, blocks):
if (
block.get("@type", False)
and block["@type"] == "__button"
and block.get("styles", False)
and block["styles"].get("buttonAlign", False)
and block["styles"]["buttonAlign"] in ["narrow", "wide"]
):
new_block = block.copy()
new_block["styles"]["blockWidth:noprefix"] = WIDTH_MAP.get(
new_block["styles"]["buttonAlign"], ""
)
del new_block["styles"]["buttonAlign"]
block.clear()
block.update(new_block)
logger.info(f'{obj.absolute_url()} - Updated "width"')

obj.blocks = blocks
modified(obj)
i += 1
if not i % 100:
logger.info(i)
transaction.commit()
transaction.commit()
return output


setSite(app.Plone) # noQA
with api.env.adopt_user("admin"):
migrate_button_block_width(app.Plone) # noQA
migrate_backgroundColor(app.Plone) # noQA
59 changes: 56 additions & 3 deletions src/kitconcept/voltolighttheme/browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from plone.restapi.blocks import visit_blocks
from Products.Five.browser import BrowserView
from zope.interface import alsoProvides
from zope.lifecycleevent import modified

import logging
import transaction
Expand All @@ -20,6 +21,15 @@
},
}

WIDTH_MAP = {
"narrow": {
"--block-width": "var(--narrow-container-width)",
},
"wide": {
"--block-width": "var(--default-container-width)",
},
}


def migrate_backgroundColor(portal):
i = 0
Expand All @@ -29,7 +39,7 @@ def migrate_backgroundColor(portal):
):
obj = brain.getObject()
blocks = obj.blocks
output += f"Processing {obj.absolute_url()}\n"
logger.info(f"Processing {obj.absolute_url()}")
for block in visit_blocks(obj, blocks):
if block.get("styles", False) and block["styles"].get(
"backgroundColor", False
Expand All @@ -40,8 +50,48 @@ def migrate_backgroundColor(portal):
del new_block["styles"]["backgroundColor"]
block.clear()
block.update(new_block)
output += f'{obj.absolute_url()} - Updated "backgroundColor" to "backgroundColor:noprefix"\n'
logger.info(
f'{obj.absolute_url()} - Updated "backgroundColor" to "backgroundColor:noprefix"'
)

obj.blocks = blocks
modified(obj)
i += 1
if not i % 100:
logger.info(i)
transaction.commit()
transaction.commit()
return output


def migrate_button_block_width(portal):
i = 0
output = ""
for brain in portal.portal_catalog(
object_provides="plone.restapi.behaviors.IBlocks"
):
obj = brain.getObject()
blocks = obj.blocks
logger.info(f"Processing {obj.absolute_url()}")
for block in visit_blocks(obj, blocks):
if (
block.get("@type", False)
and block["@type"] == "__button"
and block.get("styles", False)
and block["styles"].get("buttonAlign", False)
and block["styles"]["buttonAlign"] in ["narrow", "wide"]
):
new_block = block.copy()
new_block["styles"]["blockWidth:noprefix"] = WIDTH_MAP.get(
new_block["styles"]["buttonAlign"], ""
)
del new_block["styles"]["buttonAlign"]
block.clear()
block.update(new_block)
logger.info(f'{obj.absolute_url()} - Updated "width"')

obj.blocks = blocks
modified(obj)
i += 1
if not i % 100:
logger.info(i)
Expand All @@ -53,4 +103,7 @@ def migrate_backgroundColor(portal):
class MigrateToV3(BrowserView):
def __call__(self):
alsoProvides(self.request, IDisableCSRFProtection)
return migrate_backgroundColor(self.context)
output = ""
output += migrate_backgroundColor(self.context) + "\n"
output += migrate_button_block_width(self.context) + "\n"
return output

0 comments on commit a0caaf2

Please sign in to comment.