From 75a8d7e2d152a30649a0fb4849ccd1605764b53c Mon Sep 17 00:00:00 2001 From: eLBati Date: Wed, 2 Sep 2015 15:19:09 +0200 Subject: [PATCH] partially porting to 8.0 API --- base_field_validator/ir_model.py | 71 +++++++++++--------- base_field_validator/ir_model_field_regex.py | 18 +++-- base_field_validator/test/validator.yml | 3 +- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/base_field_validator/ir_model.py b/base_field_validator/ir_model.py index 49b2f1ebb..dcf098084 100644 --- a/base_field_validator/ir_model.py +++ b/base_field_validator/ir_model.py @@ -18,44 +18,52 @@ # ############################################################################## -from openerp.osv import orm, fields import re from openerp.tools.translate import _ import openerp from openerp import SUPERUSER_ID +from openerp import models, fields, api, _ +from openerp.exceptions import Warning +from openerp.osv import orm -class IrModel(orm.Model): + +class IrModel(models.Model): _inherit = 'ir.model' - _columns = { - 'validator_line_ids': fields.one2many( - 'ir.model.validator.line', 'name', 'Validators'), - } - - def check_vals(self, cr, uid, vals, model, context=None): - for validator_line in model.validator_line_ids: - if validator_line.field_id.name in vals: - pattern = re.compile(validator_line.regex_id.regex) - if not pattern.match(vals[validator_line.field_id.name]): - raise orm.except_orm( - _('Error'), - _('Expression %s not valid for %s') % ( - validator_line.regex_id.regex, - vals[validator_line.field_id.name])) + validator_line_ids = fields.One2many( + 'ir.model.validator.line', 'name', 'Validators') + + @api.model + def check_vals(self, vals, model_name): + models = self.search([('model', '=', model_name)]) + for model in models: + for validator_line in model.validator_line_ids: + if validator_line.field_id.name in vals: + pattern = re.compile(validator_line.regex_id.regex) + if not pattern.match(vals[validator_line.field_id.name]): + raise Warning( + _('Expression %s not valid for %s') % ( + validator_line.regex_id.regex, + vals[validator_line.field_id.name])) return True - def _wrap_create(self, old_create, model): + +class IrModel(orm.Model): + + _inherit = 'ir.model' + + def _wrap_create(self, old_create, model_name): def wrapper(cr, uid, vals, context=None, **kwargs): - self.check_vals(cr, uid, vals, model, context=context) + self.check_vals(cr, uid, vals, model_name, context=context) new_id = old_create(cr, uid, vals, context=context, **kwargs) return new_id return wrapper - def _wrap_write(self, old_write, model): + def _wrap_write(self, old_write, model_name): def wrapper(cr, uid, ids, vals, context=None, **kwargs): - self.check_vals(cr, uid, vals, model, context=context) + self.check_vals(cr, uid, vals, model_name, context=context) res = old_write(cr, uid, ids, vals, context=context, **kwargs) return res @@ -64,7 +72,7 @@ class IrModel(orm.Model): def _register_hook(self, cr, ids=None): """ Wrap the methods `create` and `write` of the model """ - res = super(IrModel, self)._register_hook(cr, ids=ids) + res = super(IrModel, self)._register_hook(cr) if ids is None: ids = self.search( cr, SUPERUSER_ID, [('validator_line_ids', '!=', False)]) @@ -77,8 +85,9 @@ class IrModel(orm.Model): model_obj, 'field_validator_checked' ): model_obj.create = self._wrap_create( - model_obj.create, model) - model_obj.write = self._wrap_write(model_obj.write, model) + model_obj.create, model_name) + model_obj.write = self._wrap_write( + model_obj.write, model_name) model_obj.field_validator_checked = True updated = True if updated: @@ -100,13 +109,9 @@ class IrModel(orm.Model): return res -class IrModelValidatorLine(orm.Model): +class IrModelValidatorLine(models.Model): _name = "ir.model.validator.line" - _columns = { - 'name': fields.many2one('ir.model', string="Model", required=True), - 'field_id': fields.many2one( - 'ir.model.fields', 'Field', required=True), - 'regex_id': fields.many2one( - 'ir.model.fields.regex', string="Validator", - required=True), - } + name = fields.Many2one('ir.model', string="Model", required=True) + field_id = fields.Many2one('ir.model.fields', 'Field', required=True) + regex_id = fields.Many2one( + 'ir.model.fields.regex', string="Validator", required=True) diff --git a/base_field_validator/ir_model_field_regex.py b/base_field_validator/ir_model_field_regex.py index f9764d399..a97e3575c 100644 --- a/base_field_validator/ir_model_field_regex.py +++ b/base_field_validator/ir_model_field_regex.py @@ -18,16 +18,14 @@ # ############################################################################## -from openerp.osv import orm, fields +from openerp import models, fields -class IrModelFieldsRegex(orm.Model): +class IrModelFieldsRegex(models.Model): _name = "ir.model.fields.regex" - _columns = { - 'name': fields.char('Description', size=512, required=True), - 'regex': fields.char( - 'Regular Expression', size=512, required=True, - help="Regular expression used to validate the field. For example, " - "you can add the expression\n%s\nto the email field" - % r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'), - } + name = fields.Char('Description', size=512, required=True) + regex = fields.Char( + 'Regular Expression', size=512, required=True, + help="Regular expression used to validate the field. For example, " + "you can add the expression\n%s\nto the email field" + % r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b') diff --git a/base_field_validator/test/validator.yml b/base_field_validator/test/validator.yml index ef6f6c0f6..a9cde8461 100644 --- a/base_field_validator/test/validator.yml +++ b/base_field_validator/test/validator.yml @@ -19,12 +19,11 @@ Try invalid email - !python {model: res.partner}: | - from openerp.osv import orm try: self.write(cr, uid, [ref('base.res_partner_12')], { 'email': 'john', }) assert False, "An exception should have been raised, 'john' is not a valid email!" - except orm.except_orm: + except openerp.exceptions.Warning: # exception was raised as expected pass