Skip to content

Instantly share code, notes, and snippets.

@ericof
Created December 12, 2012 19:39
Show Gist options
  • Save ericof/4270880 to your computer and use it in GitHub Desktop.
Save ericof/4270880 to your computer and use it in GitHub Desktop.
Plone's in-place conversion of File (ATBlob) to Image (ATBlob)
# -*- 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()
@giacomos
Copy link

import pdb;pdb.post_morten() -> import pdb;pdb.post_mortem()

finally I realize that 5 years of studying Latin weren't completely a waste :)

@batlock666
Copy link

Code doesn't work for me. I still get an exception about setting another primary field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment