This commit is contained in:
Timo Stollenwerk 2012-01-13 14:40:26 +01:00
parent 07cde3ca7b
commit cce409a729

View File

@ -18,7 +18,13 @@ from plone.app.discussion.interfaces import IConversation, IReplies, IComment
def DT2dt(DT): def DT2dt(DT):
"""Convert a Zope DateTime (with timezone) into a Python datetime (GMT).""" """Convert a Zope DateTime (with timezone) into a Python datetime (GMT)."""
DT = DT.toZone('GMT') DT = DT.toZone('GMT')
return datetime(DT.year(), DT.month(), DT.day(), DT.hour(), DT.minute(), int(DT.second())) return datetime(
DT.year(),
DT.month(),
DT.day(),
DT.hour(),
DT.minute(),
int(DT.second()))
class View(BrowserView): class View(BrowserView):
@ -32,25 +38,26 @@ class View(BrowserView):
self.total_comments_migrated = 0 self.total_comments_migrated = 0
self.total_comments_deleted = 0 self.total_comments_deleted = 0
dry_run = self.request.has_key("dry_run") dry_run = "dry_run" in self.request
# This is for testing only. # This is for testing only.
# Do not use transactions during a test. # Do not use transactions during a test.
test = self.request.has_key("test") test = "test" in self.request
if not test: if not test:
transaction.begin() # pragma: no cover transaction.begin() # pragma: no cover
catalog = getToolByName(context, 'portal_catalog') catalog = getToolByName(context, 'portal_catalog')
def log(msg): def log(msg):
# encode string before sending it to external world # encode string before sending it to external world
if isinstance(msg, unicode): if isinstance(msg, unicode):
msg = msg.encode('utf-8') # pragma: no cover msg = msg.encode('utf-8') # pragma: no cover
context.plone_log(msg) context.plone_log(msg)
out.append(msg) out.append(msg)
def migrate_replies(context, in_reply_to, replies, depth=0, just_delete=0): def migrate_replies(context, in_reply_to, replies,
depth=0, just_delete=0):
# Recursive function to migrate all direct replies # Recursive function to migrate all direct replies
# of a comment. Returns True if there are no replies to # of a comment. Returns True if there are no replies to
# this comment left, and therefore the comment can be removed. # this comment left, and therefore the comment can be removed.
@ -58,7 +65,7 @@ class View(BrowserView):
return True return True
for reply in replies: for reply in replies:
# log # log
indent = " " indent = " "
for i in range(depth): for i in range(depth):
@ -79,7 +86,7 @@ class View(BrowserView):
comment.text = reply.cooked_text comment.text = reply.cooked_text
comment.mime_type = 'text/html' comment.mime_type = 'text/html'
comment.creator = reply.Creator() comment.creator = reply.Creator()
email = reply.getProperty('email', None) email = reply.getProperty('email', None)
if email: if email:
comment.author_email = email comment.author_email = email
@ -101,12 +108,13 @@ class View(BrowserView):
self.total_comments_migrated += 1 self.total_comments_migrated += 1
# migrate all talkbacks of the reply # migrate all talkbacks of the reply
talkback = getattr( reply, 'talkback', None ) talkback = getattr(reply, 'talkback', None)
no_replies_left = migrate_replies(context, no_replies_left = migrate_replies(
new_in_reply_to, context,
talkback.getReplies(), new_in_reply_to,
depth=depth+1, talkback.getReplies(),
just_delete=not should_migrate) depth=depth + 1,
just_delete=not should_migrate)
if no_replies_left: if no_replies_left:
# remove reply and talkback # remove reply and talkback
@ -151,7 +159,7 @@ class View(BrowserView):
# Recursively run through the comment tree and migrate all comments. # Recursively run through the comment tree and migrate all comments.
for brain in new_brains: for brain in new_brains:
obj = brain.getObject() obj = brain.getObject()
talkback = getattr( obj, 'talkback', None ) talkback = getattr(obj, 'talkback', None)
if talkback: if talkback:
replies = talkback.getReplies() replies = talkback.getReplies()
if replies: if replies:
@ -166,10 +174,10 @@ class View(BrowserView):
if self.total_comments_deleted != self.total_comments_migrated: if self.total_comments_deleted != self.total_comments_migrated:
log("Something went wrong during migration. The number of \ log("Something went wrong during migration. The number of \
migrated comments (%s) differs from the number of deleted \ migrated comments (%s) differs from the number of deleted \
comments (%s)." # pragma: no cover comments (%s)." # pragma: no cover
% (self.total_comments_migrated, self.total_comments_deleted)) % (self.total_comments_migrated, self.total_comments_deleted))
if not test: # pragma: no cover if not test: # pragma: no cover
transaction.abort() # pragma: no cover transaction.abort() # pragma: no cover
log("Abort transaction") # pragma: no cover log("Abort transaction") # pragma: no cover
log("\n") log("\n")
@ -180,14 +188,16 @@ class View(BrowserView):
% (self.total_comments_migrated, count_comments_old)) % (self.total_comments_migrated, count_comments_old))
if self.total_comments_migrated != count_comments_old: if self.total_comments_migrated != count_comments_old:
log("%s comments could not be migrated." log("%s comments could not be migrated." %
% (count_comments_old - self.total_comments_migrated)) # pragma: no cover (count_comments_old - \
log("Please make sure your portal catalog is up-to-date.") # pragma: no cover self.total_comments_migrated)) # pragma: no cover
log("Please make sure your " +
"portal catalog is up-to-date.") # pragma: no cover
if dry_run and not test: if dry_run and not test:
transaction.abort() # pragma: no cover transaction.abort() # pragma: no cover
log("Dry run") # pragma: no cover log("Dry run") # pragma: no cover
log("Abort transaction") # pragma: no cover log("Abort transaction") # pragma: no cover
if not test: if not test:
transaction.commit() # pragma: no cover transaction.commit() # pragma: no cover
return '\n'.join(out) return '\n'.join(out)