From 8fc6b13e1e5c76cb28d9e49dbb7eee2b7a60ae9d Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Sat, 21 Jan 2017 14:09:11 -0800 Subject: [PATCH 1/3] [IMP] partner_identification: Add context override * 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..955223a5622 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..44d448829ae 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..c8a75e9612f 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 + })]}) From 9da75200ce6853b398a891fee62925b304b1a799 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Mon, 23 Jan 2017 09:49:35 -0800 Subject: [PATCH 2/3] Change context key name per suggestions --- partner_identification/README.rst | 2 +- partner_identification/models/res_partner_id_category.py | 2 +- partner_identification/tests/test_partner_identification.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/partner_identification/README.rst b/partner_identification/README.rst index 955223a5622..3660220d863 100644 --- a/partner_identification/README.rst +++ b/partner_identification/README.rst @@ -40,7 +40,7 @@ Python validation code: .. code-block:: python - partner.with_context(__id_no_validate__=True).write({ + partner.with_context(id_no_validate=True).write({ 'name': 'Bad Value', 'category_id': self.env.ref('id_category_only_numerics').id, }) diff --git a/partner_identification/models/res_partner_id_category.py b/partner_identification/models/res_partner_id_category.py index 44d448829ae..7b000f8958c 100644 --- a/partner_identification/models/res_partner_id_category.py +++ b/partner_identification/models/res_partner_id_category.py @@ -55,7 +55,7 @@ def validate_id_number(self, id_number): python validation code fails """ self.ensure_one() - if self.env.context.get('__id_no_validate__'): + if self.env.context.get('id_no_validate'): return eval_context = self._validation_eval_context(id_number) try: diff --git a/partner_identification/tests/test_partner_identification.py b/partner_identification/tests/test_partner_identification.py index c8a75e9612f..5451b5f254d 100644 --- a/partner_identification/tests/test_partner_identification.py +++ b/partner_identification/tests/test_partner_identification.py @@ -102,7 +102,7 @@ def test_bad_validation_code_override(self): """ }) partner_1 = self.env.ref('base.res_partner_1').with_context( - __id_no_validate__=True, + id_no_validate=True, ) partner_1.write({'id_numbers': [(0, 0, { 'name': '1234', From 972c08ae89321e0a37e5d36b09d6ea11d3b29f21 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Tue, 24 Jan 2017 09:44:30 -0800 Subject: [PATCH 3/3] Fix readme --- partner_identification/README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/partner_identification/README.rst b/partner_identification/README.rst index 3660220d863..1c568978967 100644 --- a/partner_identification/README.rst +++ b/partner_identification/README.rst @@ -36,7 +36,7 @@ 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. This functionality can be - overridden by setting ``__id_no_validate__`` to ``True`` in the context, such as: + overridden by setting ``id_no_validate`` to ``True`` in the context, such as: .. code-block:: python