From a42fdc6e427c571935924e2f72c082404f624d1f Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Mon, 8 Nov 2010 19:16:10 +0000
Subject: [PATCH] If possible, do not migrate XML to <global name="..."
 module="erp5.portal_type"/>

This preserves forward compatibility for some BT that are still developped with
instances not using portal types as classes.

Also remove TemplateTool_filterPortalTypeClassDiff, which is unreliable
(because it may break legitime migration of XML) and now useless.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@40073 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 ...TemplateTool_filterPortalTypeClassDiff.xml | 179 ------------------
 product/ERP5/bootstrap/erp5_core/bt/revision  |   2 +-
 product/ERP5Type/patches/XMLExportImport.py   |  16 +-
 3 files changed, 16 insertions(+), 181 deletions(-)
 delete mode 100644 product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_filterPortalTypeClassDiff.xml

diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_filterPortalTypeClassDiff.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_filterPortalTypeClassDiff.xml
deleted file mode 100644
index ca72ddeca4..0000000000
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/TemplateTool_filterPortalTypeClassDiff.xml
+++ /dev/null
@@ -1,179 +0,0 @@
-<?xml version="1.0"?>
-<ZopeData>
-  <record id="1" aka="AAAAAAAAAAE=">
-    <pickle>
-      <global name="Python Script" module="erp5.portal_type"/>
-    </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[
-
-if len(old_line_list) !=1 or len(new_line_list) != 1:\n
-  return False\n
-new_line = new_line_list[0]\n
-\n
-new_group_list = new_line.split(\'"\')\n
-if len(new_group_list) != 5:\n
-  return False\n
-\n
-before, portal_type, module, erp5_portal_type, after = new_group_list\n
-if before != \'<global name=\' or module != \' module=\' or erp5_portal_type != \'erp5.portal_type\' or after != \'/>\':\n
-  return False\n
-\n
-old_group_list = old_line_list[0].split(\'"\')\n
-if len(old_group_list) == 5:\n
-  before2, ignore, module2, products_erp5type, after2 = old_group_list\n
-  return before2 == before and module2 == module and products_erp5type.startswith("Products.ERP5Type.Document.") and after2 == after\n
-return False\n
-
-
-]]></string> </value>
-        </item>
-        <item>
-            <key> <string>_code</string> </key>
-            <value>
-              <none/>
-            </value>
-        </item>
-        <item>
-            <key> <string>_params</string> </key>
-            <value> <string>old_line_list, new_line_list</string> </value>
-        </item>
-        <item>
-            <key> <string>categories</string> </key>
-            <value>
-              <tuple>
-                <string>callable_type/script/diff_filter</string>
-              </tuple>
-            </value>
-        </item>
-        <item>
-            <key> <string>description</string> </key>
-            <value> <string encoding="cdata"><![CDATA[
-
-This script filter this kind of xml changes :\n
-- <global name="BaseCategory" module="Products.ERP5Type.Document.BaseCategory"/>\n
-+ <global name="Base Category" module="erp5.portal_type"/>
-
-]]></string> </value>
-        </item>
-        <item>
-            <key> <string>errors</string> </key>
-            <value>
-              <tuple/>
-            </value>
-        </item>
-        <item>
-            <key> <string>func_code</string> </key>
-            <value>
-              <object>
-                <klass>
-                  <global name="FuncCode" module="Shared.DC.Scripts.Signature"/>
-                </klass>
-                <tuple/>
-                <state>
-                  <dictionary>
-                    <item>
-                        <key> <string>co_argcount</string> </key>
-                        <value> <int>2</int> </value>
-                    </item>
-                    <item>
-                        <key> <string>co_varnames</string> </key>
-                        <value>
-                          <tuple>
-                            <string>old_line_list</string>
-                            <string>new_line_list</string>
-                            <string>len</string>
-                            <string>False</string>
-                            <string>_getitem_</string>
-                            <string>new_line</string>
-                            <string>_getattr_</string>
-                            <string>new_group_list</string>
-                            <string>_getiter_</string>
-                            <string>before</string>
-                            <string>portal_type</string>
-                            <string>module</string>
-                            <string>erp5_portal_type</string>
-                            <string>after</string>
-                            <string>old_group_list</string>
-                            <string>before2</string>
-                            <string>ignore</string>
-                            <string>module2</string>
-                            <string>products_erp5type</string>
-                            <string>after2</string>
-                          </tuple>
-                        </value>
-                    </item>
-                  </dictionary>
-                </state>
-              </object>
-            </value>
-        </item>
-        <item>
-            <key> <string>func_defaults</string> </key>
-            <value>
-              <none/>
-            </value>
-        </item>
-        <item>
-            <key> <string>id</string> </key>
-            <value> <string>TemplateTool_filterPortalTypeClassDiff</string> </value>
-        </item>
-        <item>
-            <key> <string>portal_type</string> </key>
-            <value> <string>Python Script</string> </value>
-        </item>
-        <item>
-            <key> <string>warnings</string> </key>
-            <value>
-              <tuple/>
-            </value>
-        </item>
-      </dictionary>
-    </pickle>
-  </record>
-</ZopeData>
diff --git a/product/ERP5/bootstrap/erp5_core/bt/revision b/product/ERP5/bootstrap/erp5_core/bt/revision
index a586f53540..43b5406c46 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/revision
+++ b/product/ERP5/bootstrap/erp5_core/bt/revision
@@ -1 +1 @@
-1780
\ No newline at end of file
+1781
\ No newline at end of file
diff --git a/product/ERP5Type/patches/XMLExportImport.py b/product/ERP5Type/patches/XMLExportImport.py
index 82c5bd9344..30eec4aff7 100644
--- a/product/ERP5Type/patches/XMLExportImport.py
+++ b/product/ERP5Type/patches/XMLExportImport.py
@@ -14,7 +14,7 @@
 
 # Make sure the xml export will be ordered
 
-import re
+import re, sys
 from ZODB.utils import u64, p64
 from Shared.DC.xml import ppml
 from base64 import encodestring
@@ -105,6 +105,20 @@ def cleanup_ZopePageTemplate(state):
 
 def cleanupState(classdef, state):
     classdef = getCleanClass(classdef)
+    # if possible, unmigrate document using portal_type as classes
+    # (in order to preserve forward compatibility)
+    if getattr(classdef, '__module__', None) == 'erp5.portal_type' and \
+       len(classdef.__bases__) == 1:
+      base, = classdef.__bases__
+      portal_type = isinstance(state, dict) and state.get('portal_type') or \
+                    getattr(base, 'portal_type', None)
+      if portal_type == classdef.__name__:
+        class_name = base.__name__
+        try:
+          module = sys.modules['Products.ERP5Type.Document.' + class_name]
+          classdef = getattr(module, class_name)
+        except KeyError:
+          classdef = base
     cleanupState = PICKLE_CLEANERS.get(classdef, lambda state: None)
     cleanupState(state)
     return classdef, state
-- 
2.30.9