Skip to content

Commit 70ca3f0

Browse files
committed
feat: custom exporter for comments and tests
1 parent c04bedc commit 70ca3f0

5 files changed

Lines changed: 77 additions & 5 deletions

File tree

src/collective/exportimport/configure.zcml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<adapter factory=".serializer.ChoiceFieldSerializer" />
148148
<adapter factory=".serializer.long_converter" />
149149
<adapter factory=".serializer.ExportingBlocksJSONFieldSerializer" />
150+
<adapter zcml:condition="installed plone.app.discussion" factory=".serializer.CommentSerializer" />
150151

151152
<!-- Deserializers -->
152153
<adapter factory=".deserializer.RichTextFieldDeserializerWithoutUnescape" />

src/collective/exportimport/export_other.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from App.config import getConfiguration
44
from collective.exportimport import _
55
from collective.exportimport import config
6+
from collective.exportimport.interfaces import IMigrationMarker
67
from OFS.interfaces import IOrderedContainer
78
from operator import itemgetter
89
from plone import api
@@ -30,6 +31,7 @@
3031
from zope.component import getUtility
3132
from zope.component import queryMultiAdapter
3233
from zope.component import queryUtility
34+
from zope.interface import alsoProvides
3335
from zope.interface import providedBy
3436

3537
import json
@@ -602,7 +604,8 @@ def get_default_page_info(self, obj):
602604

603605
class ExportDiscussion(BaseExport):
604606
def __call__(self, download_to_server=False):
605-
self.title = _(u"Export comments")
607+
alsoProvides(self.request, IMigrationMarker)
608+
self.title = _("Export comments")
606609
self.download_to_server = download_to_server
607610
if not self.request.form.get("form.submitted", False):
608611
return self.index()

src/collective/exportimport/import_other.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,10 @@ def import_data(self, data):
635635
conversation = IConversation(obj)
636636

637637
for item in conversation_data["conversation"]["items"]:
638-
if isinstance(item["text"], dict) and item["text"].get("mime-type"):
639-
item["mime-type"] = item["text"]["mime-type"]
640638
if isinstance(item["text"], dict) and item["text"].get("data"):
641639
item["text"] = item["text"]["data"]
640+
if isinstance(item["text"], dict) and item["text"].get("mime-type"):
641+
item["mime-type"] = item["text"]["mime-type"]
642642

643643
comment = Comment()
644644
comment_id = int(item["comment_id"])
@@ -655,7 +655,7 @@ def import_data(self, data):
655655
.replace(u"\r<br />", u"\r\n")
656656
.replace(u"<br />", u"\r\n")
657657
)
658-
comment.mime_type = item.get("mime-type", None)
658+
comment.mime_type = item.get("mime-type", "text/html")
659659

660660
if item["user_notification"]:
661661
comment.user_notification = True

src/collective/exportimport/serializer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
else:
6262
HAS_PAC = True
6363

64+
try:
65+
pkg_resources.get_distribution("plone.app.discussion")
66+
except pkg_resources.DistributionNotFound:
67+
HAS_PAD = False
68+
else:
69+
HAS_PAD = True
70+
6471

6572
FILE_SIZE_WARNING = 10000000
6673
IMAGE_SIZE_WARNING = 5000000
@@ -586,6 +593,22 @@ def fix_criteria(self, criterion):
586593
return query
587594

588595

596+
if HAS_PAD:
597+
from plone.app.discussion.interfaces import IComment
598+
from plone.restapi.interfaces import ISerializeToJson
599+
from plone.restapi.serializer.discussion import CommentSerializer as Base
600+
601+
@implementer(ISerializeToJson)
602+
@adapter(IComment, IMigrationMarker)
603+
class CommentSerializer(Base):
604+
605+
def __call__(self, include_items=True):
606+
data = super().__call__(include_items)
607+
data["text"]["data"] = self.context.text
608+
data["text"]["mime-type"] = self.context.mime_type
609+
return data
610+
611+
589612
def get_dx_blob_path(obj):
590613
return get_blob_path(obj._blob)
591614

src/collective/exportimport/tests/test_export.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,26 @@ def test_export_discussion(self):
527527
)
528528
conversation = IConversation(doc1)
529529
comment = createObject("plone.Comment")
530-
comment.text = u"Comment text"
530+
comment.text = "Comment text"
531+
532+
comment_html = createObject("plone.Comment")
533+
comment_html.text = "Comment text <strong>with HTML</strong>"
534+
comment_html.mime_type = "text/html"
535+
536+
comment_intelligent_text = createObject("plone.Comment")
537+
comment_intelligent_text.text = "Comment text with link https://plone.org"
538+
comment_intelligent_text.mime_type = "text/x-web-intelligent"
539+
540+
comment_markdown = createObject("plone.Comment")
541+
comment_markdown.text = (
542+
"Comment text with link [Link to Plone](https://plone.org)"
543+
)
544+
comment_markdown.mime_type = "text/x-web-markdown"
545+
531546
conversation.addComment(comment)
547+
conversation.addComment(comment_html)
548+
conversation.addComment(comment_intelligent_text)
549+
conversation.addComment(comment_markdown)
532550
transaction.commit()
533551

534552
browser = self.open_page("@@export_discussion")
@@ -538,9 +556,36 @@ def test_export_discussion(self):
538556
contents = DATA[-1]
539557
data = json.loads(contents)
540558
self.assertEqual(data[0]["uuid"], doc1.UID())
559+
541560
self.assertEqual(
542561
data[0]["conversation"]["items"][0]["text"]["data"], "Comment text"
543562
)
563+
self.assertEqual(
564+
data[0]["conversation"]["items"][0]["text"]["mime-type"], "text/plain"
565+
)
566+
self.assertEqual(
567+
data[0]["conversation"]["items"][1]["text"]["data"],
568+
"Comment text <strong>with HTML</strong>",
569+
)
570+
self.assertEqual(
571+
data[0]["conversation"]["items"][1]["text"]["mime-type"], "text/html"
572+
)
573+
self.assertEqual(
574+
data[0]["conversation"]["items"][2]["text"]["data"],
575+
"Comment text with link https://plone.org",
576+
)
577+
self.assertEqual(
578+
data[0]["conversation"]["items"][2]["text"]["mime-type"],
579+
"text/x-web-intelligent",
580+
)
581+
self.assertEqual(
582+
data[0]["conversation"]["items"][3]["text"]["data"],
583+
"Comment text with link [Link to Plone](https://plone.org)",
584+
)
585+
self.assertEqual(
586+
data[0]["conversation"]["items"][3]["text"]["mime-type"],
587+
"text/x-web-markdown",
588+
)
544589

545590
def test_export_ordering(self):
546591
# First create some content.

0 commit comments

Comments
 (0)