From 1016ae0e8b75d3614853fd194a93224b0a0d87fb Mon Sep 17 00:00:00 2001
From: Yoshinori Okuji <yo@nexedi.com>
Date: Tue, 10 Jun 2008 02:18:53 +0000
Subject: [PATCH] Perform substitutions after the conversion, because
 substituions can be dynamic, thus should not be affected by caches for
 portal_transformations. (But do we really want to have portal_transformations
 to cache the results? When caches are implemented at higher levels, such as
 httpd or erp5 applications, they should be rather useless.)

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21443 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/TextDocument.py | 28 ++++++++++++++-------------
 product/ERP5/tests/testERP5Web.py     |  3 ---
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/product/ERP5/Document/TextDocument.py b/product/ERP5/Document/TextDocument.py
index f3a97fbc55..fc00d04446 100644
--- a/product/ERP5/Document/TextDocument.py
+++ b/product/ERP5/Document/TextDocument.py
@@ -168,6 +168,17 @@ class TextDocument(Document, TextContent):
       # check if document has set text_content and convert if necessary
       text_content = self.getTextContent()
       if text_content is not None:
+        portal_transforms = getToolByName(self, 'portal_transforms')
+        result = portal_transforms.convertToData(mime_type, text_content,
+                                                 object=self, context=self,
+                                                 filename=self.title_or_id(),
+                                                 mimetype=src_mimetype)
+        if result is None:
+            # portal_transforms fails to convert.
+            LOG('TextDocument.convert', WARNING,
+                'portal_transforms failed to convert to %s: %r' % (mime_type, self))
+            result = ''
+
         # If a method for string substitutions of the text content, perform it.
         # Decode everything into unicode before the substitutions, in order to
         # avoid encoding errors.
@@ -175,8 +186,8 @@ class TextDocument(Document, TextContent):
         if method_id:
           mapping = guarded_getattr(self, method_id)()
 
-          if isinstance(text_content, str):
-            text_content = text_content.decode('utf-8')
+          if isinstance(result, str):
+            result = result.decode('utf-8')
 
           unicode_mapping = {}
           for k, v in mapping.iteritems():
@@ -186,18 +197,9 @@ class TextDocument(Document, TextContent):
               v = str(v).decode('utf-8')
             unicode_mapping[k] = v
 
-          text_content = Template(text_content).substitute(unicode_mapping)
+          result = Template(result).substitute(unicode_mapping)
+          # XXX is it better to convert back to str?
 
-        portal_transforms = getToolByName(self, 'portal_transforms')
-        result = portal_transforms.convertToData(mime_type, text_content,
-                                                 object=self, context=self,
-                                                 filename=self.title_or_id(),
-                                                 mimetype=src_mimetype)
-        if result is None:
-            # portal_transforms fails to convert.
-            LOG('TextDocument.convert', WARNING,
-                'portal_transforms failed to convert to %s: %r' % (mime_type, self))
-            result = ''
         return mime_type, result
       else:
         # text_content is not set, return empty string instead of None
diff --git a/product/ERP5/tests/testERP5Web.py b/product/ERP5/tests/testERP5Web.py
index 3db9963f2b..acb43c83f9 100644
--- a/product/ERP5/tests/testERP5Web.py
+++ b/product/ERP5/tests/testERP5Web.py
@@ -389,9 +389,6 @@ class TestERP5Web(ERP5TypeTestCase, ZopeTestCase.Functional):
     document.setTextContentSubstitutionMappingMethodId('getTestSubstitutionMapping')
 
     # Substitutions should occur.
-    # XXX purge transformation cache.
-    if hasattr(document, '_v_transform_cache'):
-      delattr(document, '_v_transform_cache')
     self.assertEquals(document.asStrippedHTML(), substituted_content)
 
     klass._getTestSubstitutionMapping = klass.getTestSubstitutionMapping
-- 
2.30.9