Commit 8ce12b78 authored by Ivan Tyagov's avatar Ivan Tyagov

Allow pre conversion to happen on any ERP5 Web Page's content which include...

Allow pre conversion to happen on any ERP5 Web Page's content  which include relative images (through <img> tags).
parent 9218a3d6
import re
def Base_extractImageUrlList(self, text_content=None):
"""
Extract list of image URLS used in a Text document (i.e. Web Page)
"""
if text_content is None:
text_content = self.getTextContent()
if text_content is not None:
return re.findall('src=[\"\'](.[^\"\']+)[\"\']', text_content, re.I)
return []
...@@ -63,19 +63,21 @@ if portal_type not in allowed_portal_type_list:\n ...@@ -63,19 +63,21 @@ if portal_type not in allowed_portal_type_list:\n
# no need to convert any non DMS types\n # no need to convert any non DMS types\n
return\n return\n
\n \n
format = portal.portal_preferences.getPreferredImageFormat()\n if kw=={}:\n
quality = portal.portal_preferences.getPreferredImageQuality()\n # use default set of system preferences\n
preferred_image_size = portal.portal_preferences.getPreferredImageSize()\n format = portal.portal_preferences.getPreferredImageFormat()\n
quality = portal.portal_preferences.getPreferredImageQuality()\n
# thumbnail is required always\n
display_list = ["thumbnail"]\n
kw = dict(format=format, quality=quality, display_list = display_list)\n
\n \n
# thumbnail is required always\n
display_list = ["thumbnail"]\n
method = context.getTypeBasedMethod(\'preConvert\')\n method = context.getTypeBasedMethod(\'preConvert\')\n
return method(**dict(format=format, quality=quality, display_list = display_list))\n return method(**kw)\n
</string> </value> </string> </value>
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string></string> </value> <value> <string>**kw</string> </value>
</item> </item>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_function</string> </key>
<value> <string>Base_extractImageUrlList</string> </value>
</item>
<item>
<key> <string>_module</string> </key>
<value> <string>DocumentConversion</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_extractImageUrlList</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -53,6 +53,11 @@ ...@@ -53,6 +53,11 @@
<value> <string>"""\n <value> <string>"""\n
Do actual conversion of any Image type.\n Do actual conversion of any Image type.\n
"""\n """\n
if quality is None:\n
# it\'s required so fall back to system preferences as\n
# directly accessed over URL will do the same\n
quality = context.getDefaultImageQuality(format)\n
\n
# UI uses \'large\' display\n # UI uses \'large\' display\n
display_list.append(\'large\')\n display_list.append(\'large\')\n
context.Base_preConvert(format, quality, display_list)\n context.Base_preConvert(format, quality, display_list)\n
...@@ -60,7 +65,7 @@ context.Base_preConvert(format, quality, display_list)\n ...@@ -60,7 +65,7 @@ context.Base_preConvert(format, quality, display_list)\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>format, quality, display_list</string> </value> <value> <string>format, quality=None, display_list=[]</string> </value>
</item> </item>
<item> <item>
<key> <string>_proxy_roles</string> </key> <key> <string>_proxy_roles</string> </key>
......
...@@ -53,7 +53,12 @@ ...@@ -53,7 +53,12 @@
<value> <string>"""\n <value> <string>"""\n
Do actual conversion of OOo types.\n Do actual conversion of OOo types.\n
"""\n """\n
portal = context.getPortalObject()\n
\n
if context.hasBaseData():\n if context.hasBaseData():\n
if quality is None:\n
quality = portal.portal_preferences.getPreferredImageQuality()\n
\n
# empty documents do not need pre conversion\n # empty documents do not need pre conversion\n
context.activate(serialization_tag=\'pre_convert\').convert(**{\'format\': \'html\'})\n context.activate(serialization_tag=\'pre_convert\').convert(**{\'format\': \'html\'})\n
context.activate(serialization_tag=\'pre_convert\').Base_preConvert(format, quality, display_list)\n context.activate(serialization_tag=\'pre_convert\').Base_preConvert(format, quality, display_list)\n
...@@ -61,7 +66,7 @@ if context.hasBaseData():\n ...@@ -61,7 +66,7 @@ if context.hasBaseData():\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>format, quality, display_list</string> </value> <value> <string>format, quality=None, display_list=[]</string> </value>
</item> </item>
<item> <item>
<key> <string>_proxy_roles</string> </key> <key> <string>_proxy_roles</string> </key>
......
...@@ -50,7 +50,9 @@ ...@@ -50,7 +50,9 @@
</item> </item>
<item> <item>
<key> <string>_body</string> </key> <key> <string>_body</string> </key>
<value> <string>"""\n <value> <string encoding="cdata"><![CDATA[
"""\n
Do actual conversion of Web Page. As it involves cloudoo use serialization tag and activity.\n Do actual conversion of Web Page. As it involves cloudoo use serialization tag and activity.\n
"""\n """\n
portal = context.getPortalObject()\n portal = context.getPortalObject()\n
...@@ -60,7 +62,12 @@ format_kw = {\'format\': format,\n ...@@ -60,7 +62,12 @@ format_kw = {\'format\': format,\n
for display in display_list:\n for display in display_list:\n
format_kw[\'display\'] = display\n format_kw[\'display\'] = display\n
context.activate(serialization_tag=\'pre_convert\').convert(**format_kw)\n context.activate(serialization_tag=\'pre_convert\').convert(**format_kw)\n
</string> </value> \n
# try to convert all relative referenced (i.e. by <img> tag) documents\n
context.activate().WebPage_preConvertReferencedImageList(**format_kw)\n
]]></string> </value>
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
......
<?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>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[
"""\n
Get all images inside a document and try to pre convert relative ones\n
"""\n
portal = context.getPortalObject()\n
\n
MARKER = (None, \'\',)\n
API_ARGUMENT_LIST = [\'format\', \'display\', \'quality\', \'resolution\']\n
validation_state = (\'released\', \'released_alive\', \'published\', \'published_alive\',\n
\'shared\', \'shared_alive\', \'public\', \'validated\')\n
\n
def convertUrlArgumentsToDict(convert_string):\n
convert_kw = {}\n
# some editors when creating wbe page content do escape \'&\' properly\n
convert_string = convert_string.replace(\'&amp;\', \'&\')\n
\n
# convert from URL string to python arguments dict\n
for pair in convert_string.split(\'&\'):\n
arg_list = pair.split(\'=\')\n
if len(arg_list)==2:\n
convert_kw[arg_list[0]] = arg_list[1]\n
return convert_kw\n
\n
image_url_list = context.Base_extractImageUrlList()\n
for image_url in image_url_list:\n
if not image_url.startswith(\'http://\') and not image_url.startswith(\'https://\'):\n
# try to use only relative URLs\n
part_list = image_url.split(\'?\')\n
if len(part_list)==2:\n
# don\'t deal with bad URLs (having more than one \'?\') inside\n
reference = part_list[0]\n
convert_string = part_list[1]\n
\n
# check we have locally such a reference so we can convert it\n
kw = {\'portal_type\': portal.getPortalDocumentTypeList() + portal.getPortalEmbeddedDocumentTypeList(),\n
\'reference\': reference,\n
\'validation_state\': validation_state}\n
\n
document = portal.portal_catalog.getResultValue(**kw)\n
if document is not None:\n
# try to pre convert it based on extracted URL\'s arguments\n
convert_kw = convertUrlArgumentsToDict(convert_string)\n
\n
# XXX: we do check if "data" methods exists on pretending to be Document portal types\n
# we need a way to do this by introspection\n
if ((getattr(document, "getData", None) is not None and document.getData() not in MARKER) or \\\n
(getattr(document, "getBaseData", None) is not None and document.getBaseData() not in MARKER)):\n
if \'display\' in convert_kw.keys():\n
# conversion script aggregate all possible display options into a list\n
convert_kw[\'display_list\'] = [convert_kw.pop(\'display\')]\n
\n
# only certain arguments make sense due to API so leave only them\n
for key in convert_kw.keys():\n
if key not in API_ARGUMENT_LIST:\n
convert_kw.pop(key)\n
\n
# do real conversion\n
format = convert_kw.get(\'format\')\n
if format not in MARKER:\n
# format is mandatory if it\'s missing then anyway URL request will fail so\n
# don\'t bother create an activity\n
document.activate(priority=4, tag="conversion").Base_callPreConvert(**convert_kw)\n
]]></string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>**kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>WebPage_preConvertReferencedImageList</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
18 19
\ No newline at end of file \ No newline at end of file
DocumentConversion
\ No newline at end of file
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