[IMP] mail, crm: allow to give a body to tracking

PURPOSE

Concatenate feedback message and tracking when marking a lead as lost. This
currently generates 2 consecutive message about the same change, having a
single one is better from an UI point of view.

SPECIFICATIONS

Allow to link a body to a value change tracking. Tracking is currently done
by accumulating changes in a structure (see ``env.cr.precommit.data`` usage
with ``mail.tracking.<name>`` key). In the end those values are used to
generate a message with tracking value and a subtype.

In this commit we allow to manually set a body used as a message for the
tracking message, adding a new ``mail.tracking.message.<name>`` key. It is
used when posting or logging the tracking message, simply propagated as body
to ``message_post`` or ``message_log``.

In crm we use this when losting a lead through the dedicated wizard. A bit
of custom html allows to have a nice display.

Task-2671709

Part-of: odoo/odoo#78648
This commit is contained in:
Thibault Delavallée
2021-11-23 09:39:52 +00:00
parent 869ada08a0
commit 1341d52540
4 changed files with 33 additions and 15 deletions
+3 -4
View File
@@ -92,7 +92,7 @@ class TestLeadConvert(crm_common.TestCrmCommon):
self.assertEqual(lead.probability, 0)
self.assertEqual(lead.lost_reason_id, self.lost_reason)
# check messages
self.assertEqual(len(lead.message_ids), 3, 'Should have 3 messages: creation, lost, and log')
self.assertEqual(len(lead.message_ids), 2, 'Should have 2 messages: creation, lost with log')
lost_message = lead.message_ids.filtered(lambda msg: msg.subtype_id == self.env.ref('crm.mt_lead_lost'))
self.assertTrue(lost_message)
self.assertTracking(
@@ -101,9 +101,8 @@ class TestLeadConvert(crm_common.TestCrmCommon):
('lost_reason_id', 'many2one', False, self.lost_reason)
]
)
note_message = lead.message_ids.filtered(lambda msg: msg.subtype_id == self.env.ref('mail.mt_note'))
self.assertTrue(note_message)
self.assertEqual(note_message.body, '<p>I cannot find it. It was in my closet and pouf, disappeared.</p>')
self.assertIn('<p>I cannot find it. It was in my closet and pouf, disappeared.</p>', lost_message.body,
'Feedback should be included directly within tracking message')
@users('user_sales_salesman')
@mute_logger('odoo.addons.base.models')
+7 -7
View File
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
from odoo import fields, models, _
from odoo.tools.mail import is_html_empty
@@ -18,12 +18,12 @@ class CrmLeadLost(models.TransientModel):
def action_lost_reason_apply(self):
self.ensure_one()
leads = self.env['crm.lead'].browse(self.env.context.get('active_ids'))
res = leads.action_set_lost(lost_reason_id=self.lost_reason_id.id)
if not is_html_empty(self.lost_feedback):
leads._message_log_batch(
bodies=dict(
(lead.id, self.lost_feedback)
for lead in leads),
subject=False,
leads._track_set_log_message(
'<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)
return res
+2 -2
View File
@@ -5,10 +5,10 @@
<field name="model">crm.lead.lost</field>
<field name="arch" type="xml">
<form string="Lost Reason">
<group class="oe_title">
<group>
<field name="lost_reason_id" options="{'no_create_edit': True}" />
</group>
<field name="lost_feedback" class="oe_title" placeholder="What went wrong ?"/>
<field name="lost_feedback" placeholder="What went wrong ?"/>
<footer>
<button name="action_lost_reason_apply" string="Submit" type="object" class="btn-primary" data-hotkey="q"/>
<button string="Cancel" class="btn-secondary" special="cancel" data-hotkey="z"/>
+21 -2
View File
@@ -512,6 +512,16 @@ class MailThread(models.AbstractModel):
# we have to flush() again in case we triggered some recomputations
self.flush()
def _track_set_log_message(self, message):
""" Link tracking to a message logged as body, in addition to subtype
description (if set) and tracking values that make the core content of
tracking message. """
if not self._track_get_fields():
return
body_values = self.env.cr.precommit.data.setdefault(f'mail.tracking.message.{self._name}', {})
for id_ in self.ids:
body_values[id_] = message
@tools.ormcache('self.env.uid', 'self.env.su')
def _track_get_fields(self):
""" Return the set of tracked fields names for the current model. """
@@ -558,6 +568,8 @@ class MailThread(models.AbstractModel):
except MissingError:
continue
# find content to log as body
bodies = self.env.cr.precommit.data.pop(f'mail.tracking.message.{self._name}', {})
for record in self:
changes, tracking_value_ids = tracking.get(record.id, (None, None))
if not changes:
@@ -572,9 +584,16 @@ class MailThread(models.AbstractModel):
if not subtype.exists():
_logger.debug('subtype "%s" not found' % subtype.name)
continue
record.message_post(subtype_id=subtype.id, tracking_value_ids=tracking_value_ids)
record.message_post(
body=bodies.get(record.id) or '',
subtype_id=subtype.id,
tracking_value_ids=tracking_value_ids
)
elif tracking_value_ids:
record._message_log(tracking_value_ids=tracking_value_ids)
record._message_log(
body=bodies.get(record.id) or '',
tracking_value_ids=tracking_value_ids
)
return tracking