Commit b57ecc2a authored by Jérome Perrin's avatar Jérome Perrin

substitution

parent e74ad5ea
......@@ -59,8 +59,8 @@
<key> <string>group_list</string> </key>
<value>
<list>
<string>left (Page Properties)</string>
<string>right (Publication)</string>
<string>left</string>
<string>right</string>
<string>center</string>
<string>bottom</string>
</list>
......@@ -85,18 +85,20 @@
</value>
</item>
<item>
<key> <string>left (Page Properties)</string> </key>
<key> <string>left</string> </key>
<value>
<list>
<string>my_content_type</string>
<string>my_text_content_substitution_mapping_method_id</string>
</list>
</value>
</item>
<item>
<key> <string>right (Publication)</string> </key>
<key> <string>right</string> </key>
<value>
<list/>
<list>
<string>my_text_content_substitution_mapping_method_id</string>
<string>my_text_content_substitution_mapping_ignore_missing</string>
</list>
</value>
</item>
</dictionary>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>delegated_list</string> </key>
<value>
<list>
<string>title</string>
</list>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>my_text_content_substitution_mapping_ignore_missing</string> </value>
</item>
<item>
<key> <string>message_values</string> </key>
<value>
<dictionary>
<item>
<key> <string>external_validator_failed</string> </key>
<value> <string>The input failed the external validator.</string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>overrides</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>tales</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>values</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string>my_checkbox</string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string>Base_viewFieldLibrary</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Ignore Missing Substitution Variables</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -152,8 +152,9 @@ class TestNotificationMessageModule(ERP5TypeTestCase):
self.assertEqual('substitution text: b', text.rstrip())
def test_safe_substitution_content(self):
"""Tests that 'safe' substitution is performed, unless safe_substitute is
explicitly passed to False.
"""Tests that 'safe' substitution is performed, unless the notification
message is configured text_content_substitution_mapping_ignore_missing set
to false, or safe_substitute is passed as False.
"""
module = self.portal.notification_message_module
createZODBPythonScript(self.portal,
......@@ -175,6 +176,14 @@ class TestNotificationMessageModule(ERP5TypeTestCase):
self.assertRaises(KeyError, doc.convert, 'html', safe_substitute=False)
self.assertRaises(KeyError, doc.asSubjectText, safe_substitute=False)
doc.setTextContentSubstitutionMappingIgnoreMissing(False)
self.assertRaises(KeyError, doc.convert, 'txt')
self.assertRaises(KeyError, doc.convert, 'html')
self.assertRaises(KeyError, doc.asSubjectText)
mime, text = doc.convert('txt', safe_substitute=True)
self.assertEqual('substitution text: ${b}', text.rstrip())
self.assertEqual('${b}', doc.asSubjectText(safe_substitute=True))
def test_substitution_lazy_dict(self):
"""Substitution script just needs to return an object implementing
__getitem__ protocol.
......
......@@ -20,12 +20,15 @@ if notification_message is not None:
target_format = "txt"
if context.getContentType() == 'text/html':
target_format = "html"
_, text_content = notification_message.convert(target_format,
substitution_method_parameter_dict=substitution_method_parameter_dict)
_, text_content = notification_message.convert(
target_format,
substitution_method_parameter_dict=substitution_method_parameter_dict,
safe_substitute=notification_message.isTextContentSubstitutionMappingIgnoreMissing())
context.setTextContent(text_content)
context.setAggregateSet(
context.getAggregateList() + notification_message.getProperty('aggregate_list', []))
if not context.hasTitle():
context.setTitle(notification_message.asSubjectText(
substitution_method_parameter_dict=substitution_method_parameter_dict))
substitution_method_parameter_dict=substitution_method_parameter_dict,
safe_substitute=notification_message.isTextContentSubstitutionMappingIgnoreMissing()))
......@@ -75,7 +75,7 @@ class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin, TextContent
, PropertySheet.Reference
)
def _substituteTextContent(self, text, safe_substitute=True, **kw):
def _substituteTextContent(self, text, safe_substitute=_MARKER, **kw):
# 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.
......@@ -104,6 +104,8 @@ class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin, TextContent
return v
unicode_mapping = UnicodeMapping()
if safe_substitute is _MARKER:
safe_substitute = self.isTextContentSubstitutionMappingIgnoreMissing()
if safe_substitute:
text = Template(text).safe_substitute(unicode_mapping)
else:
......@@ -116,7 +118,7 @@ class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin, TextContent
return text
security.declareProtected(Permissions.AccessContentsInformation, 'asSubjectText')
def asSubjectText(self, substitution_method_parameter_dict=None, safe_substitute=True, **kw):
def asSubjectText(self, substitution_method_parameter_dict=None, safe_substitute=_MARKER, **kw):
"""
Converts the subject of the document to a textual representation.
"""
......@@ -127,7 +129,7 @@ class TextDocument(CachedConvertableMixin, BaseConvertableFileMixin, TextContent
**substitution_method_parameter_dict)
def _convert(self, format, substitution_method_parameter_dict=None, # pylint: disable=redefined-builtin
safe_substitute=True, charset=None, text_content=None, substitute=True, **kw):
safe_substitute=_MARKER, charset=None, text_content=None, substitute=True, **kw):
"""
Convert text using portal_transforms or oood
"""
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Standard Property" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_local_properties</string> </key>
<value>
<tuple>
<dictionary>
<item>
<key> <string>id</string> </key>
<value> <string>mode</string> </value>
</item>
<item>
<key> <string>type</string> </key>
<value> <string>string</string> </value>
</item>
</dictionary>
</tuple>
</value>
</item>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>elementary_type/boolean</string>
</tuple>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Ignore missing entries from substitution mapping.\n
\n
When ignoring, missing entries will be kept as is, otherwise substitution will be an error.</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>text_content_substitution_mapping_ignore_missing_property</string> </value>
</item>
<item>
<key> <string>mode</string> </key>
<value> <string>w</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Standard Property</string> </value>
</item>
<item>
<key> <string>property_default</string> </key>
<value> <string>python: True</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
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