From 9ac47b1d86f9eb26ef53e4b12ce8dbf9e1b306d0 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 20c19137e887..1c5689789676 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 931ef0d1bac4..dcabbd38a0e7 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 71508512e270..7b000f8958c1 100644 --- a/partner_identification/models/res_partner_id_category.py +++ b/partner_identification/models/res_partner_id_category.py @@ -55,6 +55,8 @@ def validate_id_number(self, id_number): 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 9d46f7642b6a..5451b5f254da 100644 --- a/partner_identification/tests/test_partner_identification.py +++ b/partner_identification/tests/test_partner_identification.py @@ -75,7 +75,7 @@ def test_partner_id_number_validation(self): '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 @@ def test_bad_calidation_code(self): '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 + })]})