From ac6273e4351cfd232b4ca02e3e468e0feefe5e17 Mon Sep 17 00:00:00 2001 From: Lorenzo Battistini Date: Mon, 17 Nov 2014 17:56:12 +0100 Subject: [PATCH] [REF] working with tests --- field_validator/__init__.py | 3 +- field_validator/__openerp__.py | 7 +- .../{ir_model_field.py => ir_model.py} | 78 ++++++++++--------- field_validator/ir_model_demo.xml | 9 +++ field_validator/ir_model_field_regex.py | 33 ++++++++ field_validator/ir_model_field_view.xml | 14 ---- field_validator/ir_model_view.xml | 22 ++++++ field_validator/security/ir.model.access.csv | 5 ++ field_validator/test/validator.yml | 34 ++++++-- 9 files changed, 147 insertions(+), 58 deletions(-) rename field_validator/{ir_model_field.py => ir_model.py} (55%) create mode 100644 field_validator/ir_model_demo.xml create mode 100644 field_validator/ir_model_field_regex.py delete mode 100644 field_validator/ir_model_field_view.xml create mode 100644 field_validator/ir_model_view.xml create mode 100644 field_validator/security/ir.model.access.csv diff --git a/field_validator/__init__.py b/field_validator/__init__.py index 6998ce8c1..e556f3b5c 100644 --- a/field_validator/__init__.py +++ b/field_validator/__init__.py @@ -17,4 +17,5 @@ # along with this program. If not, see . # ############################################################################## -from . import ir_model_field +from . import ir_model +from . import ir_model_field_regex diff --git a/field_validator/__openerp__.py b/field_validator/__openerp__.py index 1db85a1ac..a28004312 100644 --- a/field_validator/__openerp__.py +++ b/field_validator/__openerp__.py @@ -31,9 +31,12 @@ 'license': 'AGPL-3', "depends": ['base'], "data": [ - 'ir_model_field_view.xml', + 'ir_model_view.xml', + 'security/ir.model.access.csv', + ], + "demo": [ + 'ir_model_demo.xml', ], - "demo": [], 'test': [ 'test/validator.yml', ], diff --git a/field_validator/ir_model_field.py b/field_validator/ir_model.py similarity index 55% rename from field_validator/ir_model_field.py rename to field_validator/ir_model.py index 47c3a4dc1..70f050b2a 100644 --- a/field_validator/ir_model_field.py +++ b/field_validator/ir_model.py @@ -25,29 +25,24 @@ import openerp from openerp import SUPERUSER_ID -class IrModelFields(orm.Model): +class IrModel(orm.Model): - _inherit = 'ir.model.fields' + _inherit = 'ir.model' _columns = { - 'regex_validator': fields.char( - 'Validator', size=256, - 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'), - } + 'validator_line_ids': fields.one2many( + 'ir.model.validator.line', 'name', 'Validators'), + } def check_vals(self, cr, uid, vals, model, context=None): - field_ids = self.search(cr, uid, [ - ('model', '=', model), ('regex_validator', '!=', False) - ], context=context) - for field in self.browse(cr, uid, field_ids, context=context): - if field.name in vals: - p = re.compile(field.regex_validator) - if not p.match(vals[field.name]): + 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 passed for %s') % ( - field.regex_validator, vals[field.name])) + validator_line.regex_id.regex, + vals[validator_line.field_id.name])) return True def _wrap_create(self, old_create, model): @@ -72,32 +67,45 @@ class IrModelFields(orm.Model): if ids is None: ids = self.search(cr, SUPERUSER_ID, []) updated = False - for field in self.browse(cr, SUPERUSER_ID, ids): - model = field.model_id.model - model_obj = self.pool.get(model) - if not hasattr(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.field_validator_checked = True - updated = True + for model in self.browse(cr, SUPERUSER_ID, ids): + if model.validator_line_ids: + model_name = model.model + model_obj = self.pool.get(model_name) + if model_obj and not hasattr( + 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.field_validator_checked = True + updated = True return updated def create(self, cr, uid, vals, context=None): - res_id = super(IrModelFields, self).create( + res_id = super(IrModel, self).create( cr, uid, vals, context=context) - if vals.get('regex_validator'): - if self._register_hook(cr, [res_id]): - openerp.modules.registry.RegistryManager.\ - signal_registry_change(cr.dbname) + if self._register_hook(cr, [res_id]): + openerp.modules.registry.RegistryManager.\ + signal_registry_change(cr.dbname) return res_id def write(self, cr, uid, ids, vals, context=None): if isinstance(ids, (int, long)): ids = [ids] - super(IrModelFields, self).write(cr, uid, ids, vals, context=context) - if vals.get('regex_validator'): - if self._register_hook(cr, ids): - openerp.modules.registry.RegistryManager.\ - signal_registry_change(cr.dbname) + super(IrModel, self).write(cr, uid, ids, vals, context=context) + if self._register_hook(cr, ids): + openerp.modules.registry.RegistryManager.\ + signal_registry_change(cr.dbname) return True + + +class IrModelValidatorLine(orm.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), + } diff --git a/field_validator/ir_model_demo.xml b/field_validator/ir_model_demo.xml new file mode 100644 index 000000000..857a7b642 --- /dev/null +++ b/field_validator/ir_model_demo.xml @@ -0,0 +1,9 @@ + + + + + [^@]+@[^@]+\.[^@]+ + Email + + + diff --git a/field_validator/ir_model_field_regex.py b/field_validator/ir_model_field_regex.py new file mode 100644 index 000000000..f9764d399 --- /dev/null +++ b/field_validator/ir_model_field_regex.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (C) 2014 Agile Business Group sagl () +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import orm, fields + + +class IrModelFieldsRegex(orm.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'), + } diff --git a/field_validator/ir_model_field_view.xml b/field_validator/ir_model_field_view.xml deleted file mode 100644 index 4fcb13463..000000000 --- a/field_validator/ir_model_field_view.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - ir.model.fields - - - - - - - - - diff --git a/field_validator/ir_model_view.xml b/field_validator/ir_model_view.xml new file mode 100644 index 000000000..de80f39fb --- /dev/null +++ b/field_validator/ir_model_view.xml @@ -0,0 +1,22 @@ + + + + + ir.model + + + + + + + + + + + + + + + + diff --git a/field_validator/security/ir.model.access.csv b/field_validator/security/ir.model.access.csv new file mode 100644 index 000000000..ff026f68f --- /dev/null +++ b/field_validator/security/ir.model.access.csv @@ -0,0 +1,5 @@ +"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink" +"access_ir_model_regex_erp_manager","ir_model_regex group_erp_manager","model_ir_model_fields_regex","base.group_erp_manager",1,1,1,1 +"access_ir_model_regex_all","access_ir_model_regex_all","model_ir_model_fields_regex",,1,0,0,0 +"access_ir_model_validator_line_erp_manager","ir_model_validator_line group_erp_manager","model_ir_model_validator_line","base.group_erp_manager",1,1,1,1 +"access_ir_model_validator_line_all","access_ir_model_validator_line_all","model_ir_model_validator_line",,1,0,0,0 diff --git a/field_validator/test/validator.yml b/field_validator/test/validator.yml index 6c23bc505..ef6f6c0f6 100644 --- a/field_validator/test/validator.yml +++ b/field_validator/test/validator.yml @@ -1,8 +1,30 @@ - - Try fields validator + set regex for partner - - !python {model: ir.model.fields}: | - from openerp.exceptions import Warning - field_ids = self.search(cr, uid, [('name', '=', 'email'), ('model', '=', 'res.partner')]) - assert len(field_ids) == 1, 'There must be 1 email field' - self.write(cr, uid, field_ids, {'regex_validator': r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'}) + !python {model: ir.model}: | + self.write(cr, uid, [ref('base.model_res_partner')], { + 'validator_line_ids': [(0,0, + { + 'name': ref('base.model_res_partner'), + 'field_id': ref('base.field_res_partner_email'), + 'regex_id': ref('regex_mail'), + })], + }) +- + Set valid email +- + !record {model: res.partner, id: base.res_partner_12}: + email: 'info@agilebg.com' +- + 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: + # exception was raised as expected + pass