From c9f55bb22777aab686daa94420854835af79354b Mon Sep 17 00:00:00 2001 From: Renaud Thiry Date: Thu, 4 May 2023 11:13:57 +0000 Subject: [PATCH] [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 --- addons/mail/models/mail_thread.py | 10 ++++-- addons/test_mail/tests/test_message_track.py | 38 ++++++++++++++++++++ addons/test_mail/tests/test_performance.py | 4 +-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index f93cc834a54..3de79540e91 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -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 diff --git a/addons/test_mail/tests/test_message_track.py b/addons/test_mail/tests/test_message_track.py index 3667c6c201d..75357bda2be 100644 --- a/addons/test_mail/tests/test_message_track.py +++ b/addons/test_mail/tests/test_message_track.py @@ -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='

Hello Test

') + 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" ', + '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"

Hello {custom_values['name']}

", + } + ) + 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)]}) diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index f8e77061eac..4824a08b860 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -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',