Created
December 12, 2012 19:39
-
-
Save ericof/4270880 to your computer and use it in GitHub Desktop.
Plone's in-place conversion of File (ATBlob) to Image (ATBlob)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from plone.app.blob.interfaces import IATBlobFile | |
from plone.app.blob.migrations import ATFileToBlobMigrator | |
from plone.app.blob.migrations import migrate | |
from Products.contentmigration.walker import CustomQueryWalker | |
from Testing.makerequest import makerequest | |
from zope.component.hooks import setSite | |
from zope.interface import noLongerProvides | |
import transaction | |
app = makerequest(app) | |
site = app.ieausp | |
setSite(site) | |
class BlobFileToBlobImageMigrator(ATFileToBlobMigrator): | |
''' Migrate File (ATBlob) to Image (ATBlob) ''' | |
src_portal_type = 'File' | |
src_meta_type = 'ATBlob' | |
dst_portal_type = 'Image' | |
dst_meta_type = 'ATBlob' | |
# migrate all fields except 'file', which will be moved to 'image' | |
fields_map = {'file': None} | |
def migrate_data(self): | |
value = self.old.getField('file').getAccessor(self.old)() | |
self.new.getField('image').getMutator(self.new)(value) | |
def finalize(self): | |
ATFileToBlobMigrator.finalize(self) | |
# Remove IATBlobFile interface, otherwise schemaextender yells | |
# about primary field already set | |
noLongerProvides(self.new, IATBlobFile) | |
additionalQuery = {'path': '/Plone/images/'} | |
walker = CustomQueryWalker(site, BlobFileToBlobImageMigrator, | |
use_savepoint=False, | |
query=additionalQuery, | |
src_portal_type='File', | |
dst_portal_type='Image') | |
try: | |
migrate(site, walker()) | |
except: | |
# If something goes wrong, let's find out | |
import pdb;pdb.post_morten() | |
# Commit the whole migration | |
transaction.commit() | |
# Sync zeo | |
app._p_jar.sync() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code doesn't work for me. I still get an exception about setting another primary field.