Commit 5016f968 authored by Jérome Perrin's avatar Jérome Perrin

TextDocument: lazily convert substitution mapping to unicode

This way, the substitution mapping method can return a dynamic mapping,
the only requirement is to implement __getitem__

ne use case is to be able to use the same substitution script for
different CRM notification messages and only compute the "heavy"
substitution variables when they are actually needed.

See nexedi/erp5!1047
parent 7e775f3c
...@@ -97,13 +97,15 @@ class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin, TextContent ...@@ -97,13 +97,15 @@ class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin, TextContent
if is_str: if is_str:
text = text.decode('utf-8') text = text.decode('utf-8')
unicode_mapping = {} class UnicodeMapping:
for k, v in mapping.iteritems(): def __getitem__(self, item):
v = mapping[item]
if isinstance(v, str): if isinstance(v, str):
v = v.decode('utf-8') v = v.decode('utf-8')
elif not isinstance(v, unicode): elif not isinstance(v, unicode):
v = str(v).decode('utf-8') v = str(v).decode('utf-8')
unicode_mapping[k] = v return v
unicode_mapping = UnicodeMapping()
if safe_substitute: if safe_substitute:
text = Template(text).safe_substitute(unicode_mapping) text = Template(text).safe_substitute(unicode_mapping)
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
# #
############################################################################## ##############################################################################
import textwrap
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import createZODBPythonScript from Products.ERP5Type.tests.utils import createZODBPythonScript
...@@ -174,3 +174,28 @@ class TestNotificationMessageModule(ERP5TypeTestCase): ...@@ -174,3 +174,28 @@ class TestNotificationMessageModule(ERP5TypeTestCase):
self.assertRaises(KeyError, doc.convert, 'txt', safe_substitute=False) self.assertRaises(KeyError, doc.convert, 'txt', safe_substitute=False)
self.assertRaises(KeyError, doc.convert, 'html', safe_substitute=False) self.assertRaises(KeyError, doc.convert, 'html', safe_substitute=False)
self.assertRaises(KeyError, doc.asSubjectText, safe_substitute=False) self.assertRaises(KeyError, doc.asSubjectText, safe_substitute=False)
def test_substitution_lazy_dict(self):
"""Substitution script just needs to return an object implementing
__getitem__ protocol.
"""
module = self.portal.notification_message_module
createZODBPythonScript(
self.portal, 'NotificationMessage_getDummySubstitionMapping', '**kw',
textwrap.dedent(
'''\
class DynamicDict:
def __getitem__(self, key):
return "(dynamic key: %s)" % key
return DynamicDict()
'''))
doc = module.newContent(
portal_type='Notification Message',
content_type='text/plain',
text_content='substitution text: ${a}',
text_content_substitution_mapping_method_id='NotificationMessage_getDummySubstitionMapping'
)
mime, text = doc.convert('txt')
self.assertEqual('text/plain', mime)
self.assertEqual('substitution text: (dynamic key: a)', text)
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