|
| 1 | +from plone import api |
| 2 | +from plone.restapi.blocks import visit_blocks |
| 3 | +from zope.component.hooks import setSite |
| 4 | +from textwrap import indent |
| 5 | +import json |
| 6 | +import transaction |
| 7 | + |
| 8 | +BLOCK_FIELDS = { |
| 9 | + "icons_and_numbers": ["columns.text"], |
| 10 | + "icons_and_text": ["description", "columns.text"], |
| 11 | + "numbers": ["numbers.text"], |
| 12 | + "slider": ["description"], |
| 13 | + "text1": ["content"], |
| 14 | + "text5": ["description", "text1", "text2", "content"], |
| 15 | + "text6": ["content"], |
| 16 | + "text7": ["content"], |
| 17 | +} |
| 18 | + |
| 19 | +def find_values(value: dict, fields): |
| 20 | + for field in fields: |
| 21 | + if "." in field: |
| 22 | + field, rest = field.split(".", 1) |
| 23 | + else: |
| 24 | + rest = None |
| 25 | + if field not in value: |
| 26 | + continue |
| 27 | + nextvalue = value[field] |
| 28 | + if rest and isinstance(nextvalue, list): |
| 29 | + for item in nextvalue: |
| 30 | + yield from find_values(item, [rest]) |
| 31 | + else: |
| 32 | + yield value, field, nextvalue |
| 33 | + |
| 34 | + |
| 35 | +def process_entities(text, entityRanges, entityMap): |
| 36 | + if len(entityRanges) == 1: |
| 37 | + offset = entityRanges[0]["offset"] |
| 38 | + length = entityRanges[0]["length"] |
| 39 | + key = entityRanges[0]["key"] |
| 40 | + pretext, linktext, posttext = text[:offset], text[offset:offset + length], text[offset + length:] |
| 41 | + result = [] |
| 42 | + if pretext: |
| 43 | + result.append({"text": pretext}) |
| 44 | + url = entityMap[str(key)]["data"]["url"] |
| 45 | + result.append({ |
| 46 | + "type": "link", |
| 47 | + "children": [{"text": linktext}], |
| 48 | + "data": {"url": url} |
| 49 | + }) |
| 50 | + if posttext: |
| 51 | + result.append({"text": posttext}) |
| 52 | + return result |
| 53 | + elif len(entityRanges) == 0: |
| 54 | + return [{"text": text}] |
| 55 | + else: |
| 56 | + print(json.dumps(text, indent=4)) |
| 57 | + breakpoint() |
| 58 | + |
| 59 | + |
| 60 | +def migrate_draft_to_slate(value, entityMap=None) -> list: |
| 61 | + match value: |
| 62 | + case {"blocks": [*blocks], "entityMap": entityMap}: |
| 63 | + result = [] |
| 64 | + for block in blocks: |
| 65 | + result.extend(migrate_draft_to_slate(block, entityMap)) |
| 66 | + return result |
| 67 | + case {"type": blocktype, "text": text, "entityRanges": entityRanges}: |
| 68 | + children = process_entities(text, entityRanges, entityMap) |
| 69 | + match blocktype: |
| 70 | + case "unstyled" | "align-left" | "buttons": |
| 71 | + return [{"type": "p", "children": children}] |
| 72 | + case "unordered-list-item": |
| 73 | + return [{"type": "ul", "children": [{"li": {"children": [{"text": text}]}}]}] |
| 74 | + case "blockquote": |
| 75 | + return [{"type": "blockquote", "children": children}] |
| 76 | + case _: |
| 77 | + breakpoint() |
| 78 | + raise Exception() |
| 79 | + case _: |
| 80 | + print(json.dumps(value, indent=4)) |
| 81 | + breakpoint() |
| 82 | + raise Exception() |
| 83 | + |
| 84 | + |
| 85 | +def migrate_items(): |
| 86 | + catalog = api.portal.get_tool(name="portal_catalog") |
| 87 | + i = 0 |
| 88 | + for brain in catalog.unrestrictedSearchResults(block_types=list(BLOCK_FIELDS.keys())): |
| 89 | + obj = brain.getObject() |
| 90 | + |
| 91 | + for block in visit_blocks(obj, obj.blocks): |
| 92 | + block_type = block.get("@type") |
| 93 | + for container, field, value in find_values(block, BLOCK_FIELDS.get(block_type, [])): |
| 94 | + if isinstance(value, list): |
| 95 | + # no value, or already migrated |
| 96 | + continue |
| 97 | + elif value is None: |
| 98 | + newvalue = [] |
| 99 | + else: |
| 100 | + newvalue = migrate_draft_to_slate(value) |
| 101 | + if newvalue != value: |
| 102 | + container[field] = newvalue |
| 103 | + obj._p_changed = True |
| 104 | + i += 1 |
| 105 | + print(f"{brain.getPath()} / {block_type} / {field}") |
| 106 | + print(indent(json.dumps(value, indent=4), " ")) |
| 107 | + print() |
| 108 | + print(indent(json.dumps(newvalue, indent=4), " ")) |
| 109 | + print() |
| 110 | + |
| 111 | + print(f"Total fields processed: {i}") |
| 112 | + transaction.commit() |
| 113 | + |
| 114 | +setSite(app.Plone) |
| 115 | +migrate_items() |
0 commit comments