From 76c2e7b784916cdca0753a46a7b2be75edc1d70d 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/__manifest__.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 20c19137e88..1c568978967 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/__manifest__.py b/partner_identification/__manifest__.py index 931ef0d1bac..dcabbd38a0e 100644 --- a/partner_identification/__manifest__.py +++ b/partner_identification/__manifest__.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 71508512e27..7b000f8958c 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 9d46f7642b6..5451b5f254d 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 + })]})