Created
February 11, 2017 11:17
-
-
Save keul/88547c922943d7d46e7fa0c70c25b3a0 to your computer and use it in GitHub Desktop.
PloneHelpCenter to Native
This file contains hidden or 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 -*- | |
import transaction | |
from Products.CMFCore.interfaces import IContentish | |
print "Migrating PHC" | |
site = app.portale | |
phc = site.unrestrictedTraverse('qms/documentazione-sistema-integrato') | |
wtool = site.portal_workflow | |
new_id = site.qms.invokeFactory( | |
type_name='Folder', | |
id='new-documentazione-sistema-integrato', | |
title=phc.Title(), | |
description=phc.Description() | |
) | |
new_phc = getattr(site.qms, new_id) | |
wtool.doActionFor(new_phc, 'publish') | |
def get_config(original): | |
meta_type = original.meta_type | |
if meta_type in ( | |
'HelpCenterHowToFolder', | |
'HelpCenterTutorialFolder', | |
'HelpCenterTutorial', | |
'HelpCenterReferenceManualFolder', | |
): | |
print "...it will became a folder" | |
return { | |
'type_name': 'Folder' | |
} | |
if meta_type in ('ATImage', ): | |
return { | |
'type_name': 'Image', | |
'image': original.getImage(), | |
} | |
if meta_type in ('ATFile', 'ATBlob'): | |
return { | |
'type_name': 'File', | |
'file': original.getFile(), | |
} | |
if meta_type in ( | |
'HelpCenterHowTo', | |
'HelpCenterTutorialPage', | |
'HelpCenterLeafPage', | |
'HelpCenterReferenceManual', | |
): | |
# we don't have text but have a single file inside | |
# just migrate the file! | |
text = original.getText() if original.getField('text') else '' | |
text = text.strip() | |
# hacky way for choosing if a text field is empty | |
if text and len(text) < 15: | |
text = '' | |
sub_items = get_subobjects(original) | |
if text and sub_items: | |
# We have text and subitems: create a folder and a default page | |
return { | |
'type_name': 'Folder', | |
'subtype_name': 'Document', | |
'text': original.getRawText(), | |
} | |
# we have some text and not subitems... | |
# just a Document! | |
if text and not sub_items: | |
print "...it will became a simple document" | |
return { | |
'type_name': 'Document', | |
'text': original.getRawText(), | |
} | |
if not text\ | |
and len(sub_items) == 1: | |
if sub_items[0]['obj'].portal_type == 'File': | |
print "...it will became a simple file" | |
return { | |
'type_name': 'File', | |
'file': sub_items[0]['obj'].getFile(), | |
'prevent_subitems': True | |
} | |
if sub_items[0]['obj'].portal_type == 'Image': | |
print "...it will became a simple image" | |
return { | |
'type_name': 'Image', | |
'image': sub_items[0]['obj'].getImage(), | |
'prevent_subitems': True | |
} | |
# A blank but not empty PHC rich content | |
if not text and len(sub_items) > 1: | |
print "...it will became a simple folder with subitems" | |
return { | |
'type_name': 'Folder', | |
} | |
# emtpy item... let's skip this | |
if not text and not sub_items: | |
return None | |
import pdb; pdb.set_trace() | |
raise ValueError('Unknow type: %s' % meta_type) | |
def get_subobjects(container): | |
contents = container.objectValues() | |
if contents: | |
return [ | |
{ | |
'meta_type': x.meta_type, | |
'id': x.id, | |
'obj': getattr(container, x.getId()), | |
} for x in contents if IContentish.providedBy(x) | |
] | |
contents = getattr(container, '_objects', []) | |
return [ | |
{ | |
'meta_type': x['meta_type'], | |
'id': x['id'], | |
'obj': getattr(container, x['id']), | |
} for x in contents | |
] | |
def clone_content(original, new_parent): | |
print "Cloning %s" % original.absolute_url_path() | |
clone_data = get_config(original) | |
if not clone_data: | |
print "...empty item! Skipped" | |
return | |
new_id = new_parent.invokeFactory( | |
type_name=clone_data['type_name'], | |
id=original.id, | |
title=original.Title(), | |
description=original.Description(), | |
creators=original.Creators() | |
) | |
new_content = getattr(new_parent, new_id) | |
# Need an index page | |
if clone_data.get('subtype_name'): | |
print "...creating index page for %s" % original.absolute_url_path() | |
new_content.invokeFactory( | |
type_name=clone_data['subtype_name'], | |
id='front-page', | |
title=original.Title(), | |
description=original.Description(), | |
creators=original.Creators(), | |
text=clone_data['text'], | |
) | |
wtool.doActionFor(new_content['front-page'], 'publish') | |
new_content.setDefaultPage('front-page') | |
# is an image | |
if clone_data.get('image'): | |
new_content.edit(image=clone_data['image']) | |
# is a file | |
if clone_data.get('file'): | |
new_content.edit(file=clone_data['file']) | |
# has some text | |
if clone_data.get('text'): | |
new_content.edit(text=clone_data['text']) | |
transition = clone_data.get('transition', 'publish') | |
if transition: | |
wtool.doActionFor(new_content, transition) | |
if original.isPrincipiaFolderish and not clone_data.get('prevent_subitems'): | |
sub_objects = get_subobjects(original) | |
for subo in sub_objects: | |
clone_content( | |
subo['obj'], | |
new_parent=new_content | |
) | |
for macro_section in get_subobjects(phc): | |
#if macro_section['id'] in ('how-to', 'istruzioni'): | |
# continue | |
meta_type = macro_section['meta_type'] | |
id = macro_section['id'] | |
sub_section = macro_section['obj'] | |
clone_content(sub_section, new_parent=new_phc) | |
print "Section %s completed" % sub_section.Title() | |
transaction.savepoint(optimistic=True ) | |
print "Done" | |
transaction.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment