From c8f52b8c98775e98657600bfc3a559960c500be9 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Thu, 18 Nov 2010 18:47:21 +0200 Subject: [PATCH] ORM, ir_*: convert standard constraint messages to callables Also, fix way that callable constraints are passed with the context so that they can have translatable strings inside (but not on the results of the callable). Conflicts: bin/addons/base/ir/ir_model.py bin/addons/base/module/module.py bin/tools/translate.py bzr revid: p_christ@hol.gr-20101118164721-19rgx43d3p96b2lf --- bin/addons/base/ir/ir_actions.py | 6 +++++- bin/addons/base/ir/ir_model.py | 9 +++++++-- bin/addons/base/ir/ir_ui_menu.py | 7 ++++++- bin/addons/base/module/module.py | 9 +++++++-- bin/osv/orm.py | 11 ++++++++--- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/bin/addons/base/ir/ir_actions.py b/bin/addons/base/ir/ir_actions.py index 84419d25b51..807d0438b82 100644 --- a/bin/addons/base/ir/ir_actions.py +++ b/bin/addons/base/ir/ir_actions.py @@ -151,8 +151,12 @@ class act_window(osv.osv): if action.src_model and not self.pool.get(action.src_model): return False return True + + def _invalid_model_msg(self, cr, uid, ids, context=None): + return _('Invalid model name in the action definition.') + _constraints = [ - (_check_model, 'Invalid model name in the action definition.', ['res_model','src_model']) + (_check_model, _invalid_model_msg, ['res_model','src_model']) ] def _views_get_fnc(self, cr, uid, ids, name, arg, context={}): diff --git a/bin/addons/base/ir/ir_model.py b/bin/addons/base/ir/ir_model.py index 3f3700b0e45..5f9e3347b74 100644 --- a/bin/addons/base/ir/ir_model.py +++ b/bin/addons/base/ir/ir_model.py @@ -88,8 +88,10 @@ class ir_model(osv.osv): return False return True + def _model_name_msg(self, cr, uid, ids, context=None): + return _('The Object name must start with x_ and not contain any special character !') _constraints = [ - (_check_model_name, 'The Object name must start with x_ and not contain any special character !', ['model']), + (_check_model_name, _model_name_msg, ['model']), ] # overridden to allow searching both on model name (model field) @@ -181,8 +183,11 @@ class ir_model_fields(osv.osv): 'selectable': lambda *a: 1, } _order = "id" + def _size_gt_zero_msg(self, cr, user, ids, context=None): + return _('Size of the field can never be less than 1 !') + _sql_constraints = [ - ('size_gt_zero', 'CHECK (size>0)', 'Size of the field can never be less than 1 !'), + ('size_gt_zero', 'CHECK (size>0)',_size_gt_zero_msg ), ] def unlink(self, cr, user, ids, context=None): for field in self.browse(cr, user, ids, context): diff --git a/bin/addons/base/ir/ir_ui_menu.py b/bin/addons/base/ir/ir_ui_menu.py index f79493b4813..81dd5bef4d7 100644 --- a/bin/addons/base/ir/ir_ui_menu.py +++ b/bin/addons/base/ir/ir_ui_menu.py @@ -22,6 +22,7 @@ from osv import fields, osv import re import tools +from tools.translate import _ def one_in(setA, setB): """Check the presence of an element of setA in setB @@ -285,8 +286,12 @@ class ir_ui_menu(osv.osv): ('ir.actions.server', 'ir.actions.server'), ]), } + + def _rec_message(self, cr, uid, ids, context=None): + return _('Error ! You can not create recursive Menu.') + _constraints = [ - (_check_recursion, 'Error ! You can not create recursive Menu.', ['parent_id']) + (_check_recursion, _rec_message , ['parent_id']) ] _defaults = { 'icon' : lambda *a: 'STOCK_OPEN', diff --git a/bin/addons/base/module/module.py b/bin/addons/base/module/module.py index 81985332157..f392ee51326 100644 --- a/bin/addons/base/module/module.py +++ b/bin/addons/base/module/module.py @@ -187,9 +187,14 @@ class module(osv.osv): } _order = 'name' + def _name_uniq_msg(self, cr, uid, ids, context=None): + return _('The name of the module must be unique !') + def _certificate_uniq_msg(self, cr, uid, ids, context=None): + return _('The certificate ID of the module must be unique !') + _sql_constraints = [ - ('name_uniq', 'unique (name)', 'The name of the module must be unique !'), - ('certificate_uniq', 'unique (certificate)', 'The certificate ID of the module must be unique !') + ('name_uniq', 'UNIQUE (name)',_name_uniq_msg ), + ('certificate_uniq', 'UNIQUE (certificate)',_certificate_uniq_msg ) ] def unlink(self, cr, uid, ids, context=None): diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 5cf0e08a02e..e168146b595 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -1053,9 +1053,14 @@ class orm_template(object): # 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 + tmp_msg = msg(self, cr, uid, ids, context=context) + # Why translate something that has been generated dynamically? + # tmp_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=txt_msg) or txt_msg + if isinstance(tmp_msg, tuple): + tmp_msg, params = tmp_msg + translated_msg = tmp_msg % params + else: + translated_msg = tmp_msg else: translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg error_msgs.append(