Skip to content

Commit

Permalink
[MIG] email_template_qweb: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaisLForgeFlow committed May 6, 2024
1 parent 28acbb2 commit 4c4c80b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 72 deletions.
14 changes: 9 additions & 5 deletions email_template_qweb/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ Usage

To use this module, you need to:

1. Select QWeb View in the field Body templating engine
2. Select a QWeb View to be used to render the body field
3. Apart from QWeb View standard variables, you also have access to
``object`` and ``email_template``, which are browse records of the
current object and the email template in use, respectively.
1. Go to *Settings / Email Templates* and create a new template
2. Select QWeb View in the field Body templating engine
3. Select a QWeb View to be used to render the body field

Apart from QWeb View standard variables, you also have access to
``object`` and ``email_template``, which are browse records of the
current object and the email template in use, respectively.

Bug Tracker
===========
Expand Down Expand Up @@ -92,6 +94,8 @@ Contributors

- Dzung Tran <[email protected]>

- Anaïs Lopez <[email protected]>

Other credits
-------------

Expand Down
7 changes: 4 additions & 3 deletions email_template_qweb/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# Copyright 2016-2024 Therp BV <http://therp.nl>
# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "QWeb for email templates",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"author": "Therp BV, Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Marketing",
Expand Down
62 changes: 28 additions & 34 deletions email_template_qweb/models/mail_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# Copyright 2016-2024 Therp BV <http://therp.nl>
# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields, models, tools
from odoo.tools import format_datetime


class MailTemplate(models.Model):
Expand All @@ -15,36 +17,28 @@ class MailTemplate(models.Model):
body_view_id = fields.Many2one("ir.ui.view", domain=[("type", "=", "qweb")])
body_view_arch = fields.Text(related="body_view_id.arch")

def generate_email(self, res_ids, fields):
multi_mode = True
IrQweb = self.env["ir.qweb"]
def _generate_template(self, res_ids, render_fields, find_or_create_partners=False):
render_results = super()._generate_template(
res_ids, render_fields, find_or_create_partners=find_or_create_partners
)

if isinstance(res_ids, int):
res_ids = [res_ids]
multi_mode = False
result = super(MailTemplate, self).generate_email(res_ids, fields=fields)
for lang, (_template, _template_res_ids) in self._classify_per_lang(
res_ids
).items():
self_with_lang = self.with_context(lang=lang)
for res_id in res_ids:
if self.body_type == "qweb_view" and (
not fields or "body_html" in fields
):
for record in self_with_lang.env[self.model].browse(res_id):
body_html = IrQweb._render(
self_with_lang.body_view_id.id,
{"object": record, "email_template": self_with_lang},
)
# Some wizards, like when sending a sales order, need this
# fix to display accents correctly
body_html = tools.ustr(body_html)
result[res_id][
"body_html"
] = self_with_lang._render_template_postprocess(
{res_id: body_html}
)[res_id]
result[res_id]["body"] = tools.html_sanitize(
result[res_id]["body_html"]
)
return result if multi_mode else result[res_ids[0]]
if self.body_type == "qweb_view":
if "body_html" in render_fields:
IrQweb = self.env["ir.qweb"]
for res_id in res_ids:
record = self.env[self.model].browse(res_id)
custom_context = {
"object": record,
"email_template": self,
"format_datetime": lambda dt,
tz=False,
dt_format=False,
lang_code=False: format_datetime(
self.env, dt, tz, dt_format, lang_code
),
}
body_html = IrQweb._render(self.body_view_id.id, custom_context)
body_html = tools.ustr(body_html)
render_results[res_id]["body_html"] = body_html

return render_results
4 changes: 3 additions & 1 deletion email_template_qweb/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@

- Foram Shah \<<[email protected]>\>

