From c74e8a2fc41651b3a9928811c2ebf436d5c23837 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Tue, 24 Jan 2017 09:55:18 -0800 Subject: [PATCH] [IMP] partner_identification: Add context override (#373) Allow for context override of validations using ``id_no_validate`` --- partner_identification/README.rst | 10 +++++++++- partner_identification/__openerp__.py | 2 +- .../models/res_partner_id_category.py | 2 ++ .../tests/test_partner_identification.py | 20 ++++++++++++++++++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/partner_identification/README.rst b/partner_identification/README.rst index 20c19137e..1c5689789 100644 --- a/partner_identification/README.rst +++ b/partner_identification/README.rst @@ -35,8 +35,15 @@ Name: 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. + Optional python code called to validate ID numbers of this ID type. This functionality can be + overridden by setting ``id_no_validate`` to ``True`` in the context, such as: + .. code-block:: python + + partner.with_context(id_no_validate=True).write({ + 'name': 'Bad Value', + 'category_id': self.env.ref('id_category_only_numerics').id, + }) Usage ===== @@ -97,6 +104,7 @@ Contributors * Gerhard Könighofer * Laurent Mignon * Jairo Llopis +* Dave Lasley Maintainer ---------- diff --git a/partner_identification/__openerp__.py b/partner_identification/__openerp__.py index 931ef0d1b..dcabbd38a 100644 --- a/partner_identification/__openerp__.py +++ b/partner_identification/__openerp__.py @@ -11,7 +11,7 @@ { 'name': 'Partner Identification Numbers', 'category': 'Customer Relationship Management', - 'version': '10.0.1.0.0', + 'version': '10.0.1.0.1', 'depends': [ 'sales_team', ], diff --git a/partner_identification/models/res_partner_id_category.py b/partner_identification/models/res_partner_id_category.py index 71508512e..7b000f895 100644 --- a/partner_identification/models/res_partner_id_category.py +++ b/partner_identification/models/res_partner_id_category.py @@ -55,6 +55,8 @@ class ResPartnerIdCategory(models.Model): python validation code fails """ self.ensure_one() + if self.env.context.get('id_no_validate'): + return eval_context = self._validation_eval_context(id_number) try: safe_eval(self.validation_code, diff --git a/partner_identification/tests/test_partner_identification.py b/partner_identification/tests/test_partner_identification.py index 9d46f7642..5451b5f25 100644 --- a/partner_identification/tests/test_partner_identification.py +++ b/partner_identification/tests/test_partner_identification.py @@ -75,7 +75,7 @@ if id_number.name != '1235': 'category_id': partner_id_category2.id }) - def test_bad_calidation_code(self): + def test_bad_validation_code(self): partner_id_category = self.env['res.partner.id_category'].create({ 'code': 'id_code', 'name': 'id_name', @@ -90,3 +90,21 @@ if id_number.name != '1234' # missing : 'name': '1234', 'category_id': partner_id_category.id })]}) + + def test_bad_validation_code_override(self): + """ It should allow a bad validation code if context overrides. """ + 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_context( + id_no_validate=True, + ) + partner_1.write({'id_numbers': [(0, 0, { + 'name': '1234', + 'category_id': partner_id_category.id + })]})