Convertir PloneArticle en Richdocument



Supposons que :

voici le script convert_to_richdocument.py de conversion de tous les PloneArticle 4.2 en RichDocument 3.4 ::

import transaction from copy import deepcopy from Acquisition import aq_parent from Testing.makerequest import makerequest from AccessControl.SecurityManagement import newSecurityManager from zope.component.hooks import setSite

app=makerequest(app) portal = app.monsite setSite(portal)

admin = portal.getOwner() newSecurityManager(None, admin)

catalog = portal.portal_catalog
wtool = portal.portal_workflow

def generateUniqueId(type_name=None): \"\"\" Generate an id for the content This is not the archetype s uid. \"\"\" from DateTime import DateTime from random import random

now = DateTime()
time = "%s.%s" % (now.strftime("%Y-%m-%d ), str(now.millis())[7:])
rand = str(random())[2:6]
prefix = ""
suffix = ""

if type_name is not None:
    prefix = type_name.replace(" ", "_") + "."
prefix = prefix.lower()

return prefix + time + rand + suffix

def migrate(old): parent = aq_parent(old) newid = parent.invokeFactory( richDocument", generateUniqueId(), title=old.Title(), text=old.getText()) new = parent[newid] new.ac_local_roles = deepcopy(old.ac_local_roles) new._md = deepcopy(old._md) new.workflow_history = deepcopy(old.workflow_history)

workflows = wtool.getWorkflowsFor(new)
for workflow in workflows:
    workflow.updateRoleMappingsFor(new)
#new.reindexObjectSecurity()
for oldfile in old.getFiles():
    newfileid = new.invokeFactory("FileAttachment", oldfile.getId(),
                                  title=oldfile.Title(),
                                  description=oldfile.Description(),
                                  file=oldfile.getFile())

for oldimage in old.getImages():
    if oldimage.getContentType() ==  application/pdf":
        print \"pdf en image!!!!\"
        newfileid = new.invokeFactory("FileAttachment", oldimage.getId(),
                                  title=oldimage.Title(),
                                  description=oldimage.Description(),
                                  file=oldimage.getImage())
    else:
        newimageid = new.invokeFactory( imageAttachment", oldimage.getId(),
                                  title=oldimage.Title(),
                                  description=oldimage.Description(),
                                  image=oldimage.getImage())

new.creation_date = old.creation_date
new.modification_date = old.modification_date

# delete old doc
parent.manage_delObjects([old.getId()])
# rename new doc
new._renameAfterCreation()

new.__dict__["notifyModified ] = lambda *args: None
new.reindexObject()
del new.__dict__["notifyModified ]
transaction.commit()
return new.getId()

brains = catalog.unrestrictedSearchResults(portal_type="PloneArticle", sort_on="getObjPositionInParent")

print len(brains), "PloneArticle"

count = 0 for brain in brains[:]: count += 1 print count, brain.getURL() article = brain.getObject() print migrate(article)

Pour exécuter le script ::

cd Plone42/instance bin/instance run convert_to_richdocument.py

That s it


Publié le : 29-12-2014 - 18:00