From eafbc90df802424c4c58c092249d3183bc7ab98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Hennecart?= Date: Fri, 7 Aug 2020 15:29:09 +0000 Subject: [PATCH] [FIX] mass_mailing: try to render jinja content when using test wizards Before when a user clicked on the button "Test", jinja syntax was not taken into account for the test mail sent. If an error was present clicking on the button did not raise an error before the cron was executed. Now, if there is at least one record in the mailing model, the template is correctly rendered and throws an error if there is a syntax error. If we don't have any record to render the template, we fallback on the raw content like before. PR odoo/odoo#55696 Task ID-2312442 X-original-commit: 92aef67cc8cd1f3ac1ac28803f69aeab1d14ca97 --- .../wizard/mailing_mailing_test.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/addons/mass_mailing/wizard/mailing_mailing_test.py b/addons/mass_mailing/wizard/mailing_mailing_test.py index 2ffba12ebf5..e3c1970afae 100644 --- a/addons/mass_mailing/wizard/mailing_mailing_test.py +++ b/addons/mass_mailing/wizard/mailing_mailing_test.py @@ -22,16 +22,28 @@ class TestMassMailing(models.TransientModel): mailing = self.mass_mailing_id test_emails = tools.email_split(self.email_to) mass_mail_layout = self.env.ref('mass_mailing.mass_mailing_mail_layout') + + record = self.env[mailing.mailing_model_real].search([], limit=1) + body = mailing._prepend_preview(mailing.body_html, mailing.preview) + subject = mailing.subject + + # If there is atleast 1 record for the model used in this mailing, then we use this one to render the template + # Downside: Jinja syntax is only tested when there is atleast one record of the mailing's model + if record: + # Returns a proper error if there is a syntax error with jinja + body = self.env['mail.render.mixin']._render_template(body, mailing.mailing_model_real, record.ids, post_process=True)[record.id] + subject = self.env['mail.render.mixin']._render_template(subject, mailing.mailing_model_real, record.ids)[record.id] + + # Convert links in absolute URLs before the application of the shortener + body = self.env['mail.render.mixin']._replace_local_links(body) + body = tools.html_sanitize(body, sanitize_attributes=True, sanitize_style=True) + for test_mail in test_emails: - # Convert links in absolute URLs before the application of the shortener - body = mailing._prepend_preview(mailing.body_html, mailing.preview) - body = self.env['mail.render.mixin']._replace_local_links(body) - body = tools.html_sanitize(body, sanitize_attributes=True, sanitize_style=True) mail_values = { 'email_from': mailing.email_from, 'reply_to': mailing.reply_to, 'email_to': test_mail, - 'subject': mailing.subject, + 'subject': subject, 'body_html': mass_mail_layout._render({'body': body}, engine='ir.qweb', minimal_qcontext=True), 'notification': True, 'mailing_id': mailing.id,