Skip to content

Commit

Permalink
[FIX] mail: set error_msg in template_preview
Browse files Browse the repository at this point in the history
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: 0976ce5
Part-of: odoo#118710
  • Loading branch information
reth-odoo committed Apr 17, 2023
1 parent 85ce80f commit 20fe457
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions addons/mail/wizard/mail_template_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def _compute_mail_template_fields(self):
""" 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()
Expand All @@ -82,10 +83,10 @@ def _compute_mail_template_fields(self):
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:
Expand Down
14 changes: 14 additions & 0 deletions addons/test_mail/tests/test_mail_template_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def test_initial_values(self):
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({
Expand Down

0 comments on commit 20fe457

Please sign in to comment.