ContentMigration Example

When migrating Plone from 2.5 to 4.x, you might want to migrate the custom Archetypes packages. Here is one simple example. Note that this might not be best practice, but should be working if your migration case is simple as mine.

First create a migration.py file in myproj.atcontent/myproj/atcontent/Extensions/

# -*- coding: utf-8 -*-

from Products.contentmigration.archetypes import InplaceATItemMigrator, ATItemMigrator
from Products.contentmigration.walker import CustomQueryWalker
from Products.contentmigration.archetypes import *
from Products.contentmigration.common import unrestricted_rename

from transaction import savepoint
from Products.CMFCore.utils import getToolByName

def getMigrationWalker(context, migrator):
    """ set up migration walker using the given item migrator """
    portal = getToolByName(context, 'portal_url').getPortalObject()
    return CustomQueryWalker(portal, migrator, use_savepoint=False)

class OldTypeToNewTypeMigrator(InplaceATItemMigrator):
    src_portal_type = 'OldType'
    src_meta_type = 'OldType'
    dst_portal_type = 'NewType'
    dst_meta_type = 'NewType'

    def last_migrate_reindex(self):
        self.new.reindexObject(idxs=['object_provides', 'portal_type',
            'Type', 'UID'])

    def renameOld(self):
        self.code_field = self.old.getCode_field()

    def remove(self):
        if self.new.getId() != self.code_field:
            unrestricted_rename(self.new.aq_inner.aq_parent, self.new.getId(), self.code_field)

    fields_map = {
        'datasource': 'data_src',
        'old_field': 'new_field',

def getOldTypeToNewTypeMigrationWalker(self):
    return getMigrationWalker(self, migrator=OldTypeToNewTypeMigrator)

def migrateOldType(self):
    walker = getOldTypeToNewTypeMigrationWalker(self)
    return walker.getOutput()

class ImagesToPhotosMigrator(InplaceATItemMigrator):
    src_portal_type = 'Image'
    src_meta_type = 'ATBlob'
    dst_portal_type = 'Photo'
    dst_meta_type = 'Photo'

    def last_migrate_reindex(self):

        self.new.reindexObject(idxs=['object_provides', 'portal_type',
            'Type', 'UID'])

    fields_map = {

def getImagesToPhotosMigrationWalker(self):
    return getMigrationWalker(self, migrator=ImagesToPhotosMigrator)

def migrateImages(self):
    walker = getImagesToPhotosMigrationWalker(self, {'path': '/my_folder'})
    return walker.getOutput()

Then, go ZMI and add an External Method in Plone Site root:

Id: myproj.atcontent.migration
Module Name: myproj.atcontent.migration
Function Name: migrateOldType

Click Test tab to run the External Method. If everything goes well, you might want go portal_catalog Advanced tab to Update Catalog.

No comments: