ploneplone-6

Reset (remove) blocks and blocks_layout values


I have some news items imported from Plone 4 to Plone 6 and I discovered a bug when trying to edit an old item. Edit - save - edit... and the items is broken with some strange values on blocks - blocks_layout.

I want to remove the values, but with no success:

First try:

(Pdb) news_item
<FolderishNewsItem at /Plone/sandbox1/news/test-news-item>
(Pdb) hasattr(news_item, 'blocks')
True
(Pdb) news_item.blocks
{'89ec635f-0a69-4a72-a8fe-7e477a0ea0de': {'@type': 'title'}, 'undefined': {'@type': 'title'}, '72d988ce-e065-43d3-a9d0-975d49da6e52': {'@type': 'slate'}}
(Pdb) delattr(news_item, 'blocks')
*** AttributeError: 'FolderishNewsItem' object has no attribute 'blocks'

Second:

(Pdb) news_item
<FolderishNewsItem at /Plone/sandbox1/news/test-news-item>
(Pdb) item = news_item.aq_inner.aq_self
(Pdb) item
<FolderishNewsItem at test-news-item>
(Pdb) item.blocks
{'89ec635f-0a69-4a72-a8fe-7e477a0ea0de': {'@type': 'title'}, 'undefined': {'@type': 'title'}, '72d988ce-e065-43d3-a9d0-975d49da6e52': {'@type': 'slate'}}
(Pdb) delattr(item, 'blocks')
*** AttributeError: 'FolderishNewsItem' object has no attribute 'blocks'

How can I reset these fields? I found them here: https://github.com/plone/plone.restapi/blob/main/src/plone/restapi/behaviors.py#L33

UPDATE:

Initializing with None gives on news item view:

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 181, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 391, in publish_module
  Module ZPublisher.WSGIPublisher, line 285, in publish
  Module ZPublisher.mapply, line 98, in mapply
  Module ZPublisher.WSGIPublisher, line 68, in call_object
  Module plone.rest.service, line 22, in __call__
  Module plone.restapi.services, line 19, in render
  Module plone.restapi.services.content.get, line 16, in reply
  Module plone.restapi.serializer.dxcontent, line 167, in __call__
  Module plone.restapi.serializer.dxcontent, line 119, in __call__
  Module plone.restapi.serializer.blocks, line 29, in __call__
  Module plone.restapi.blocks, line 25, in visit_blocks
AttributeError: 'NoneType' object has no attribute 'values'

Solution

  • It's working by setting a default value: https://community.plone.org/t/solved-reset-remove-blocks-and-blocks-layout-values/19533/2?u=ghitab

    In my case I emptied them:

    default = {"blocks": {}, "blocks_layout": {"items": []}}
    news_item.blocks = default["blocks"]
    news_item.blocks_layout = default["blocks_layout"]