Browse Source

[IMP] Add the possibility to specify a validation rule on the ID category

pull/255/head
Laurent Mignon (ACSONE) 9 years ago
parent
commit
9ec4cf3288
  1. 20
      partner_identifiers/README.rst
  2. 139
      partner_identifiers/i18n/chricar_partner_id_number.pot
  3. 45
      partner_identifiers/models/res_partner_id_category.py
  4. 6
      partner_identifiers/models/res_partner_id_number.py
  5. 42
      partner_identifiers/tests/test_partner_identifiers.py

20
partner_identifiers/README.rst

@ -32,7 +32,7 @@ For example, we create a category 'Driver License':
* Name : Name of this ID type. For example, 'Driver License'
* Code : Code, abbreviation or acronym of this ID type. For example, 'driver_license'
* Python validation code: Optional python code called to validate ID numbers of this ID type.
Usage
=====
@ -77,4 +77,22 @@ Credits
Contributors
------------
* Antonio Espinosa <antonioea@antiun.com>
* Laurent Mignon <laurent.mignon@acsone.eu>
Maintainer
----------
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://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 https://odoo-community.org.

139
partner_identifiers/i18n/chricar_partner_id_number.pot

@ -1,139 +0,0 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * chricar_partner_id_number
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 8.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-03-24 17:33+0000\n"
"PO-Revision-Date: 2015-03-24 17:33+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: chricar_partner_id_number
#: field:res.partner.id_category,active:0
#: field:res.partner.id_number,active:0
msgid "Active"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,category_id:0
msgid "Category"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,code:0
msgid "Code"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,create_uid:0
#: field:res.partner.id_number,create_uid:0
msgid "Created by"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,create_date:0
#: field:res.partner.id_number,create_date:0
msgid "Created on"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,id:0
#: field:res.partner.id_number,id:0
msgid "ID"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,name:0
msgid "ID Number"
msgstr ""
#. module: chricar_partner_id_number
#: view:res.partner:chricar_partner_id_number.view_partner_form
msgid "ID Numbers"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,name:0
msgid "ID name"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner,id_numbers:0
msgid "Identification Numbers"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,partner_issued_id:0
msgid "Issued by"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,date_issued:0
msgid "Issued on"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,write_uid:0
#: field:res.partner.id_number,write_uid:0
msgid "Last Updated by"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_category,write_date:0
#: field:res.partner.id_number,write_date:0
msgid "Last Updated on"
msgstr ""
#. module: chricar_partner_id_number
#: view:res.partner.id_number:chricar_partner_id_number.view_partner_id_numbers_form
#: field:res.partner.id_number,comment:0
msgid "Notes"
msgstr ""
#. module: chricar_partner_id_number
#: model:ir.model,name:chricar_partner_id_number.model_res_partner
#: field:res.partner.id_number,partner_id:0
msgid "Partner"
msgstr ""
#. module: chricar_partner_id_number
#: model:ir.actions.act_window,name:chricar_partner_id_number.action_partner_id_category
#: model:ir.ui.menu,name:chricar_partner_id_number.menu_partner_id_category
msgid "Partner ID Categories"
msgstr ""
#. module: chricar_partner_id_number
#: model:ir.actions.act_window,name:chricar_partner_id_number.action_partner_id_numbers_form
#: view:res.partner.id_number:chricar_partner_id_number.view_partner_id_numbers_form
#: view:res.partner.id_number:chricar_partner_id_number.view_partner_id_numbers_tree
msgid "Partner ID Numbers"
msgstr ""
#. module: chricar_partner_id_number
#: view:res.partner.id_category:chricar_partner_id_number.view_partner_id_category_form
#: view:res.partner.id_category:chricar_partner_id_number.view_partner_id_category_tree
msgid "Partner Identification Categories"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,state:0
msgid "State"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,valid_from:0
msgid "Valid from"
msgstr ""
#. module: chricar_partner_id_number
#: field:res.partner.id_number,valid_until:0
msgid "Valid until"
msgstr ""

