[IMP] email_template_qweb: black, isort

pull/1072/head
tfo 2020-01-02 11:23:57 +01:00 committed by Dũng (Trần Đình)
parent 17ba39bc6d
commit 5382bde82c
5 changed files with 33 additions and 45 deletions

View File

@ -3,21 +3,13 @@
{
"name": "QWeb for email templates",
"version": "12.0.1.0.0",
"author": "Therp BV, "
"Odoo Community Association (OCA)",
"author": "Therp BV, Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Marketing",
"summary": "Use the QWeb templating mechanism for emails",
'website': 'https://github.com/OCA/social',
"depends": [
'mail',
],
"demo": [
"demo/ir_ui_view.xml",
"demo/mail_template.xml",
],
"data": [
"views/mail_template.xml",
],
'installable': True,
"website": "https://github.com/OCA/social",
"depends": ["mail"],
"demo": ["demo/ir_ui_view.xml", "demo/mail_template.xml"],
"data": ["views/mail_template.xml"],
"installable": True,
}

View File

@ -1,17 +1,21 @@
# Copyright 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, tools, models
from odoo import api, fields, models, tools
class MailTemplate(models.Model):
_inherit = 'mail.template'
_inherit = "mail.template"
body_type = fields.Selection(
[('jinja2', 'Jinja2'), ('qweb', 'QWeb')], 'Body templating engine',
default='jinja2', required=True)
[("jinja2", "Jinja2"), ("qweb", "QWeb")],
"Body templating engine",
default="jinja2",
required=True,
)
body_view_id = fields.Many2one(
'ir.ui.view', 'Body view', domain=[('type', '=', 'qweb')])
body_view_arch = fields.Text(related='body_view_id.arch')
"ir.ui.view", "Body view", domain=[("type", "=", "qweb")]
)
body_view_arch = fields.Text(related="body_view_id.arch")
@api.multi
def generate_email(self, res_ids, fields=None):
@ -19,24 +23,18 @@ class MailTemplate(models.Model):
if isinstance(res_ids, int):
res_ids = [res_ids]
multi_mode = False
result = super(MailTemplate, self).generate_email(
res_ids, fields=fields
)
result = super(MailTemplate, self).generate_email(res_ids, fields=fields)
for res_id, template in self.get_email_template(res_ids).items():
if template.body_type == 'qweb' and\
(not fields or 'body_html' in fields):
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,
})
body_html = template.body_view_id.render(
{"object": record, "email_template": template}
)
# 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.render_post_process(
body_html
)
result[res_id]['body'] = tools.html_sanitize(
result[res_id]['body_html']
result[res_id]["body_html"] = self.render_post_process(body_html)
result[res_id]["body"] = tools.html_sanitize(
result[res_id]["body_html"]
)
return multi_mode and result or result[res_ids[0]]

View File

@ -5,21 +5,19 @@ 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')
template = self.env.ref("email_template_qweb.email_template_demo1")
mail_values = template.generate_email([self.env.user.id])
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']
)
"<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"]),
)
# the same method is also called in a non multi mode
mail_values = template.generate_email(self.env.user.id)
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["body_html"],
"Did not receive rendered template in response. Got: \n%s\n"
% (mail_values["body_html"]),
)