Skip to content

Commit

Permalink
[IMP] partner_identification: Add context override (OCA#373)
Browse files Browse the repository at this point in the history
Allow for context override of validations using ``id_no_validate``
  • Loading branch information
lasley authored and laurent.corron committed Nov 26, 2019
1 parent a9bdcd8 commit 304754e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
10 changes: 9 additions & 1 deletion partner_identification/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====
Expand Down Expand Up @@ -97,6 +104,7 @@ Contributors
* Gerhard Könighofer <gerhard.koenighofer@swing-system.com>
* Laurent Mignon <laurent.mignon@acsone.eu>
* Jairo Llopis <jairo.llopis@tecnativa.com>
* Dave Lasley <dave@laslabs.com>

Maintainer
----------
Expand Down
2 changes: 1 addition & 1 deletion partner_identification/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
2 changes: 2 additions & 0 deletions partner_identification/models/res_partner_id_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 19 additions & 1 deletion partner_identification/tests/test_partner_identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
})]})

0 comments on commit 304754e

Please sign in to comment.