[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]: c15c07c405

after this commit, the search will return active records
only.

closes odoo/odoo#128563

X-original-commit: 08d4aeb0449d145a4e96565ff7e68dc856392904
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
niyasraphy
2023-07-15 13:05:56 +02:00
parent 8a310ae2e4
commit f8f15b7d59
2 changed files with 19 additions and 0 deletions
+1
View File
@@ -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)',
@@ -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)