Browse Source

Merge pull request #246 from eLBati/8.0-base_field_validator

8.0 base field validator
pull/364/head
Alexandre Fayolle 8 years ago
committed by GitHub
parent
commit
9245f7c72f
  1. 67
      base_field_validator/README.rst
  2. 5
      base_field_validator/__init__.py
  3. 27
      base_field_validator/__openerp__.py
  4. 9
      base_field_validator/data/ir_model_regex_data.xml
  5. 12
      base_field_validator/demo/ir_model_regex_demo.yml
  6. 98
      base_field_validator/i18n/it.po
  7. 6
      base_field_validator/models/__init__.py
  8. 92
      base_field_validator/models/ir_model.py
  9. 19
      base_field_validator/models/ir_model_field_regex.py
  10. 5
      base_field_validator/security/ir.model.access.csv
  11. BIN
      base_field_validator/static/description/icon.png
  12. 17
      base_field_validator/test/validator.yml
  13. 22
      base_field_validator/views/ir_model_view.xml

67
base_field_validator/README.rst

@ -0,0 +1,67 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
================
Fields Validator
================
This module allows to set a regular expression as field validator.
When the regular expresion is set, write and create operations on the involved
field are blocked, if the regular expression is not satisfied.
See demo and test data for an example with partner email.
Configuration
=============
Open ir.model form (say res.partner) and add 'Validators' lines
Usage
=====
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/8.0
Known issues / Roadmap
======================
The module performs the check at server side. Client side check is also needed,
to improve user experience and avoid server calls when unnecessary
Bug Tracker
===========
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
`here <https://github.com/OCA/server-tools/issues/new?body=module:%20base_field_validator%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Credits
=======
Contributors
------------
* Lorenzo Battistini <lorenzo.battistini@agilebg.com>
* Nicola Malcontenti <nicola.malcontenti@agilebg.com>
Maintainer
----------
.. image:: http://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: http://odoo-community.org
This module is maintained by the OCA.
OCA, or the Odoo Community Association, is a nonprofit organization
whose mission is to support the collaborative development of Odoo features
and promote its widespread use.
To contribute to this module, please visit http://odoo-community.org.

5
base_field_validator/__init__.py

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright © 2014-2016 Lorenzo Battistini - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import models

27
base_field_validator/__openerp__.py

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright © 2014-2016 Lorenzo Battistini - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': "Fields Validator",
'version': '8.0.1.0.0',
'category': 'Tools',
'summary': "Validate fields using regular expressions",
'author': 'Agile Business Group, Odoo Community Association (OCA)',
'website': 'http://www.agilebg.com',
'license': 'AGPL-3',
"depends": ['base'],
"data": [
'views/ir_model_view.xml',
'security/ir.model.access.csv',
'data/ir_model_regex_data.xml',
],
"demo": [
'demo/ir_model_regex_demo.yml',
],
'test': [
'test/validator.yml',
],
"active": False,
"installable": True
}

9
base_field_validator/data/ir_model_regex_data.xml

@ -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>

12
base_field_validator/demo/ir_model_regex_demo.yml

@ -0,0 +1,12 @@
-
set regex for partner
-
!python {model: ir.model}: |
self.write(cr, uid, [ref('base.model_res_partner')], {
'validator_line_ids': [(0,0,
{
'model_id': ref('base.model_res_partner'),
'field_id': ref('base.field_res_partner_email'),
'regex_id': ref('regex_mail'),
})],
})

98
base_field_validator/i18n/it.po

@ -0,0 +1,98 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_field_validator
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 8.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-09-02 13:30+0000\n"
"PO-Revision-Date: 2015-09-02 13:30+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
#. module: base_field_validator
#: field:ir.model.fields.regex,create_uid:0
#: field:ir.model.validator.line,create_uid:0
msgid "Created by"
msgstr "Created by"
#. module: base_field_validator
#: field:ir.model.fields.regex,create_date:0
#: field:ir.model.validator.line,create_date:0
msgid "Created on"
msgstr "Created on"
#. module: base_field_validator
#: field:ir.model.fields.regex,name:0
msgid "Description"
msgstr "Descrizione"
#. module: base_field_validator
#: code:addons/base_field_validator/ir_model.py:46
#, python-format
msgid "Expression %s not valid for %s"
msgstr "Espressione %s non valida per %s"
#. module: base_field_validator
#: field:ir.model.validator.line,field_id:0
msgid "Field"
msgstr "Campo"
#. module: base_field_validator
#: field:ir.model.fields.regex,id:0
#: field:ir.model.validator.line,id:0
msgid "ID"
msgstr "ID"
#. module: base_field_validator
#: field:ir.model.fields.regex,write_uid:0
#: field:ir.model.validator.line,write_uid:0
msgid "Last Updated by"
msgstr "Last Updated by"
#. module: base_field_validator
#: field:ir.model.fields.regex,write_date:0
#: field:ir.model.validator.line,write_date:0
msgid "Last Updated on"
msgstr "Last Updated on"
#. module: base_field_validator
#: field:ir.model.validator.line,name:0
msgid "Model"
msgstr "Model"
#. module: base_field_validator
#: model:ir.model,name:base_field_validator.model_ir_model
msgid "Models"
msgstr "Modelli"
#. module: base_field_validator
#: field:ir.model.fields.regex,regex:0
msgid "Regular Expression"
msgstr "Espressione regolare"
#. module: base_field_validator
#: help:ir.model.fields.regex,regex:0
msgid "Regular expression used to validate the field. For example, you can add the expression\n"
"\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b\n"
"to the email field"
msgstr "Regular expression used to validate the field. For example, you can add the expression\n"
"\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b\n"
"to the email field"
#. module: base_field_validator
#: field:ir.model.validator.line,regex_id:0
msgid "Validator"
msgstr "Validatore"
#. module: base_field_validator
#: view:ir.model:base_field_validator.view_model_form_validator
#: field:ir.model,validator_line_ids:0
msgid "Validators"
msgstr "Validatori"

6
base_field_validator/models/__init__.py

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright © 2014-2016 Lorenzo Battistini - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import ir_model
from . import ir_model_field_regex

92
base_field_validator/models/ir_model.py

@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
# Copyright © 2014-2016 Lorenzo Battistini - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import re
from openerp.tools.translate import _
from openerp import SUPERUSER_ID
from openerp import models, fields, api, _
from openerp.exceptions import Warning
class IrModel(models.Model):
_inherit = 'ir.model'
validator_line_ids = fields.One2many(
'ir.model.validator.line', 'model_id', 'Validators')
@api.model
def check_vals(self, vals, model_name):
validator_lines = self.env['ir.model.validator.line'].search([
('model_id.model', '=', model_name),
('field_id.name', 'in', vals.keys())])
for validator_line in validator_lines:
pattern = re.compile(validator_line.regex_id.regex)
if vals[validator_line.field_id.name]:
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 _field_validator_hook(self, cr, ids):
def _wrap_create():
def create(self, cr, uid, vals, context=None, **kwargs):
model_pool = self.pool['ir.model']
model_pool.check_vals(
cr, uid, vals, self._name, context=context)
new_id = create.origin(
self, cr, uid, vals, context=context, **kwargs)
return new_id
return create
def _wrap_write():
def write(self, cr, uid, ids, vals, context=None, **kwargs):
model_pool = self.pool['ir.model']
model_pool.check_vals(
cr, uid, vals, self._name, context=context)
res = write.origin(
self, cr, uid, ids, vals, context=context, **kwargs)
return res
return write
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._patch_method('create', _wrap_create())
model_obj._patch_method('write', _wrap_write())
model_obj.field_validator_checked = True
return True
def _register_hook(self, cr):
self._field_validator_hook(cr, self.search(cr, SUPERUSER_ID, []))
return super(IrModel, self)._register_hook(cr)
def create(self, cr, uid, vals, context=None):
res_id = super(IrModel, self).create(
cr, uid, vals, context=context)
self._field_validator_hook(cr, [res_id])
return res_id
def write(self, cr, uid, ids, vals, context=None):
if isinstance(ids, (int, long)):
ids = [ids]
res = super(IrModel, self).write(cr, uid, ids, vals, context=context)
self._field_validator_hook(cr, ids)
return res
class IrModelValidatorLine(models.Model):
_name = "ir.model.validator.line"
_rec_name = 'model_id'
model_id = 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)

19
base_field_validator/models/ir_model_field_regex.py

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Copyright © 2014-2016 Lorenzo Battistini - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import models, fields
class IrModelFieldsRegex(models.Model):
_name = "ir.model.fields.regex"
name = fields.Char('Description', required=True)
regex = fields.Char(
'Regular Expression', required=True,
help="Regular expression used to validate the field. For example, "
"you can add the expression\n%s\nto the email field"
% r'[^@]+@[^@]+\.[^@]+')
_sql_constraints = [(
'name_unique', 'unique (name)',
'The name of a regular expression must be unique'
)]

5
base_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

BIN
base_field_validator/static/description/icon.png

After

Width: 249  |  Height: 249  |  Size: 18 KiB

17
base_field_validator/test/validator.yml

@ -0,0 +1,17 @@
-
Set valid email
-
!record {model: res.partner, id: base.res_partner_12}:
email: 'info@agilebg.com'
-
Try invalid email
-
!python {model: res.partner}: |
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 openerp.exceptions.Warning:
# exception was raised as expected
pass

22
base_field_validator/views/ir_model_view.xml

@ -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>
Loading…
Cancel
Save