Skip to content

Commit

Permalink
[IMP] partner_identification: Add search option
Browse files Browse the repository at this point in the history
  • Loading branch information
lasley committed May 26, 2017
1 parent 81c8544 commit b41812f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
54 changes: 46 additions & 8 deletions partner_identification/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ def _compute_identification(self, field_name, category_code):
.. code-block:: python
social_security_id = fields.Char(
string='Social Security',
social_security = fields.Char(
compute=lambda s: s._compute_identification(
'social_security_id', 'SSN',
'social_security', 'SSN',
),
inverse=lambda s: s._inverse_identification(
'social_security_id', 'SSN',
'social_security', 'SSN',
),
search=lambda s, *a: s._search_identification(
'social_security', 'SSN', *a
),
)
Expand Down Expand Up @@ -76,13 +78,15 @@ def _inverse_identification(self, field_name, category_code):
.. code-block:: python
social_security_id = fields.Char(
string='Social Security',
social_security = fields.Char(
compute=lambda s: s._compute_identification(
'social_security_id', 'SSN',
'social_security', 'SSN',
),
inverse=lambda s: s._inverse_identification(
'social_security_id', 'SSN',
'social_security', 'SSN',
),
search=lambda s, *a: s._search_identification(
'social_security', 'SSN', *a
),
)
Expand Down Expand Up @@ -130,3 +134,37 @@ def _inverse_identification(self, field_name, category_code):
) % (
record._name, category_code, field_name,
))

@api.model
def _search_identification(self, field_name, category_code,
operator, value):
""" Search method for an identification field.
Example:
.. code-block:: python
social_security = fields.Char(
compute=lambda s: s._compute_identification(
'social_security', 'SSN',
),
inverse=lambda s: s._inverse_identification(
'social_security', 'SSN',
),
search=lambda s, *a: s._search_identification(
'social_security', 'SSN', *a
),
)
Args:
field_name (str): Name of field to set.
category_code (str): Category code of the Identification type.
Returns:
list: Domain to search with.
"""

return [
(field_name, operator, value),
('category_id.code', '=', category_code),
]
56 changes: 55 additions & 1 deletion partner_identification/tests/test_res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,57 @@
# Copyright 2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo.tests import common
from odoo.exceptions import ValidationError


class TestResPartner(common.TransactionCase):
class ResPartner(models.Model):
_inherit = 'res.partner'

social_security = fields.Char(
compute=lambda s: s._compute_identification(
'social_security', 'SSN',
),
inverse=lambda s: s._inverse_identification(
'social_security', 'SSN',
),
search=lambda s, *a: s._search_identification(
'social_security', 'SSN', *a
),
)


class TestResPartner(common.SavepointCase):

@classmethod
def _init_test_model(cls, model_cls):
""" Build a model from model_cls in order to test abstract models.
Note that this does not actually create a table in the database, so
there may be some unidentified edge cases.
Args:
model_cls (openerp.models.BaseModel): Class of model to initialize
Returns:
model_cls: Instance
"""
registry = cls.env.registry
cr = cls.env.cr
inst = model_cls._build_model(registry, cr)
model = cls.env[model_cls._inherit].with_context(todo=[])
model._prepare_setup()
model._setup_base(partial=False)
model._setup_fields(partial=False)
model._setup_complete()
model._auto_init()
model.init()
model._auto_end()
return inst

@classmethod
def setUpClass(cls):
super(TestResPartner, cls).setUpClass()
cls.env.registry.enter_test_mode()
cls._init_test_model(ResPartner)

def setUp(self):
super(TestResPartner, self).setUp()
Expand Down Expand Up @@ -70,3 +116,11 @@ def test_inverse_identification_multi_exception(self):
})
with self.assertRaises(ValidationError):
self.partner._inverse_identification('name', 'id_code')

def test_search_identification(self):
""" It should return the right record when searched by ID. """
self.partner.social_security = 'Test'
partner = self.env['res.partner'].search([
('social_security', '=', 'Test'),
])
self.assertEqual(partner, self.partner)

0 comments on commit b41812f

Please sign in to comment.