function to recursively migrate comments added.

svn path=/plone.app.discussion/trunk/; revision=28016
This commit is contained in:
Timo Stollenwerk 2009-07-12 16:09:34 +00:00
parent d7eaab75c8
commit 18ee5159bd
2 changed files with 73 additions and 32 deletions

View File

@ -13,6 +13,8 @@ from Products.CMFCore.interfaces import IContentish
from zope.component import createObject from zope.component import createObject
from plone.app.discussion.comment import CommentFactory
from plone.app.discussion.interfaces import IConversation from plone.app.discussion.interfaces import IConversation
@ -24,10 +26,42 @@ class View(BrowserView):
context = aq_inner(self.context) context = aq_inner(self.context)
out = [] out = []
self.total_comments_migrated = 0
def log(msg): def log(msg):
context.plone_log(msg) context.plone_log(msg)
out.append(msg) out.append(msg)
def migrate_replies(context, in_reply_to, replies, depth=0):
# recursive function to migrate all direct replies
# of a comment
for reply in replies:
# log
indent = " "
for i in range(depth):
indent += " "
log("%smigrate_reply: '%s'." % (indent, reply.title))
# create a reply object
comment = CommentFactory()
comment.title = reply.Title()
comment.text = reply.text
comment.creator = reply.Creator()
#comment.author_name = author_username
#comment.author_email = ""
#comment.creation_date
comment.reply_to = in_reply_to
new_in_reply_to = conversation.addComment(comment)
self.total_comments_migrated += 1
# migrate all talkbacks of the reply
talkback = getattr( reply, 'talkback', None )
migrate_replies(context, new_in_reply_to, talkback.getReplies(), depth=depth+1)
log("Comment migration started.") log("Comment migration started.")
# Find content # Find content
@ -35,29 +69,24 @@ class View(BrowserView):
dtool = context.portal_discussion dtool = context.portal_discussion
brains = catalog.searchResults( brains = catalog.searchResults(
object_provides='Products.CMFCore.interfaces._content.IContentish') object_provides='Products.CMFCore.interfaces._content.IContentish')
log("Found %s content objects to migrate." % len(brains)) comment_brains = catalog.searchResults(Type='Discussion Item')
total_comment_brains = len(comment_brains)
log("Found %s content objects." % len(brains))
log("Found %s Discussion Item objects." % total_comment_brains)
for brain in brains: for brain in brains:
if brain.portal_type != 'Discussion Item': if brain.portal_type != 'Discussion Item':
old_comments = []
obj = brain.getObject() obj = brain.getObject()
talkback = getattr( obj, 'talkback', None ) talkback = getattr( obj, 'talkback', None )
if talkback: if talkback:
replies = talkback.objectValues() replies = talkback.getReplies()
log("%s: Found talkback with %s comments to migrate"\ if replies:
% (obj.absolute_url(relative=1), len(replies))) conversation = IConversation(obj)
for reply in replies: log("Create conversation for: '%s'" % obj.Title())
old_comments.append(reply) log("%s: Found talkback" % obj.absolute_url(relative=1))
migrate_replies(context, 0, replies)
# Build up new conversation/comments structure
conversation = IConversation(obj)
for old_comment in old_comments:
comment = createObject('plone.Comment')
comment.title = old_comment.Title()
comment.text = old_comment.text
comment.Creator = old_comment.Creator
conversation.addComment(comment)
log("Comment migration finished.") log("Comment migration finished.")
return out log("%s of %s comments migrated."
% (self.total_comments_migrated, total_comment_brains))
return '\n'.join(out)

View File

@ -35,12 +35,16 @@ class MigrationTest(PloneTestCase):
def test_migrate_comment(self): def test_migrate_comment(self):
# Create one comment # Create one comment
self.discussion.getDiscussionFor(self.doc) talkback = self.discussion.getDiscussionFor(self.doc)
self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim') self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim')
reply = self.doc.talkback.objectValues()[0] #reply = talkback.objectValues()[0]
self.assertEqual(reply.Title(), 'My Title') reply = talkback.getReplies()[0]
self.assertEqual(reply.EditableBody(), 'My Text') reply.setReplyTo(self.doc)
self.assertEquals(reply.Title(), 'My Title')
self.assertEquals(reply.EditableBody(), 'My Text')
self.failUnless('Jim' in reply.listCreators()) self.failUnless('Jim' in reply.listCreators())
self.assertEquals(talkback.replyCount(self.doc), 1)
self.assertEquals(reply.inReplyTo(), self.doc)
# Call migration script # Call migration script
self.view() self.view()
@ -53,10 +57,17 @@ class MigrationTest(PloneTestCase):
# Check migration # Check migration
self.assertEquals(conversation.total_comments, 1) self.assertEquals(conversation.total_comments, 1)
self.failUnless(conversation.getComments().next()) self.failUnless(conversation.getComments().next())
self.assert_(IComment.providedBy(conversation.getComments().next())) comment1 = conversation.values()[0]
self.assertEquals(conversation.values()[0].Title(), 'My Title') self.assert_(IComment.providedBy(comment1))
self.assertEquals(conversation.values()[0].text, 'My Text') self.assertEquals(comment1.Title(), 'My Title')
self.assertEquals(conversation.values()[0].Creator(), 'Jim') self.assertEquals(comment1.text, 'My Text')
self.assertEquals(comment1.Creator(), 'Jim')
self.assertEquals(
[{'comment': comment1, 'depth': 0, 'id': long(comment1.id)},]
, list(conversation.getThreads()))
def test_migrate_allow_discussion(self):
pass
def test_migrate_nested_comments(self): def test_migrate_nested_comments(self):
# Create some nested comments and migrate them # Create some nested comments and migrate them
@ -89,13 +100,14 @@ class MigrationTest(PloneTestCase):
# Check migration # Check migration
conversation = IConversation(self.doc) conversation = IConversation(self.doc)
self.assertEquals(conversation.total_comments, 2) self.assertEquals(conversation.total_comments, 2)
self.assert_(IComment.providedBy(conversation.getComments().next()))
# XXX: This is not very elegant comment1 = conversation.values()[0]
self.failUnless('First comment' in conversation.values()[0].Title() or comment2 = conversation.values()[1]
'Re: First comment' in conversation.values()[0].Title())
self.failUnless('This is my first comment.' in conversation.values()[0].text or self.assertEquals(
'This is my first reply.' in conversation.values()[1].text) [{'comment': comment1, 'depth': 0, 'id': long(comment1.id)},
{'comment': comment2, 'depth': 0, 'id': long(comment2.id)},
], list(conversation.getThreads()))
def test_suite(): def test_suite():
return unittest.defaultTestLoader.loadTestsFromName(__name__) return unittest.defaultTestLoader.loadTestsFromName(__name__)