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({