import time
import dateutil.parser
import transaction
from pytz import utc
from zExceptions import BadRequest
from DateTime.interfaces import IDateTime
from Products.BTreeFolder2.BTreeFolder2 import manage_addBTreeFolder
from babble.server.conversation import Conversation
from babble.server.interfaces import IChatService
from babble.server.message import Message
def run(self):
    * Move existing messages from messageboxes in IUser objects, messageboxes
      in IConversation objects.
    * Rename all old messages' ids to remove the previx "message."
    * Change all old messages' time attr to python's datetime.
    services = []
    for o in self.objectValues():
        if IChatService.providedBy(o):
    for service in services:
        if not hasattr(service, 'conversations'):
            manage_addBTreeFolder(service, 'conversations', 'Conversations')
        convs = service._getOb('conversations')
        for user in getattr(service, 'users').objectValues():
            for oldmbox in user.objectValues():
                conv = service._getConversation(,
                newmbox = Conversation._getMessageBox(conv,
                for message in oldmbox.objectValues():
                    if !=
                        # We now only store one author's messages per mbox. So
                        # we ignore the other messages. They will be deleted.
                    if IDateTime.providedBy(message.time):
                        message.time = message.time.asdatetime().replace(tzinfo=utc)
                    elif type(message.time) == str:
                        message.time = dateutil.parser.parse(message.time)
                    newid = '%f' % time.mktime(message.time.timetuple())
                    message.time = message.time.isoformat()
                    newmsg= Message(message.text,
                    newmsg.time = message.time
           = newid
                        newmbox._setObject(newid, message)
                    except BadRequest:
    return "Succesfully migrated the messages"