From f8f15b7d59e9ef9d8d950b28008e8e8f321d835d Mon Sep 17 00:00:00 2001 From: niyasraphy Date: Sun, 4 Jun 2023 07:38:57 +0000 Subject: [PATCH] [FIX] sales_team: searching on member_ids returning archived records before this commit, if user is searching on member_ids field in crm.team is giving an archived record also from member_ids table. * create a partner (Test Partner) and sales team (Test Team) * set the created sales team for partner * add and remove a user (A) to this sales team * search for partners with sales team in which user (A) is part of. * result says that partner (Test Partner) matches the search condition, which is wrong In [1] we ensure that m2o relations in multipath domains do not filter on 'active'. However the context variable that does this will propagate to all potential subqueries. In this case this means 'crm_team_member_ids.user_id' on sale teams will be searched without filtering on 'active' when it is a subquery of searching 'team_id.member_ids'. Even though searching 'crm_team_member_ids.user_id' on its own would have filtered on 'active'. The fix is to set the context for 'active_test' on the field directly, as we already have another field with 'active_test=False' if that is ever needed. [1]: c15c07c40517bfece5df66106772ed16eedcf6fa after this commit, the search will return active records only. closes odoo/odoo#128563 X-original-commit: 08d4aeb0449d145a4e96565ff7e68dc856392904 Signed-off-by: Thibault Delavallee (tde) --- addons/sales_team/models/crm_team.py | 1 + .../tests/test_sales_team_membership.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/addons/sales_team/models/crm_team.py b/addons/sales_team/models/crm_team.py index 5ac998b5ee8..abe395a48af 100644 --- a/addons/sales_team/models/crm_team.py +++ b/addons/sales_team/models/crm_team.py @@ -118,6 +118,7 @@ class CrmTeam(models.Model): member_warning = fields.Text('Membership Issue Warning', compute='_compute_member_warning') crm_team_member_ids = fields.One2many( 'crm.team.member', 'crm_team_id', string='Sales Team Members', + context={'active_test': True}, help="Add members to automatically assign their documents to this sales team.") crm_team_member_all_ids = fields.One2many( 'crm.team.member', 'crm_team_id', string='Sales Team Members (incl. inactive)', diff --git a/addons/sales_team/tests/test_sales_team_membership.py b/addons/sales_team/tests/test_sales_team_membership.py index 9186c98e27e..17dc0694f09 100644 --- a/addons/sales_team/tests/test_sales_team_membership.py +++ b/addons/sales_team/tests/test_sales_team_membership.py @@ -275,6 +275,24 @@ class TestMembership(TestSalesCommon): with self.assertRaises(exceptions.UserError), mute_logger('odoo.sql_db'): added.write({'crm_team_id': sales_team_1.id}) + def test_sales_team_member_search(self): + """ when a search is triggered on the member_ids field in crm.team + it is currently returning the archived records also. this test will + ensure that the search wont return archived record. + + this is to fix unwanted ORM behavior + """ + self.env['res.partner'].create({'name': 'Test Partner', 'team_id': self.new_team.id}) + self.env['crm.team.member'].create({ + 'user_id': self.env.uid, + 'crm_team_id': self.new_team.id, + 'active': False, + }) + partner_exists = self.env['res.partner'].search([ + ('team_id.member_ids', 'in', [self.env.uid]) + ]) + self.assertFalse(partner_exists, msg="Partner should return empty as current user is removed from team") + def test_users_sale_team_id(self): self.assertTrue(self.sales_team_1.sequence < self.new_team.sequence)