From c5f8edc817204bb76b900578d4587b65f29744bc Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Fri, 24 Feb 2012 21:45:05 +0900
Subject: [PATCH] Fix import of a file for DocumentTemplateItem for filesystem
 Document and Extension.

Also, set _archive properly for ZODB Document and Extension.
---
 product/ERP5/Document/BusinessTemplate.py | 25 ++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 00ed09a180..b6afa8c229 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -3798,17 +3798,28 @@ class DocumentTemplateItem(FilesystemToZodbTemplateItem):
                         "%s.py" % class_id)
 
   def _importFile(self, file_name, file_obj):
-    if (file_name.endswith('.py') and
-        file_obj.name.rsplit(os.path.sep, 2)[-2] == 'portal_components'):
-      return
-
-    FilesystemToZodbTemplateItem._importFile(self, file_name, file_obj)
+    if file_name.endswith('.py'):
+      # If portal_components/XXX.py, then ignore it as it will be handled when
+      # the .xml file will be processed
+      if file_obj.name.rsplit(os.path.sep, 2)[-2] != 'portal_components':
+        return FilesystemDocumentTemplateItem._importFile(self, file_name,
+                                                          file_obj)
+    else:
+      ObjectTemplateItem._importFile(self, file_name, file_obj)
 
-    if file_name.endswith('.xml'):
-      obj = self._objects[file_name[:-4]]
+      name = file_name[:-4]
+      obj = self._objects[name]
       with open("%s.py" % file_obj.name[:-4]) as f:
         obj.text_content = f.read()
 
+      # When importing a Business Template, there is no way to determine if it
+      # has been already migrated or not in __init__() when it does not
+      # already exist, therefore BaseTemplateItem.__init__() is called which
+      # does not set _archive with portal_components/ like
+      # ObjectTemplateItem.__init__()
+      self._archive[name] = None
+      del self._archive[name.replace('portal_components/', '')]
+
   def export(self, context, bta, **kw):
     path = self.__class__.__name__ + '/'
     for key, obj in self._objects.iteritems():
-- 
2.30.9