Commit 1016ae0e authored by Yoshinori Okuji's avatar Yoshinori Okuji

Perform substitutions after the conversion, because substituions can be...

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
parent c749a284
......@@ -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
......
......@@ -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
......
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