From 897eedf22e7499be43c7014b05ca30ce22ab50cb Mon Sep 17 00:00:00 2001 From: "Abdelouahab (abla)" Date: Wed, 21 Sep 2022 13:37:25 +0000 Subject: [PATCH] [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 --- .../models/mail_thread_phone.py | 10 +++++++- .../tests/test_mail_thread_phone.py | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/addons/phone_validation/models/mail_thread_phone.py b/addons/phone_validation/models/mail_thread_phone.py index 229c743d9a4..931dee5f565 100644 --- a/addons/phone_validation/models/mail_thread_phone.py +++ b/addons/phone_validation/models/mail_thread_phone.py @@ -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.')) diff --git a/addons/test_mail_sms/tests/test_mail_thread_phone.py b/addons/test_mail_sms/tests/test_mail_thread_phone.py index e26bef4aee2..cd5350a45de 100644 --- a/addons/test_mail_sms/tests/test_mail_thread_phone.py +++ b/addons/test_mail_sms/tests/test_mail_thread_phone.py @@ -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 """