[IMP] improved constraints: can now pass a callable message function instead of hardcoded message

bzr revid: odo@openerp.com-20100518170023-h6t203f3ik8j46vq
This commit is contained in:
Quentin De Paoli 2010-05-18 19:00:23 +02:00 committed by Olivier Dony
commit 60234cf878
1 changed files with 8 additions and 1 deletions

View File

@ -1023,7 +1023,14 @@ class orm_template(object):
for constraint in self._constraints:
fun, msg, fields = constraint
if not fun(self, cr, uid, ids):
translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
# Check presence of __call__ directly instead of using
# callable() because it will be deprecated as of Python 3.0
if hasattr(msg, '__call__'):
txt_msg, params = msg(self, cr, uid, ids)
tmp_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=txt_msg) or txt_msg
translated_msg = tmp_msg % params
else:
translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
error_msgs.append(
_("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)
)