public
Last active

Plone's in-place conversion of File (ATBlob) to Image (ATBlob)

  • Download Gist
convert.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
# -*- 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()

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

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.