Commit eaced69f authored by Alexandre Boeglin's avatar Alexandre Boeglin

Add proxification (delegation or surcharging) of error messages


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@19325 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 96873d65
......@@ -150,6 +150,7 @@ class ProxyField(ZMIField):
widget = ProxyWidgetInstance
validator = ProxyValidatorInstance
delegated_list = tuple()
delegated_message_list = tuple()
# methods screen
security.declareProtected('View management screens',
......@@ -161,6 +162,10 @@ class ProxyField(ZMIField):
'manage_talesForm')
manage_talesForm = DTMLFile('dtml/proxyFieldTales', globals())
# messages screen
security.declareProtected('View management screens', 'manage_messagesForm')
manage_messagesForm = DTMLFile('dtml/proxyFieldMessages', globals())
# proxy field list header
security.declareProtected('View management screens', 'proxyFieldListHeader')
proxyFieldListHeader = DTMLFile('dtml/proxyFieldListHeader', globals())
......@@ -352,6 +357,50 @@ class ProxyField(ZMIField):
if not self.values.has_key(key):
self.values[key] = self.get_recursive_orig_value(key, include=0)
security.declareProtected('Change Formulator Fields', 'manage_messages')
def manage_messages(self, REQUEST):
"""Change message texts.
"""
surcharge_list = []
messages = self.message_values
unicode_mode = self.get_unicode_mode()
for message_key in self.get_error_names():
checkbox_key = "surcharge_%s" % message_key
if not REQUEST.has_key(checkbox_key):
surcharge_list.append(message_key)
message = REQUEST[message_key]
if unicode_mode:
message = unicode(message, 'UTF-8')
messages[message_key] = message
else:
if message_key in messages:
messages.pop(message_key)
self.message_values = messages
self.delegated_message_list = surcharge_list
if REQUEST:
message="Content changed."
return self.manage_messagesForm(self,REQUEST,
manage_tabs_message=message)
security.declareProtected('View management screens', 'get_error_message')
def get_error_message(self, name):
if not self.is_message_delegated(name):
try:
return self.message_values[name]
except KeyError:
if name in self.validator.message_names:
return getattr(self.validator, name)
else:
return "Unknown error: %s" % name
else:
return self.getTemplateField().get_error_message(name)
security.declareProtected('View management screens', 'get_error_names')
def get_error_names(self):
"""Get error messages.
"""
return self.getTemplateField().get_error_names()
def getTemplateField(self):
"""
Return template field of the proxy field.
......@@ -408,6 +457,15 @@ class ProxyField(ZMIField):
"""
return id not in self.delegated_list
security.declareProtected('Access contents information',
'is_message_delegated')
def is_message_delegated(self, id):
"""
Return true if we get the message from the proxied field.
No, if we surcharged the message on the proxy field.
"""
return id not in self.delegated_message_list
security.declareProtected('Access contents information',
'get_recursive_orig_value')
def get_recursive_orig_value(self, id, include=1):
......@@ -450,20 +508,6 @@ class ProxyField(ZMIField):
"""
return self.overrides.get(id, "")
security.declareProtected('View management screens', 'get_error_message')
def get_error_message(self, name):
"""
"""
try:
return self.message_values[name]
except KeyError:
proxied_field = self.getTemplateField()
if proxied_field is not None:
return proxied_field.get_error_message(name)
else:
return ZMIField.get_error_message(self, name)
security.declareProtected('Edit target', 'manage_edit_target')
def manage_edit_target(self, REQUEST):
"""
......
<dtml-var manage_page_header>
<dtml-var manage_tabs>
<p class="form-help">
Edit <dtml-var meta_type> error messages here.
</p>
<form action="manage_messages" method="POST">
<table border="0">
<dtml-let proxy_field="this()">
<dtml-in "get_error_names()">
<dtml-let name=sequence-item value="get_error_message(name)"
checkbox_key="'surcharge_%s' % name">
<tr>
<td>
<dtml-if "proxy_field.is_message_delegated(name)">
<input type="checkbox" name="<dtml-var checkbox_key>"
checked="checked" />
<dtml-else >
<input type="checkbox" name="<dtml-var checkbox_key>" />
</dtml-if >
</td>
<td class="form-label"><dtml-var name></td>
<td><textarea name="&dtml-name;" cols="50" rows="4"><dtml-var value></textarea></td>
</tr>
</dtml-let>
</dtml-in>
</dtml-let>
<tr><td><input type="submit" value=" OK "></td></tr>
</table>
</form>
<dtml-var manage_page_footer>
......@@ -375,6 +375,35 @@ class TestProxyField(unittest.TestCase):
surcharge_tales()
delegate_tales()
def test_proxify_error_message(self):
"""
Test that error messages can be delegated and surcharged.
"""
# create a field
original_field = self.addField(self.container.Base_viewProxyFieldLibrary,
'my_title', 'OrigTitle', 'StringField')
field = self.addField(self.container.Base_view,
'my_dict_test', '', 'ProxyField')
field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
field_id='my_title',))
self.assertEquals(original_field.get_error_names(),
field.get_error_names())
test_error = 'too_long' # arbitrary chosen among StringField error names
test_message = 'Some Unprobable Error'
test_message2 = 'Some Even More Unprobable Error'
original_field.message_values[test_error] = test_message
field.message_values[test_error] = test_message2
# delegated (by default)
self.assertEquals(original_field.get_error_message(test_error),
test_message)
self.assertTrue(field.is_message_delegated(test_error))
self.assertEquals(field.get_error_message(test_error), test_message)
# surcharged
field.delegated_message_list = [test_error]
self.assertEquals(original_field.get_error_message(test_error),
test_message)
self.assertFalse(field.is_message_delegated(test_error))
self.assertEquals(field.get_error_message(test_error), test_message2)
class TestFieldValueCache(unittest.TestCase):
"""Tests field value caching system
......
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