From 3121454e9d02be3b0859b6aa5ff5b167c59ddcc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 2 Feb 2023 15:21:31 +0000 Subject: [PATCH] [IMP] mail: move composer 'email_add_signature' field to computed RATIONALE Remove _onchange_template_id / defaults usage for composer fields. It lacks control and make value computation hard to understand and predict. PURPOSE Split _onchange_template_id + default_get + get_record_data usage into editable computed stored fields. Each field should have its own method with its triggers (template, composition mode, ...). SPECIFICATIONS Improve composer to correctly compute field ``email_add_signature`` in compute method. When having a template, consider it defines completely body and do not add signature. Without template, add signature by default in comment due to post processing for notification emails. Mailing mode does not handle signature. Followup of odoo/odoo#107356 Task-2088884 (Mail: Use editable computed stored fields in composer) closes odoo/odoo#113522 Related: odoo/enterprise#37478 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/wizard/mail_compose_message.py | 18 ++++++++++++++++-- addons/test_mail/tests/test_mail_composer.py | 7 ++++--- addons/test_mail/tests/test_performance.py | 6 +++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index 3c419c50da0..fdc4b1cd449 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -93,7 +93,9 @@ class MailComposer(models.TransientModel): 'wizard_id', 'attachment_id', string='Attachments', compute='_compute_attachment_ids', readonly=False, store=True) email_layout_xmlid = fields.Char('Email Notification Layout', copy=False) - email_add_signature = fields.Boolean(default=True) + email_add_signature = fields.Boolean( + 'Add signature', + compute='_compute_email_add_signature', readonly=False, store=True) # origin email_from = fields.Char( 'From', compute='_compute_authorship', readonly=False, store=True, @@ -261,6 +263,18 @@ class MailComposer(models.TransientModel): elif not composer.template_id: composer.attachment_ids = False + @api.depends('template_id') + def _compute_email_add_signature(self): + """ When having a template, consider it defines completely body and + do not add signature. Without template, add signature by default + in comment due to post processing for notification emails. Mailing + mode does not handle signature. """ + for composer in self: + if composer.composition_mode == 'mass_mail': + composer.email_add_signature = False # not supported + else: + composer.email_add_signature = not bool(composer.template_id) + @api.depends('composition_mode', 'email_from', 'model', 'res_domain', 'res_ids', 'template_id') def _compute_authorship(self): @@ -804,7 +818,7 @@ class MailComposer(models.TransientModel): # back on the regular display_name retrieved in ``_notify_by_email_prepare_rendering_context()``. model_description = self.env.context.get('model_description') values.update( - email_add_signature=not bool(self.template_id) and self.email_add_signature, + email_add_signature=self.email_add_signature, email_layout_xmlid=self.email_layout_xmlid, force_send=self.force_send, mail_auto_delete=self.auto_delete, diff --git a/addons/test_mail/tests/test_mail_composer.py b/addons/test_mail/tests/test_mail_composer.py index d71d9f769e0..19d33643386 100644 --- a/addons/test_mail/tests/test_mail_composer.py +++ b/addons/test_mail/tests/test_mail_composer.py @@ -654,12 +654,13 @@ class TestComposerInternals(TestMailComposer): if composition_mode == 'comment': self.assertTrue(composer.auto_delete, 'By default, remove notification emails') self.assertFalse(composer.auto_delete_keep_log, 'Not used in comment mode') + self.assertTrue(composer.email_add_signature, 'Default value in comment mode') self.assertEqual(composer.subtype_id, self.env.ref('mail.mt_comment')) else: self.assertFalse(composer.auto_delete, 'By default, keep mailing emails') self.assertFalse(composer.auto_delete_keep_log, 'Emails are not unlinked, logs are already kept') + self.assertFalse(composer.email_add_signature, 'Not supported in mass mailing mode') self.assertFalse(composer.subtype_id) - self.assertTrue(composer.email_add_signature) self.assertEqual(composer.email_layout_xmlid, 'mail.test_layout') self.assertEqual(composer.message_type, 'comment') @@ -670,14 +671,14 @@ class TestComposerInternals(TestMailComposer): if composition_mode == 'comment': self.assertTrue(composer.auto_delete) self.assertFalse(composer.auto_delete_keep_log, 'Not used in comment mode') - self.assertTrue(composer.email_add_signature, 'TODO: should be False as template negates this config') + self.assertFalse(composer.email_add_signature, 'Template is considered as complete') self.assertEqual(composer.email_layout_xmlid, 'mail.test_layout') self.assertEqual(composer.message_type, 'comment') self.assertEqual(composer.subtype_id, self.env.ref('mail.mt_comment')) else: self.assertTrue(composer.auto_delete) self.assertTrue(composer.auto_delete_keep_log) - self.assertTrue(composer.email_add_signature, 'TODO: should be False as template negates this config') + self.assertFalse(composer.email_add_signature, 'Template is considered as complete') self.assertEqual(composer.email_layout_xmlid, 'mail.test_layout') self.assertEqual(composer.message_type, 'comment') self.assertFalse(composer.subtype_id) diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index 4d07de7c57f..6716b4acbcc 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -379,7 +379,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=11, employee=11): + with self.assertQueryCount(admin=12, employee=12): composer_form = Form( self.env['mail.compose.message'].with_context({ 'default_composition_mode': 'comment', @@ -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 36/36 + with self.assertQueryCount(admin=38, employee=38): # tm 28/28 / com 37/37 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=37, employee=37): # tm 27/27 / com 36/36 + with self.assertQueryCount(admin=38, employee=38): # tm 28/28 / com 37/37 composer_form = Form( self.env['mail.compose.message'].with_context({ 'default_composition_mode': 'comment',