- [Trobz](https://trobz.com):
- [Trobz](https://trobz.com):
- Dzung Tran \<<[email protected]>\>

- Anaïs Lopez \<<[email protected]>\>
12 changes: 7 additions & 5 deletions email_template_qweb/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
To use this module, you need to:

1. Select QWeb View in the field Body templating engine
2. Select a QWeb View to be used to render the body field
3. Apart from QWeb View standard variables, you also have access to
`object` and `email_template`, which are browse records of the
current object and the email template in use, respectively.
1. Go to *Settings / Email Templates* and create a new template
2. Select QWeb View in the field Body templating engine
3. Select a QWeb View to be used to render the body field

Apart from QWeb View standard variables, you also have access to
`object` and `email_template`, which are browse records of the
current object and the email template in use, respectively.
9 changes: 6 additions & 3 deletions email_template_qweb/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,13 @@ <h1 class="title">QWeb for email templates</h1>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>To use this module, you need to:</p>
<ol class="arabic simple">
<li>Go to <em>Settings / Email Templates</em> and create a new template</li>
<li>Select QWeb View in the field Body templating engine</li>
<li>Select a QWeb View to be used to render the body field</li>
<li>Apart from QWeb View standard variables, you also have access to
<tt class="docutils literal">object</tt> and <tt class="docutils literal">email_template</tt>, which are browse records of the
current object and the email template in use, respectively.</li>
</ol>
<p>Apart from QWeb View standard variables, you also have access to
<tt class="docutils literal">object</tt> and <tt class="docutils literal">email_template</tt>, which are browse records of the
current object and the email template in use, respectively.</p>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
Expand Down Expand Up @@ -443,6 +444,8 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li>Dzung Tran &lt;<a class="reference external" href="mailto:dungtd&#64;trobz.com">dungtd&#64;trobz.com</a>&gt;</li>
</ul>
</li>
<li><p class="first">Anaïs Lopez &lt;<a class="reference external" href="mailto:anais.lopez&#64;forgeflow.com">anais.lopez&#64;forgeflow.com</a>&gt;</p>
</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand Down
26 changes: 15 additions & 11 deletions email_template_qweb/tests/test_mail_template_qweb.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# Copyright 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# Copyright 2016-2024 Therp BV <http://therp.nl>
# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase


class TestMailTemplateQweb(TransactionCase):
def test_email_template_qweb(self):
template = self.env.ref("email_template_qweb.email_template_demo1")
mail_values = template.generate_email([self.env.user.id], ["body_html"])
render_fields = ["body_html"]
mail_values = template._generate_template([self.env.user.id], render_fields)
self.assertTrue(
# this comes from the called template if everything worked
"<footer>" in mail_values[self.env.user.id]["body_html"],
"Did not receive rendered template in response. Got: \n%s\n"
% (mail_values[self.env.user.id]["body_html"]),
"Did not receive rendered template in response. Got: \n{}".format(
mail_values[self.env.user.id]["body_html"]
),
)
# the same method is also called in a non multi mode
mail_values = template.generate_email(self.env.user.id, ["body_html"])
mail_values_single_mode = template._generate_template(
[self.env.user.id], render_fields
)
self.assertTrue(
# this comes from the called template if everything worked
"<footer>" in mail_values["body_html"],
"Did not receive rendered template in response. Got: \n%s\n"
% (mail_values["body_html"]),
"<footer>" in mail_values_single_mode[self.env.user.id]["body_html"],
"Did not receive rendered template in response. Got: \n{}".format(
mail_values_single_mode[self.env.user.id]["body_html"]
),
)
16 changes: 6 additions & 10 deletions email_template_qweb/views/mail_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,26 @@
<field name="body_type" />
</field>
<field name="body_html" position="before">
<group attrs="{'invisible': [('body_type', '!=', 'qweb_view')]}">
<field
name="body_view_id"
attrs="{'required': [('body_type', '=', 'qweb_view')]}"
/>
<group invisible="body_type != 'qweb_view'">
<field name="body_view_id" required="body_type == 'qweb_view'" />
<field
name="body_view_arch"
widget="ace"
attrs="{'required': [('body_type', '=', 'qweb_view')], 'invisible': [('body_view_id', '=', False)]}"
invisible="not body_view_id"
required="body_type == 'qweb_view'"
/>
</group>
</field>
<field name="body_html" position="attributes">
<attribute
name="attrs"
>{'invisible': [('body_type', '!=', 'qweb')]}</attribute>
<attribute name="invisible">body_type != 'qweb'</attribute>
</field>
</field>
</record>
<record id="email_template_tree" model="ir.ui.view">
<field name="model">mail.template</field>
<field name="inherit_id" ref="mail.email_template_tree" />
<field name="arch" type="xml">
<field name="report_name" position="before">
<field name="partner_to" position="after">
<field name="body_type" />
</field>
</field>
Expand Down

0 comments on commit 4c4c80b

Please sign in to comment.