Commit 3798fea5 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

support for calling unicode/str on a validation error before its actual translation.

If the exception is logged or printed before the call to translate, we don't
want to change its internal state so it may still be translated later.

Closes #286709
parent e2963189f80a
......@@ -99,12 +99,14 @@ class ValidationError(SchemaError):
self._translated = False
def __unicode__(self):
if not self._translated:
self.translate(unicode)
if len(self.errors) == 1:
attr, error = self.errors.items()[0]
if self._translated:
errors = self.errors
else:
errors = dict(self._translated_errors(unicode))
if len(errors) == 1:
attr, error = errors.items()[0]
return u'%s (%s): %s' % (self.entity, attr, error)
errors = '\n'.join('* %s: %s' % (k, v) for k, v in self.errors.items())
errors = '\n'.join('* %s: %s' % (k, v) for k, v in errors.items())
return u'%s:\n%s' % (self.entity, errors)
def translate(self, _):
......@@ -122,10 +124,12 @@ class ValidationError(SchemaError):
if self.i18nvalues:
for key in self.i18nvalues:
self.msgargs[key] = _(self.msgargs[key])
for key, msg in self.errors.iteritems():
msg = _(msg)
if key is not None:
msg = msg.replace('%(KEY-', '%('+key+'-')
self.errors[key] = msg % self.msgargs
self.errors = dict(self._translated_errors(_))
def _translated_errors(self, _):
for key, msg in self.errors.items():
msg = _(msg)
if key is not None:
msg = msg.replace('%(KEY-', '%('+key+'-')
yield key, msg % self.msgargs
......@@ -487,6 +487,14 @@ class SchemaTC(BaseSchemaTC):
self.assertEqual(verr.errors,
{None: 'global message about eid 1'})
def test_validation_error_unicode_then_translation(self):
verr = ValidationError(1, {None: 'global message about eid %(eid)s'}, {'eid': 1})
self.assertEqual(str(verr), '1 (None): global message about eid 1')
self.assertEqual(unicode(verr), '1 (None): global message about eid 1')
verr.translate(unicode)
self.assertEqual(verr.errors,
{None: 'global message about eid 1'})
def test_pickle(self):
"""schema should be pickeable"""
import pickle
......
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