From 1a25988101fea91b544dd1e5efd753baf8f71038 Mon Sep 17 00:00:00 2001 From: Renaud Thiry Date: Fri, 5 May 2023 08:26:52 +0000 Subject: [PATCH] [IMP] crm: replace context with explicit default Prior to this we used the context active_ids directly to determine which leads to mark as lost in the lost reason wizard. We now use a m2m field to store that value to make the behavior more obvious and predictable, and to ease debugging. task-3272955 Part-of: odoo/odoo#118494 --- addons/crm/models/crm_lead.py | 1 + addons/crm/tests/test_crm_lead_lost.py | 22 ++++++++++------------ addons/crm/views/crm_lead_views.xml | 4 ++-- addons/crm/wizard/crm_lead_lost.py | 13 ++++++++++--- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 8bb07da6af2..14a072f7377 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -999,6 +999,7 @@ class Lead(models.Model): def action_set_lost_with_reason(self): action = self.env['ir.actions.act_window']._for_xml_id('crm.crm_lead_lost_action') action.update({'context': {**self.env.context, **literal_eval(action.get('context', '{}'))}}) + action.setdefault('default_lead_ids', self.ids) return action def action_set_won(self): diff --git a/addons/crm/tests/test_crm_lead_lost.py b/addons/crm/tests/test_crm_lead_lost.py index 33d5f38ea41..4c39abe6f82 100644 --- a/addons/crm/tests/test_crm_lead_lost.py +++ b/addons/crm/tests/test_crm_lead_lost.py @@ -41,9 +41,8 @@ class TestLeadConvert(crm_common.TestCrmCommon): self.assertEqual(update_message.subtype_id, self.env.ref('mail.mt_note')) # mark as lost using the wizard - lost_wizard = self.env['crm.lead.lost'].with_context({ - 'active_ids': lead.ids, - }).create({ + lost_wizard = self.env['crm.lead.lost'].create({ + 'lead_ids': lead.ids, 'lost_reason_id': self.lost_reason.id, 'lost_feedback': '

', # void content }) @@ -76,9 +75,8 @@ class TestLeadConvert(crm_common.TestCrmCommon): self.assertEqual(len(leads), 10) self.flush_tracking() - lost_wizard = self.env['crm.lead.lost'].with_context({ - 'active_ids': leads.ids, - }).create({ + lost_wizard = self.env['crm.lead.lost'].create({ + 'lead_ids': leads.ids, 'lost_reason_id': self.lost_reason.id, 'lost_feedback': '

I cannot find it. It was in my closet and pouf, disappeared.

', }) @@ -121,12 +119,12 @@ class TestLeadConvert(crm_common.TestCrmCommon): 'name': 'Test Reason' }) - lost_wizard = self.env['crm.lead.lost'].with_context({ - 'active_ids': lead.ids - }).create({ - 'lost_reason_id': lost_reason.id - }) - # nice try little salesman, you cannot invoke a wizard to update other people leads with self.assertRaises(AccessError): + # wizard needs to be here due to cache clearing in assertRaises + # (ORM does not load m2m records unavailable to the user from database) + lost_wizard = self.env['crm.lead.lost'].create({ + 'lead_ids': lead.ids, + 'lost_reason_id': lost_reason.id + }) lost_wizard.action_lost_reason_apply() diff --git a/addons/crm/views/crm_lead_views.xml b/addons/crm/views/crm_lead_views.xml index dea34eccdb9..3f15ae04dae 100644 --- a/addons/crm/views/crm_lead_views.xml +++ b/addons/crm/views/crm_lead_views.xml @@ -9,8 +9,8 @@