From 20fe457377e1d366e1fa1d03768bfee0edf5680d Mon Sep 17 00:00:00 2001 From: Renaud Thiry Date: Mon, 6 Mar 2023 11:50:31 +0000 Subject: [PATCH] [FIX] mail: set error_msg in template_preview If the preview wizard was used to preview a template on a model that has no record, error_msg would not be set. This means the field is never set in that case and creating the wizard results in a cache miss on that field. The fix is to simply set it, and we add a test to cover that flow of the wizard. task-3162320 X-original-commit: 0976ce53e0a97a1f49dc2a262cf66355242fde25 Part-of: odoo/odoo#118710 --- addons/mail/wizard/mail_template_preview.py | 5 +++-- .../test_mail/tests/test_mail_template_preview.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/addons/mail/wizard/mail_template_preview.py b/addons/mail/wizard/mail_template_preview.py index 68ca33421cc..aea4beddcc3 100644 --- a/addons/mail/wizard/mail_template_preview.py +++ b/addons/mail/wizard/mail_template_preview.py @@ -71,6 +71,7 @@ class MailTemplatePreview(models.TransientModel): """ Preview the mail template (body, subject, ...) depending of the language and the record reference, more precisely the record id for the defined model of the mail template. If no record id is selectable/set, the inline_template placeholders won't be replace in the display information. """ + error_msg = False mail_template = self.mail_template_id.with_context(lang=self.lang) if not self.resource_ref or not self.resource_ref.id: self._set_mail_attributes() @@ -82,10 +83,10 @@ class MailTemplatePreview(models.TransientModel): self._MAIL_TEMPLATE_FIELDS )[self.resource_ref.id] self._set_mail_attributes(values=mail_values) - self.error_msg = False except (ValueError, UserError) as user_error: self._set_mail_attributes() - self.error_msg = user_error.args[0] + error_msg = user_error.args[0] + self.error_msg = error_msg def _set_mail_attributes(self, values=None): for field in self._MAIL_TEMPLATE_FIELDS: diff --git a/addons/test_mail/tests/test_mail_template_preview.py b/addons/test_mail/tests/test_mail_template_preview.py index 213f9e25e43..df223d6fd97 100644 --- a/addons/test_mail/tests/test_mail_template_preview.py +++ b/addons/test_mail/tests/test_mail_template_preview.py @@ -20,6 +20,20 @@ class TestMailTemplateTools(TestMailTemplateCommon): self.assertEqual(len(self.test_template.partner_to.split(',')), 2) self.assertTrue(self.test_record.email_from) + def test_mail_template_preview_empty_database(self): + """Check behaviour of the wizard when there is no record for the target model.""" + self.env['mail.test.lang'].search([]).unlink() + test_template = self.env['mail.template'].browse(self.test_template.ids) + preview = self.env['mail.template.preview'].create({ + 'mail_template_id': test_template.id, + }) + + self.assertFalse(preview.error_msg) + for field in preview._MAIL_TEMPLATE_FIELDS: + if field in ['partner_to', 'report_template_ids']: + continue + self.assertEqual(test_template[field], preview[field]) + def test_mail_template_preview_force_lang(self): test_record = self.env['mail.test.lang'].browse(self.test_record.ids) test_record.write({