[IMP] mail: Show reorder template response

If a tracked value sent a mail template on creation of a record,
the template was displayed before the original message in the chatter.

We fix this by using precommit hooks similar to those already used for
tracking.

We also update the query count for a couple of tests. This is required
because in those tests the test user is not in the cache when we execute
the precommit hook, which we use to fetch a fallback language early in
the precommit.

Task-2834304

Part-of: odoo/odoo#94018
This commit is contained in:
Renaud Thiry
2023-05-05 09:25:42 +02:00
parent c9637fd3bd
commit c9f55bb227
3 changed files with 48 additions and 4 deletions
+8 -2
View File
@@ -296,8 +296,9 @@ class MailThread(models.AbstractModel):
# based on tracked field to stay consistent with write
# we don't consider that a falsy field is a change, to stay consistent with previous implementation,
# but we may want to change that behaviour later.
thread._message_track_post_template(changes)
if changes:
self.env.cr.precommit.add(thread._track_post_template_finalize) # call to _track_post_template_finalize bound to this record
self.env.cr.precommit.data.setdefault(f'mail.tracking.create.{self._name}.{thread.id}', changes)
return threads
def write(self, values):
@@ -550,6 +551,11 @@ class MailThread(models.AbstractModel):
for id_ in self.ids:
authors[id_] = author
def _track_post_template_finalize(self):
"""Call the tracking template method with right values from precommit."""
self._message_track_post_template(self.env.cr.precommit.data.pop(f'mail.tracking.create.{self._name}.{self.id}', []))
self.env.flush_all()
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
@@ -4,6 +4,7 @@
from unittest.mock import patch
from odoo.addons.mail.tests.common import MailCommon
from odoo.addons.test_mail.data.test_mail_data import MAIL_TEMPLATE
from odoo.tests.common import tagged
from odoo.tests import Form
@@ -148,6 +149,43 @@ class TestTracking(MailCommon):
# one email send due to template
self.assertSentEmail(self.record.env.user.partner_id, [self.partner_admin], body='<p>Hello Test</p>')
def test_message_track_template_at_create_from_message(self):
"""Make sure records created through aliasing show the original message before the template"""
# setup
test_model = self.env['ir.model']._get('mail.test.ticket')
original_sender = self.user_admin.partner_id
custom_values = {'name': 'Test', 'customer_id': original_sender.id,
'mail_template': self.env.ref('test_mail.mail_test_ticket_tracking_tpl').id}
self.env['mail.alias'].create({
'alias_name': 'groups',
'alias_user_id': False,
'alias_model_id': test_model.id,
'alias_contact': 'everyone',
'alias_defaults': custom_values})
record = self.format_and_process(MAIL_TEMPLATE, '"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>',
'groups@test.com', target_field='customer_id', subject=custom_values['customer_id'],
target_model='mail.test.ticket')
with self.mock_mail_gateway(mail_unlink_sent=False):
self.flush_tracking()
# Should be trigger message and response template
self.assertEqual(len(record.message_ids), 2)
messages = list(record.message_ids)
messages.sort(key=lambda msg: msg.id)
trigger = messages[0]
template = messages[1]
self.assertIn('Please call me as soon as possible this afternoon!', trigger.body)
self.assertIn(f"Hello {custom_values['name']}", template.body)
self.assertMailMail(
original_sender,
'sent',
author=self.env.ref('base.partner_root'),
email_values={
'body_content': f"<p>Hello {custom_values['name']}</p>",
}
)
def test_create_partner_from_tracking_multicompany(self):
company1 = self.env['res.company'].create({'name': 'company1'})
self.env.user.write({'company_ids': [(4, company1.id, False)]})
+2 -2
View File
@@ -500,7 +500,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
test_template.write({'attachment_ids': [(5, 0)]})
customer = self.env['res.partner'].browse(self.customer.ids)
with self.assertQueryCount(admin=37, employee=37): # tm 27/27 / com 35/35
with self.assertQueryCount(admin=38, employee=38): # tm 28/28 / com 36/36
composer_form = Form(
self.env['mail.compose.message'].with_context({
'default_composition_mode': 'comment',
@@ -530,7 +530,7 @@ class TestMailAPIPerformance(BaseMailPerformance):
test_record, test_template = self._create_test_records()
customer = self.env['res.partner'].browse(self.customer.ids)
with self.assertQueryCount(admin=36, employee=36): # tm 26/26 / com 34/34
with self.assertQueryCount(admin=37, employee=37): # tm 27/27 / com 35/35
composer_form = Form(
self.env['mail.compose.message'].with_context({
'default_composition_mode': 'comment',