Skip to content

Commit

Permalink
Fix missing TextField mimetypes
Browse files Browse the repository at this point in the history
This refs WEB-4003
  • Loading branch information
laulaz committed Nov 13, 2023
1 parent a52f565 commit 478ee4b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Changelog
1.2.2 (unreleased)
------------------

- Nothing changed yet.
- WEB-4003 : Fix missing TextField mimetypes
[laulaz]


1.2.1 (2023-10-29)
Expand Down
2 changes: 1 addition & 1 deletion src/imio/smartweb/common/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<version>1020</version>
<version>1021</version>
<dependencies>
<dependency>profile-plone.restapi:default</dependency>
<dependency>profile-eea.facetednavigation:default</dependency>
Expand Down
16 changes: 14 additions & 2 deletions src/imio/smartweb/common/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def added_content(obj, event):
value = getattr(obj, name)
if IRichTextValue.providedBy(value):
str = clean_invisible_char(value.raw)
setattr(obj, name, RichTextValue(str))
new_value = RichTextValue(
str,
mimeType=value.mimeType,
outputMimeType=value.outputMimeType,
encoding=value.encoding,
)
setattr(obj, name, new_value)
reindex_breadcrumb(obj, event)


Expand All @@ -48,7 +54,13 @@ def modified_content(obj, event):
continue
if IRichTextValue.providedBy(value):
str = clean_invisible_char(value.raw)
setattr(obj, name, RichTextValue(str))
new_value = RichTextValue(
str,
mimeType=value.mimeType,
outputMimeType=value.outputMimeType,
encoding=value.encoding,
)
setattr(obj, name, new_value)
reindex_breadcrumb(obj, event)


Expand Down
2 changes: 2 additions & 0 deletions src/imio/smartweb/common/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_cleaning_text(self):
document.text = text
modified(document)
self.assertEqual(document.text.raw, "<p>Kamoulox</p>")
self.assertEqual(document.text.mimeType, "text/html")
self.assertEqual(document.text.outputMimeType, "text/html")

document = api.content.create(
container=self.folder,
Expand Down
8 changes: 8 additions & 0 deletions src/imio/smartweb/common/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,12 @@
profile="imio.smartweb.common:default"
/>

<genericsetup:upgradeStep
title="Restore missing textfields mimetypes"
source="1020"
destination="1021"
handler=".upgrades.restore_textfields_mimetypes"
profile="imio.smartweb.common:default"
/>

</configure>
31 changes: 31 additions & 0 deletions src/imio/smartweb/common/upgrades/upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from plone import api
from plone.app.imagecropping import PAI_STORAGE_KEY
from plone.app.imagecropping.interfaces import IImageCroppingMarker
from plone.app.textfield.value import IRichTextValue
from plone.app.textfield.value import RichTextValue
from plone.dexterity.utils import iterSchemata
from zope.annotation.interfaces import IAnnotations
from zope.schema import getFields

import logging

Expand Down Expand Up @@ -54,3 +58,30 @@ def remove_deprecated_cropping_annotations(context):
logger.info(
f'Remove deprecated scales : {",".join(deleted_scales)} cropping annotation on {obj.absolute_url()}'
)


def restore_textfields_mimetypes(content):
brains = api.content.find(
portal_type=[
"imio.events.Event",
"imio.news.NewsItem",
"imio.smartweb.CirkwiView",
"imio.smartweb.DirectoryView",
"imio.smartweb.SectionText",
]
)
for brain in brains:
obj = brain.getObject()
for schema in iterSchemata(obj):
for name, field in getFields(schema).items():
value = getattr(obj, name)
if not IRichTextValue.providedBy(value):
continue
new_value = RichTextValue(
value.raw,
mimeType="text/html",
outputMimeType="text/x-html-safe",
encoding="utf-8",
)
setattr(obj, name, new_value)
logger.info(f"Fixed WYSIWYG field mimetypes on {obj.absolute_url()}")

0 comments on commit 478ee4b

Please sign in to comment.