Since it's a bit disappointing there is no sample code to be found for this problem, I have written some.
Here it is:
plone=app.Plone
from zope.component.hooks import setSite
setSite(plone)
from plone.app.contenttypes.migration.migration import migrateCustomAT
def custom_document_migration():
fields_mapping = (
{'AT_field_name': 'text',
'DX_field_name': 'text',
'DX_field_type': 'RichText',
},
{'AT_field_name': 'flaggedobject',
'DX_field_name': 'flaggedobject',
},
)
r = migrateCustomAT(
fields_mapping,
src_type='Document',
dst_type='Document')
return r
def custom_folder_migration():
fields_mapping = (
{'AT_field_name': 'flaggedobject',
'DX_field_name': 'flaggedobject',
},
)
r = migrateCustomAT(
fields_mapping,
src_type='Folder',
dst_type='Folder')
return r
def custom_file_migration():
fields_mapping = (
{'AT_field_name': 'file',
'DX_field_name': 'file',
'DX_field_type': 'NamedBlobFile',
},
{'AT_field_name': 'flaggedobject',
'DX_field_name': 'flaggedobject',
},
)
r = migrateCustomAT(
fields_mapping,
src_type='File',
dst_type='File')
return r
def custom_image_migration():
fields_mapping = (
{'AT_field_name': 'image',
'DX_field_name': 'image',
'DX_field_type': 'NamedBlobImage',
},
{'AT_field_name': 'flaggedobject',
'DX_field_name': 'flaggedobject',
},
)
r = migrateCustomAT(
fields_mapping,
src_type='Image',
dst_type='Image')
return r
def custom_news_migration():
fields_mapping = (
{'AT_field_name': 'text',
'DX_field_name': 'text',
'DX_field_type': 'RichText',
},
{'AT_field_name': 'image',
'DX_field_name': 'image',
'DX_field_type': 'NamedBlobImage',
},
{'AT_field_name': 'imageCaption',
'DX_field_name': 'imageCaption',
},
{'AT_field_name': 'flaggedobject',
'DX_field_name': 'flaggedobject',
},
)
r = migrateCustomAT(
fields_mapping,
src_type='News Item',
dst_type='News Item')
return r
import transaction
with api.env.adopt_user(username="admin"):
custom_document_migration()
transaction.commit()
with api.env.adopt_user(username="admin"):
custom_folder_migration()
transaction.commit()
with api.env.adopt_user(username="admin"):
custom_file_migration()
transaction.commit()
with api.env.adopt_user(username="admin"):
custom_image_migration()
transaction.commit()
Caveat:
- there is a 'dry_run' parameter to the migrateCustomAT routine do NOT try to use it it does not work
- custom fields (here 'flaggedobject') are not created automatically - I used the Dexterity TTW editor :-/ before running the code
- committing between each migration is needed else duplicated id errors
- I don't know why there are catalog error messages while committing folder handling, maybe these messages are harmless, maybe not.
- I migrated collections with @@atct_migrator (collections are a bit special and simple code such as this will not work)