[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
This commit is contained in:
Renaud Thiry
2023-05-10 10:18:46 +02:00
parent 6d420a3324
commit 1a25988101
4 changed files with 23 additions and 17 deletions
+1
View File
@@ -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):
+10 -12
View File
@@ -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': '<p></p>', # 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': '<p>I cannot find it. It was in my closet and pouf, disappeared.</p>',
})
@@ -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()
+2 -2
View File
@@ -9,8 +9,8 @@
<button name="action_set_won_rainbowman" string="Won"
type="object" class="oe_highlight" data-hotkey="w" title="Mark as won"
attrs="{'invisible': ['|','|', ('active','=',False), ('probability', '=', 100), ('type', '=', 'lead')]}"/>
<button name="%(crm.crm_lead_lost_action)d" string="Lost" data-hotkey="l" title="Mark as lost"
type="action" context="{'default_lead_id': active_id}" attrs="{'invisible': ['|', ('type', '=', 'lead'), '&amp;',('active', '=', False),('probability', '&lt;', 100)]}"/>
<button name="%(crm.action_set_lost_with_reason)d" string="Lost" data-hotkey="l" title="Mark as lost"
type="action" attrs="{'invisible': ['|', ('type', '=', 'lead'), '&amp;',('active', '=', False),('probability', '&lt;', 100)]}"/>
<button name="%(crm.action_crm_lead2opportunity_partner)d" string="Convert to Opportunity" type="action" help="Convert to Opportunity"
class="oe_highlight" attrs="{'invisible': ['|', ('type', '=', 'opportunity'), ('active', '=', False)]}" data-hotkey="v"/>
<button name="toggle_active" string="Restore" type="object" data-hotkey="z"
+10 -3
View File
@@ -10,20 +10,27 @@ class CrmLeadLost(models.TransientModel):
_name = 'crm.lead.lost'
_description = 'Get Lost Reason'
lead_ids = fields.Many2many('crm.lead')
lost_reason_id = fields.Many2one('crm.lost.reason', 'Lost Reason')
lost_feedback = fields.Html(
'Closing Note', sanitize=True
)
def action_lost_reason_apply(self):
"""Mark lead as lost and apply the loss reason"""
self.ensure_one()
leads = self.env['crm.lead'].browse(self.env.context.get('active_ids'))
# get lead from context for retro-compatibility
if not self.lead_ids and self.env.context.get('active_model', '') == 'crm.lead':
ctx = self.env.context
ctx_lead_ids = ctx.get('active_ids') or [ctx['active_id']] if ctx.get('active_id') else []
self.lead_ids = self.lead_ids.browse(ctx_lead_ids)
if not is_html_empty(self.lost_feedback):
leads._track_set_log_message(
self.lead_ids._track_set_log_message(
Markup('<div style="margin-bottom: 4px;"><p>%s:</p>%s<br /></div>') % (
_('Lost Comment'),
self.lost_feedback
)
)
res = leads.action_set_lost(lost_reason_id=self.lost_reason_id.id)
res = self.lead_ids.action_set_lost(lost_reason_id=self.lost_reason_id.id)
return res