Commit 99f0744c authored by Tristan Cavelier's avatar Tristan Cavelier

erp5_base: add Convert To scripts

parent f27c534f
portal = context.getPortalObject()
content_type = context.getContentType()
def splitext(unixpath):
slashsplit = unixpath.split("/")
dotsplit = slashsplit[-1].split(".")
reqlen = 3 if dotsplit[0] == '' else 2
if len(dotsplit) < reqlen:
return (unixpath, '')
return ('/'.join(slashsplit[:-1] + ['.'.join(dotsplit[:-1])]), '.' + dotsplit[-1])
def getTargetFormatItemList(content_type):
# returns [(title, mimetype), ...]
if content_type is None:
return []
format_list = []
available_mimetype_list = portal.portal_transforms.getAvailableTargetMimetypeList(content_type)
mimetype_registry = portal.mimetypes_registry
for available_mimetype in available_mimetype_list:
mimetype_info_list = mimetype_registry.lookup(available_mimetype)
for mimetype_info in mimetype_info_list:
format_list.append((mimetype_info.name(), available_mimetype))
return format_list
def getExtension(content_type, default=None):
for info in portal.mimetypes_registry.lookup(content_type):
try:
return info.extensions[0]
except IndexError:
pass
return default
if __list:
if content_type in ("", None):
return []
#return getTargetFormatItemList(content_type)
#return [("%s (%s)" %(t, mt), mt) for t, mt in getTargetFormatItemList(content_type)]
return [("%s (%s)" %(t, getExtension(mt, "-")), mt) for t, mt in getTargetFormatItemList(content_type)]
destination_portal_type = portal.portal_contribution_registry.findPortalTypeName(content_type=destination_mimetype)
module_id = portal.getDefaultModuleId(destination_portal_type, default=None, only_visible=True)
extension = getExtension(destination_mimetype, "bin")
data = portal.portal_transforms.convertToData(destination_mimetype, str(context.getData() or ""), context=context, mimetype=content_type)
if data is None:
raise ValueError("Failed to convert to %r" % destination_mimetype)
document = portal[module_id].newContent(
portal_type=destination_portal_type,
title=context.getTitle(),
short_title=context.getShortTitle(),
reference=context.getReference(),
version=version,
language=context.getLanguage(),
effective_date=context.getEffectiveDate(),
filename=splitext(context.getFilename('untitled'))[0] + "." + extension,
content_type=destination_mimetype,
data=data,
category_list=context.getCategoryList(),
description=context.getDescription(),
)
if REQUEST is None:
return document
return document.Base_redirect(keep_items={"portal_status_message": context.Base_translateString("Converted successfully")})
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>destination_mimetype=\'\', version=None, __list=False, REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_convertToNewContent</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ERP5 Form" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>action</string> </key>
<value> <string>Base_convertToNewContent</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>edit_order</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>encoding</string> </key>
<value> <string>UTF-8</string> </value>
</item>
<item>
<key> <string>enctype</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>group_list</string> </key>
<value>
<list>
<string>left</string>
<string>right</string>
<string>center</string>
<string>bottom</string>
<string>hidden</string>
</list>
</value>
</item>
<item>
<key> <string>groups</string> </key>
<value>
<dictionary>
<item>
<key> <string>bottom</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>center</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>hidden</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>left</string> </key>
<value>
<list>
<string>your_destination_mimetype</string>
<string>your_version</string>
</list>
</value>
</item>
<item>
<key> <string>right</string> </key>
<value>
<list/>
</value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_viewConvertToNewContentDialog</string> </value>
</item>
<item>
<key> <string>method</string> </key>
<value> <string>POST</string> </value>
</item>
<item>
<key> <string>name</string> </key>
<value> <string>Base_viewConvertToNewContentDialog</string> </value>
</item>
<item>
<key> <string>pt</string> </key>
<value> <string>form_dialog</string> </value>
</item>
<item>
<key> <string>row_length</string> </key>
<value> <int>4</int> </value>
</item>
<item>
<key> <string>stored_encoding</string> </key>
<value> <string>UTF-8</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Convert Document</string> </value>
</item>
<item>
<key> <string>unicode_mode</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>update_action</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>update_action_title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment