From 9641c1eaf9ed30998c660fc7b2340ee07e25d27b Mon Sep 17 00:00:00 2001
From: Ivan Tyagov <ivan@nexedi.com>
Date: Mon, 15 Oct 2012 09:45:29 +0200
Subject: [PATCH] Add generic code to check if a document is convertible in one
 place : Base_isConvertible. Make sure convert is not called in case resource
 is not convertible.

---
 .../SQLCatalog_preConvertDocumentList.xml     |  5 +-
 .../Base_callPreConvert.xml                   |  7 +-
 .../Base_isConvertible.xml                    | 82 +++++++++++++++++++
 bt5/erp5_dms_conversion_catalog/bt/revision   |  2 +-
 4 files changed, 86 insertions(+), 10 deletions(-)
 create mode 100644 bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_isConvertible.xml

diff --git a/bt5/erp5_dms_conversion_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/SQLCatalog_preConvertDocumentList.xml b/bt5/erp5_dms_conversion_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/SQLCatalog_preConvertDocumentList.xml
index 2b33ca5bbd..fc8b4753b9 100644
--- a/bt5/erp5_dms_conversion_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/SQLCatalog_preConvertDocumentList.xml
+++ b/bt5/erp5_dms_conversion_catalog/CatalogMethodTemplateItem/portal_catalog/erp5_mysql_innodb/SQLCatalog_preConvertDocumentList.xml
@@ -62,10 +62,7 @@ if address not in MARKER and port not in MARKER:\n
   for index_uid in range(len(uid)):\n
     document_relative_url = getRelativeUrl[index_uid]\n
     document = portal.restrictedTraverse(document_relative_url)\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 document.Base_isConvertible():\n
       document.activate(priority=4, tag="conversion").Base_callPreConvert()\n
 </string> </value>
         </item>
diff --git a/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_callPreConvert.xml b/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_callPreConvert.xml
index b71aaeee74..754346f5ff 100644
--- a/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_callPreConvert.xml
+++ b/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_callPreConvert.xml
@@ -56,11 +56,8 @@
 """\n
 portal = context.getPortalObject()\n
 \n
-portal_type = context.getPortalType()\n
-allowed_portal_type_list = portal.getPortalDocumentTypeList() + portal.getPortalEmbeddedDocumentTypeList()\n
-\n
-if portal_type not in allowed_portal_type_list:\n
-  # no need to convert any non DMS types\n
+if not context.Base_isConvertible():\n
+  # no need to convert any non convertible types\n
   return\n
 \n
 if kw=={}:\n
diff --git a/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_isConvertible.xml b/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_isConvertible.xml
new file mode 100644
index 0000000000..5cf39a10bc
--- /dev/null
+++ b/bt5/erp5_dms_conversion_catalog/SkinTemplateItem/portal_skins/erp5_dms_conversion_catalog/Base_isConvertible.xml
@@ -0,0 +1,82 @@
+<?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>"""\n
+  Return true or false based on if document is convertible or not.\n
+"""\n
+MARKER = (None, \'\',)\n
+portal = context.getPortalObject()\n
+\n
+portal_type = context.getPortalType()\n
+allowed_portal_type_list = portal.getPortalDocumentTypeList() + portal.getPortalEmbeddedDocumentTypeList()\n
+\n
+if portal_type not in allowed_portal_type_list:\n
+  return False\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
+return (getattr(context, "getData", None) is not None and context.getData() not in MARKER) or \\\n
+       (getattr(context, "getBaseData", None) is not None and context.getBaseData() not in MARKER)\n
+</string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>Base_isConvertible</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_dms_conversion_catalog/bt/revision b/bt5/erp5_dms_conversion_catalog/bt/revision
index 2edeafb09d..b5045cc404 100644
--- a/bt5/erp5_dms_conversion_catalog/bt/revision
+++ b/bt5/erp5_dms_conversion_catalog/bt/revision
@@ -1 +1 @@
-20
\ No newline at end of file
+21
\ No newline at end of file
-- 
2.30.9