45
partner_identifiers/models/res_partner_id_category.py

@ -6,10 +6,14 @@
# © 2015 Antiun Ingenieria, SL (Madrid, Spain)
# http://www.antiun.com
# Antonio Espinosa <antonioea@antiun.com>
# © 2016 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import models, fields
from openerp import api, models, fields
from openerp.exceptions import ValidationError, UserError
from openerp.tools.safe_eval import safe_eval
from openerp.tools.translate import _
class ResPartnerIdCategory(models.Model):
@ -19,3 +23,42 @@ class ResPartnerIdCategory(models.Model):
code = fields.Char(string="Code", size=16, required=True)
name = fields.Char(string="ID name", required=True, translate=True)
active = fields.Boolean(string="Active", default=True)
validation_code = fields.Text(
'Python validation code',
help="Python code called to validate an id number.",
default="""
# Python code. Use failed = True to .
# You can use the following variables :
# - self: browse_record of the current ID Category browse_record
# - id_number: browse_record of ID number to validte
"""
)
@api.multi
def _validation_eval_context(self, id_number):
self.ensure_one()
return {'self': self,
'id_number': id_number,
}
@api.multi
def validate_id_number(self, id_number):
"""Validate the given ID number
The method raises an openerp.exceptions.ValidationError if the eval of
python validation code fails
"""
self.ensure_one()
eval_context = self._validation_eval_context(id_number)
try:
safe_eval(self.validation_code,
eval_context,
mode='exec',
nocopy=True)
except Exception as e:
raise UserError(
_('Error when evaluating the id_category validation code:'
':\n %s \n(%s)') % (self.name, e))
if eval_context.get('failed', False):
raise ValidationError(
_("%s is not a valid %s identifier") % (
id_number.name, self.name))

6
partner_identifiers/models/res_partner_id_number.py

@ -9,13 +9,17 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import models, fields
from openerp import api, models, fields
class ResPartnerIdNumber(models.Model):
_name = "res.partner.id_number"
_order = "name"
@api.constrains('name')
def validate_id_number(self):
self.category_id.validate_id_number(self)
name = fields.Char(string="ID Number", required=True)
category_id = fields.Many2one(string="Category", required=True,
comodel_name='res.partner.id_category')

42
partner_identifiers/tests/test_partner_identifiers.py

@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from psycopg2._psycopg import IntegrityError
import openerp.tests.common as common
from openerp.exceptions import ValidationError
class TestPartnerIdentifierBase(common.TransactionCase):
@ -29,3 +30,44 @@ class TestPartnerIdentifierBase(common.TransactionCase):
})]
self.assertEquals(len(partner_1.id_numbers), 1)
self.assertEquals(partner_1.id_numbers.name, '1234')
class TestPartnerCategoryValidation(common.TransactionCase):
def test_partner_id_number_validation(self):
partner_id_category = self.env['res.partner.id_category'].create({
'code': 'id_code',
'name': 'id_name',
'validation_code': """
if id_number.name != '1234':
failed = True
"""
})
partner_1 = self.env.ref('base.res_partner_1')
with self.assertRaises(ValidationError), self.cr.savepoint():
partner_1.id_numbers = [(0, 0, {
'name': '01234',
'category_id': partner_id_category
})]
partner_1.id_numbers = [(0, 0, {
'name': '1234',
'category_id': partner_id_category
})]
self.assertEquals(len(partner_1.id_numbers), 1)
self.assertEquals(partner_1.id_numbers.name, '1234')
def test_bad_calidation_code(self):
partner_id_category = self.env['res.partner.id_category'].create({
'code': 'id_code',
'name': 'id_name',
'validation_code': """
if id_number.name != '1234' # missing :
failed = True
"""
})
partner_1 = self.env.ref('base.res_partner_1')
with self.assertRaises(ValidationError):
partner_1.id_numbers = [(0, 0, {
'name': '1234',
'category_id': partner_id_category
})]
Loading…
Cancel
Save