From 0f6f7c3676e0716345441c7de5091da5bdfacd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 21 Nov 2022 11:56:59 +0000 Subject: [PATCH] [IMP] mail: make composer mixin take lang from template Mail composer mixin already synchronizes subject and body based on a mail template, easing calling rendering methods available through mail render mixin. In this commit we add the same mechanism on lang. Lang is taken from the template and can be used to render content based on language defined on template, generally based on a customer. Some query counters are updated, without being sure those are really coming from this commit. Anyway they will be updated in batch at the end of this PR, as tracking them on each commit is tedious and not always really useful. Task-3046371 (Mail: Better Language Support in Composer) Part-of: odoo/odoo#106177 --- addons/mail/models/mail_composer_mixin.py | 12 ++++++++++++ addons/test_mail/tests/test_mail_composer_mixin.py | 3 +-- addons/test_mail/tests/test_performance.py | 12 ++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/addons/mail/models/mail_composer_mixin.py b/addons/mail/models/mail_composer_mixin.py index a17b5941199..797a6ad3946 100644 --- a/addons/mail/models/mail_composer_mixin.py +++ b/addons/mail/models/mail_composer_mixin.py @@ -26,6 +26,8 @@ class MailComposerMixin(models.AbstractModel): 'Contents', compute='_compute_body', readonly=False, store=True, render_engine='qweb', render_options={'post_process': True}, sanitize=False) template_id = fields.Many2one('mail.template', 'Mail Template', domain="[('model', '=', render_model)]") + # Language: override mail.render.mixin field, copy template value + lang = fields.Char(compute='_compute_lang', precompute=True, readonly=False, store=True) # Access is_mail_template_editor = fields.Boolean('Is Editor', compute='_compute_is_mail_template_editor') can_edit_body = fields.Boolean('Can Edit Body', compute='_compute_can_edit_body') @@ -46,6 +48,16 @@ class MailComposerMixin(models.AbstractModel): elif not composer_mixin.body: composer_mixin.body = False + @api.depends('template_id') + def _compute_lang(self): + for composer_mixin in self: + """ Take value form template when set. When removing the template + reset the value to avoid keeping part of template configuration. """ + if composer_mixin.template_id.lang: + composer_mixin.lang = composer_mixin.template_id.lang + elif not composer_mixin.template_id: + composer_mixin.lang = False + @api.depends_context('uid') def _compute_is_mail_template_editor(self): is_mail_template_editor = self.env.is_admin() or self.env.user.has_group('mail.group_mail_template_editor') diff --git a/addons/test_mail/tests/test_mail_composer_mixin.py b/addons/test_mail/tests/test_mail_composer_mixin.py index 9591646b4a0..4dd79b17f30 100644 --- a/addons/test_mail/tests/test_mail_composer_mixin.py +++ b/addons/test_mail/tests/test_mail_composer_mixin.py @@ -47,8 +47,8 @@ class TestMailComposerMixin(TestMailCommon, TestRecipients): 'source_ids': [(4, source.id)], }) self.assertEqual(composer.body, self.mail_template.body_html) + self.assertEqual(composer.lang, self.mail_template.lang) self.assertEqual(composer.subject, self.mail_template.subject) - self.assertFalse(composer.lang, 'Fixme: lang is not propagated currently') subject = composer._render_field('subject', source.ids)[source.id] self.assertEqual(subject, f'EnglishSubject for {source.name}') @@ -84,7 +84,6 @@ class TestMailComposerMixin(TestMailCommon, TestRecipients): source = self.test_record.with_env(self.env) composer = self.env['mail.test.composer.mixin'].create({ 'description': '

Description for

', - 'lang': '{{ object.customer_id.lang }}', 'name': 'Invite', 'template_id': self.mail_template.id, 'source_ids': [(4, source.id)], diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index b929055ccbc..7a57ab192a0 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -338,7 +338,7 @@ class TestMailAPIPerformance(BaseMailPerformance): def test_mail_composer(self): test_record, _test_template = self._create_test_records() customer_id = self.customer.id - with self.assertQueryCount(admin=3, employee=3): + with self.assertQueryCount(admin=4, employee=4): composer = self.env['mail.compose.message'].with_context({ 'default_composition_mode': 'comment', 'default_model': test_record._name, @@ -348,7 +348,7 @@ class TestMailAPIPerformance(BaseMailPerformance): 'partner_ids': [(4, customer_id)], }) - with self.assertQueryCount(admin=35, employee=35): + with self.assertQueryCount(admin=34, employee=34): composer._action_send_mail() @users('admin', 'employee') @@ -358,7 +358,7 @@ class TestMailAPIPerformance(BaseMailPerformance): test_record, _test_template = self._create_test_records() customer = self.env['res.partner'].browse(self.customer.ids) attachments = self.env['ir.attachment'].with_user(self.env.user).create(self.test_attachments_vals) - with self.assertQueryCount(admin=3, employee=3): + with self.assertQueryCount(admin=4, employee=4): composer = self.env['mail.compose.message'].with_context({ 'default_composition_mode': 'comment', 'default_model': test_record._name, @@ -369,7 +369,7 @@ class TestMailAPIPerformance(BaseMailPerformance): 'partner_ids': [(4, customer.id)], }) - with self.assertQueryCount(admin=38, employee=38): + with self.assertQueryCount(admin=37, employee=37): composer._action_send_mail() @users('admin', 'employee') @@ -426,7 +426,7 @@ class TestMailAPIPerformance(BaseMailPerformance): def test_mail_composer_nodelete(self): test_record, _test_template = self._create_test_records() customer_id = self.customer.id - with self.assertQueryCount(admin=3, employee=3): + with self.assertQueryCount(admin=4, employee=4): composer = self.env['mail.compose.message'].with_context({ 'default_composition_mode': 'comment', 'default_model': test_record._name, @@ -437,7 +437,7 @@ class TestMailAPIPerformance(BaseMailPerformance): 'partner_ids': [(4, customer_id)], }) - with self.assertQueryCount(admin=35, employee=35): + with self.assertQueryCount(admin=34, employee=34): composer._action_send_mail() @users('admin', 'employee')