From 1a7b1420280e93b50777961e41c63d8c13c9ca44 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Tue, 4 May 2021 11:53:00 +0200 Subject: [PATCH] email_template_qweb: ease override of values, include defaults FWD port of https://github.com/OCA/social/commit/cd35bfbec370c0b427b2afee4e6a317838cdb21c --- email_template_qweb/models/mail_template.py | 69 ++++++++++++++++++++- email_template_qweb/readme/CONTRIBUTORS.rst | 1 + 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/email_template_qweb/models/mail_template.py b/email_template_qweb/models/mail_template.py index f3adcdf16..bf73e9cd7 100644 --- a/email_template_qweb/models/mail_template.py +++ b/email_template_qweb/models/mail_template.py @@ -1,6 +1,9 @@ # Copyright 2016 Therp BV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import fields, models, tools +from odoo.tools.misc import format_amount + +from odoo.addons.mail.models.mail_template import format_date, format_datetime class MailTemplate(models.Model): @@ -25,7 +28,7 @@ class MailTemplate(models.Model): if template.body_type == "qweb" and (not fields or "body_html" in fields): for record in self.env[template.model].browse(res_id): body_html = template.body_view_id.render( - {"object": record, "email_template": template} + template._qweb_render_context(record) ) # Some wizards, like when sending a sales order, need this # fix to display accents correctly @@ -35,3 +38,67 @@ class MailTemplate(models.Model): result[res_id]["body_html"] ) return result if multi_mode else result[res_ids[0]] + + def _qweb_render_context(self, record): + """Compute variables to render Qweb templates. + + Some vars are added to ease porting of Jinja templates. + """ + res = { + "object": record, + "email_template": self, + } + ctx = dict(self.env.context) + if record._name == "mail.message": + partner_model = self.env["res.partner"] # pragma: no cover + # These variables are usually loaded when the notification is sent. + # It normally happens on `partner._notify` call, + # hence - due to the call here - is going to happen twice. + # Nevertheless there are some important values + # that can be mandatory for your templates + # especially if you load them + # from the mail composer. In this case + # the template will be rendered but it will miss many params. + # NOTE: some keys related to partner-specific permissions + # such as `actions` or `button_follow` won't be available + # until the final rendering happens. + # Check the variable `recipient_template_values` + # that comes from `_message_notification_recipients` + # in `partner._notify`. + ctx.update( + partner_model._notify_prepare_template_context(record) + ) # pragma: no cover + res.update( + { + # Same as for Jinja rendering, + # taken from `mail_template.render_template`. + # These ease porting of old Jinja templates to qweb ones. + # fmt: off + "format_date": + lambda date, format=False, context=self._context: format_date( + self.env, date, format + ), + "format_tz": + lambda dt, tz=False, format=False, context=self._context: + format_datetime( + self.env, dt, tz, format + ), + "format_amount": + lambda amount, currency, context=self._context: format_amount( + self.env, amount, currency + ), + # fmt: on + "user": self.env.user, + # keep it for Jinja template compatibility + "ctx": ctx, + } + ) + # Alias `record` to be always the main record needed by the template. + # Imagine a template for sale.order: when you load it in the composer + # the current `object` is the SO you are looking at, + # BUT when you send the message, the `object` is the message + # and not the SO anymore. + # Hence, stick the main record to `record` to make templates robust. + res["record"] = ctx.get("record", record) + res["record_name"] = ctx.get("record_name", record.display_name) + return res diff --git a/email_template_qweb/readme/CONTRIBUTORS.rst b/email_template_qweb/readme/CONTRIBUTORS.rst index e20e6e921..8d4f3c024 100644 --- a/email_template_qweb/readme/CONTRIBUTORS.rst +++ b/email_template_qweb/readme/CONTRIBUTORS.rst @@ -6,3 +6,4 @@ * Ernesto Tejeda * Thomas Fossoul (thomas@niboo.com) +* Simone Orsi