[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
This commit is contained in:
Renaud Thiry
2023-04-17 11:47:11 +02:00
parent 85ce80f029
commit 20fe457377
2 changed files with 17 additions and 2 deletions
+3 -2
View File
@@ -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:
@@ -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({