[FIX] phone_validation : filter by phone/mobile is set
To reproduce ============ Go to Contacts and try to filter by phone/mobile is/not set. A traceback is raised. Problem ======= The search method responsible for this field didn't take into account Value to filter by to be a boolean. Solution ======== Correctly support this case in search method. Add unit test. Task-3012789 opw-2980542 X-original-commit: 62b7da527c6ddb4654fbccf5c6d8c94158915ebc Part-of: odoo/odoo#104111
This commit is contained in:
committed by
Thibault Delavallée
parent
47f08bf9d3
commit
897eedf22e
@@ -48,6 +48,7 @@ class PhoneMixin(models.AbstractModel):
|
||||
phone_mobile_search = fields.Char("Phone/Mobile", store=False, search='_search_phone_mobile_search')
|
||||
|
||||
def _search_phone_mobile_search(self, operator, value):
|
||||
value = value.strip() if isinstance(value, str) else value
|
||||
phone_fields = [
|
||||
fname for fname in self._phone_get_number_fields()
|
||||
if fname in self._fields and self._fields[fname].store
|
||||
@@ -55,7 +56,14 @@ class PhoneMixin(models.AbstractModel):
|
||||
if not phone_fields:
|
||||
raise UserError(_('Missing definition of phone fields.'))
|
||||
|
||||
value = value.strip()
|
||||
# search if phone/mobile is set or not
|
||||
if (value is True or not value) and operator in ('=', '!='):
|
||||
if value:
|
||||
# inverse the operator
|
||||
operator = '=' if operator == '!=' else '!='
|
||||
op = expression.AND if operator == '=' else expression.OR
|
||||
return op([[(phone_field, operator, False)] for phone_field in phone_fields])
|
||||
|
||||
if len(value) < 3:
|
||||
raise UserError(_('Please enter at least 3 characters when searching a Phone/Mobile number.'))
|
||||
|
||||
|
||||
@@ -46,6 +46,29 @@ class TestSMSActionsCommon(TestSMSCommon, TestSMSRecipients):
|
||||
[False] * 5 + ['+32475110505', '0032475110606', '0032475110707', False]
|
||||
)
|
||||
|
||||
@users('employee')
|
||||
def test_search_phone_mobile_search_boolean(self):
|
||||
test_phone_records = self.test_phone_records.with_env(self.env)
|
||||
|
||||
# test Falsy -> is set / is not set
|
||||
for test_values in [False, '', ' ']:
|
||||
# test is not set -> both fields should be not set
|
||||
results = self.env['mail.test.sms.bl'].search([('phone_mobile_search', '=', test_values)])
|
||||
self.assertEqual(results, test_phone_records[-1],
|
||||
'Search on phone_mobile_search: = False: record with two void values')
|
||||
# test is set -> at least one field should be set
|
||||
results = self.env['mail.test.sms.bl'].search([('phone_mobile_search', '!=', test_values)])
|
||||
self.assertEqual(results, test_phone_records[:-1],
|
||||
'Search on phone_mobile_search: != False: record at least one value set')
|
||||
|
||||
# test Truthy -> is set / is not set
|
||||
results = self.env['mail.test.sms.bl'].search([('phone_mobile_search', '=', True)])
|
||||
self.assertEqual(results, test_phone_records[:-1],
|
||||
'Search on phone_mobile_search: = True: record at least one value set')
|
||||
results = self.env['mail.test.sms.bl'].search([('phone_mobile_search', '!=', True)])
|
||||
self.assertEqual(results, test_phone_records[-1],
|
||||
'Search on phone_mobile_search: != True: record with two void values')
|
||||
|
||||
@users('employee')
|
||||
def test_search_phone_mobile_search_equal(self):
|
||||
""" Test searching by phone/mobile with direct search """
|
||||
|
||||
Reference in New Issue
Block a user