Skip to content

Commit 51fd6b6

Browse files
authored
Merge pull request #276 from collective/erral-index-comments
Index discussion items on import
2 parents c9480fa + cc4784d commit 51fd6b6

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Changelog
55
1.16 (unreleased)
66
-----------------
77

8-
- Skip setting a default page if the default is already set to the same value.
8+
- Notify the comment creation to have them indexed in the catalog
9+
[erral]
10+
- Skip setting a default page if the default is already set to the same value.
911
[mamico]
1012
- Fixed data extraction of Archetypes ATFilefields to concatenate all `Pdata` chunks, when the FileField data was still stored in Filestorage and not not migrated to use blobstorage (using plone.app.blobs).
1113
[@jnptk]

src/collective/exportimport/import_other.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
from zope.component import queryUtility
2525
from zope.component.interfaces import IFactory
2626
from zope.container.interfaces import INameChooser
27+
from zope.event import notify
2728
from zope.globalrequest import getRequest
2829
from zope.interface import alsoProvides
30+
from zope.lifecycleevent import ObjectAddedEvent
2931
from ZPublisher.HTTPRequest import FileUpload
3032

3133
import dateutil
@@ -681,6 +683,10 @@ def import_data(self, data):
681683
annotions = IAnnotations(obj)
682684
if DISCUSSION_ANNOTATION_KEY not in annotions:
683685
annotions[DISCUSSION_ANNOTATION_KEY] = aq_base(conversation)
686+
687+
comment = comment.__of__(conversation)
688+
notify(ObjectAddedEvent(comment))
689+
684690
added += 1
685691
logger.info("Added {} comments to {}".format(added, obj.absolute_url()))
686692
results += added

src/collective/exportimport/tests/test_import.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
from zope.annotation.interfaces import IAnnotations
2121
from zope.component import getUtility
2222
from zope.lifecycleevent import modified
23-
23+
from plone.app.discussion.interfaces import IConversation
24+
from zope.component import createObject
2425
import json
2526
import os
2627
import shutil
@@ -1484,6 +1485,50 @@ def test_reset_dates(self):
14841485
convert = catalog._catalog.indexes["modified"]._convert
14851486
self.assertEqual(indexdata["modified"], convert(reset_modification_date))
14861487

1488+
def test_import_discussion(self):
1489+
app = self.layer["app"]
1490+
portal = self.layer["portal"]
1491+
login(app, SITE_OWNER_NAME)
1492+
doc1 = api.content.create(
1493+
container=portal, type="Document", id="doc1", title="Document 1"
1494+
)
1495+
conversation = IConversation(doc1)
1496+
comment = createObject("plone.Comment")
1497+
comment.text = "Comment text"
1498+
conversation.addComment(comment)
1499+
transaction.commit()
1500+
1501+
browser = self.open_page("@@export_discussion")
1502+
browser.getForm(action="@@export_discussion").submit(name="form.submitted")
1503+
contents = browser.contents
1504+
if not browser.contents:
1505+
contents = DATA[-1]
1506+
data = json.loads(contents)
1507+
self.assertEqual(data[0]["uuid"], doc1.UID())
1508+
self.assertEqual(
1509+
data[0]["conversation"]["items"][0]["text"]["data"], "Comment text"
1510+
)
1511+
1512+
# Delete the comment to prepare the import
1513+
del conversation[comment.id]
1514+
self.assertEqual(len(conversation), 0)
1515+
1516+
transaction.commit()
1517+
1518+
browser = self.open_page("@@import_discussion")
1519+
browser.getForm(action="@@import_discussion").submit(name="form.submitted")
1520+
upload = browser.getControl(name="jsonfile")
1521+
upload.add_file(contents, "application/json", "export_discussion.json")
1522+
browser.getForm(action="@@import_discussion").submit()
1523+
1524+
self.assertIn("Imported 1 comments", browser.contents)
1525+
1526+
brains = api.content.find(portal_type="Discussion Item")
1527+
self.assertEqual(len(brains), 1)
1528+
conversation = IConversation(doc1)
1529+
1530+
self.assertEqual(len(conversation), 1)
1531+
14871532

14881533
def dateify(value):
14891534
return value.asdatetime().replace(microsecond=0)

0 commit comments

Comments
 (0)