Lorenzo Battistini
10 years ago
committed by
eLBati
9 changed files with 147 additions and 58 deletions
-
3field_validator/__init__.py
-
7field_validator/__openerp__.py
-
54field_validator/ir_model.py
-
9field_validator/ir_model_demo.xml
-
33field_validator/ir_model_field_regex.py
-
14field_validator/ir_model_field_view.xml
-
22field_validator/ir_model_view.xml
-
5field_validator/security/ir.model.access.csv
-
34field_validator/test/validator.yml
@ -0,0 +1,9 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<openerp> |
|||
<data> |
|||
<record id="regex_mail" model="ir.model.fields.regex"> |
|||
<field name="regex">[^@]+@[^@]+\.[^@]+</field> |
|||
<field name="name">Email</field> |
|||
</record> |
|||
</data> |
|||
</openerp> |
@ -0,0 +1,33 @@ |
|||
# -*- coding: utf-8 -*- |
|||
############################################################################## |
|||
# |
|||
# Copyright (C) 2014 Agile Business Group sagl (<http://www.agilebg.com>) |
|||
# |
|||
# 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 <http://www.gnu.org/licenses/>. |
|||
# |
|||
############################################################################## |
|||
|
|||
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'), |
|||
} |
@ -1,14 +0,0 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<openerp> |
|||
<data> |
|||
<record id="view_model_fields_form_validator" model="ir.ui.view"> |
|||
<field name="model">ir.model.fields</field> |
|||
<field name="inherit_id" ref="base.view_model_fields_form"></field> |
|||
<field name="arch" type="xml"> |
|||
<field name="on_delete" position="after"> |
|||
<field name="regex_validator" attrs="{'readonly': [('ttype','not in',['char','text'])]}"></field> |
|||
</field> |
|||
</field> |
|||
</record> |
|||
</data> |
|||
</openerp> |
@ -0,0 +1,22 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<openerp> |
|||
<data> |
|||
<record id="view_model_form_validator" model="ir.ui.view"> |
|||
<field name="model">ir.model</field> |
|||
<field name="inherit_id" ref="base.view_model_form"></field> |
|||
<field name="arch" type="xml"> |
|||
<notebook position="inside"> |
|||
<page string="Validators"> |
|||
<field name="validator_line_ids"> |
|||
<tree editable="bottom"> |
|||
<field name="field_id" |
|||
domain="[('model_id','=',parent.id)]"></field> |
|||
<field name="regex_id"></field> |
|||
</tree> |
|||
</field> |
|||
</page> |
|||
</notebook> |
|||
</field> |
|||
</record> |
|||
</data> |
|||
</openerp> |
@ -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 |
@ -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 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue