Skip to content

Commit 31b9e5b

Browse files
committed
Draft: [16.0][FIX] email_template_qweb: use language-aware env for rendering QWeb
1 parent a170a31 commit 31b9e5b

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

email_template_qweb/models/mail_template.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,40 @@ class MailTemplate(models.Model):
1717

1818
def generate_email(self, res_ids, fields):
1919
multi_mode = True
20-
IrQweb = self.env["ir.qweb"]
21-
2220
if isinstance(res_ids, int):
2321
res_ids = [res_ids]
2422
multi_mode = False
25-
result = super(MailTemplate, self).generate_email(res_ids, fields=fields)
26-
for lang, (_template, _template_res_ids) in self._classify_per_lang(
23+
24+
result = super().generate_email(res_ids, fields=fields)
25+
26+
if self.body_type != "qweb_view" or (fields and "body_html" not in fields):
27+
return result if multi_mode else result[res_ids[0]]
28+
29+
for lang, (_template, template_res_ids) in self._classify_per_lang(
2730
res_ids
2831
).items():
2932
self_with_lang = self.with_context(lang=lang)
30-
for res_id in res_ids:
31-
if self.body_type == "qweb_view" and (
33+
34+
# bind QWeb AFTER switching lang context
35+
IrQweb = self_with_lang.env["ir.qweb"]
36+
37+
for res_id in template_res_ids: # ✅ use the bucketed ids
38+
if self_with_lang.body_type == "qweb_view" and (
3239
not fields or "body_html" in fields
3340
):
34-
for record in self_with_lang.env[self.model].browse(res_id):
35-
body_html = IrQweb._render(
36-
self_with_lang.body_view_id.id,
37-
{"object": record, "email_template": self_with_lang},
38-
)
39-
# Some wizards, like when sending a sales order, need this
40-
# fix to display accents correctly
41-
body_html = tools.ustr(body_html)
42-
result[res_id][
43-
"body_html"
44-
] = self_with_lang._render_template_postprocess(
45-
{res_id: body_html}
46-
)[
47-
res_id
48-
]
49-
result[res_id]["body"] = tools.html_sanitize(
50-
result[res_id]["body_html"]
51-
)
41+
record = self_with_lang.env[self_with_lang.model].browse(res_id)
42+
result_res_id = result[res_id]
43+
body_html = IrQweb._render(
44+
self_with_lang.body_view_id.id,
45+
{"object": record, "email_template": self_with_lang},
46+
)
47+
body_html = tools.ustr(body_html)
48+
rendered_post_temp = self_with_lang._render_template_postprocess(
49+
{res_id: body_html}
50+
)
51+
result_res_id["body_html"] = rendered_post_temp[res_id]
52+
result_res_id["body"] = tools.html_sanitize(
53+
result_res_id["body_html"]
54+
)
55+
5256
return result if multi_mode else result[res_ids[0]]

0 commit comments

Comments
 (0)