[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", "name": "QWeb for email templates",
"version": "12.0.1.0.0", "version": "12.0.1.0.0",
"author": "Therp BV, " "author": "Therp BV, Odoo Community Association (OCA)",
"Odoo Community Association (OCA)",
"license": "AGPL-3", "license": "AGPL-3",
"category": "Marketing", "category": "Marketing",
"summary": "Use the QWeb templating mechanism for emails", "summary": "Use the QWeb templating mechanism for emails",
'website': 'https://github.com/OCA/social', "website": "https://github.com/OCA/social",
"depends": [ "depends": ["mail"],
'mail', "demo": ["demo/ir_ui_view.xml", "demo/mail_template.xml"],
], "data": ["views/mail_template.xml"],
"demo": [ "installable": True,
"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> # Copyright 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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): class MailTemplate(models.Model):
_inherit = 'mail.template' _inherit = "mail.template"
body_type = fields.Selection( body_type = fields.Selection(
[('jinja2', 'Jinja2'), ('qweb', 'QWeb')], 'Body templating engine', [("jinja2", "Jinja2"), ("qweb", "QWeb")],
default='jinja2', required=True) "Body templating engine",
default="jinja2",
required=True,
)
body_view_id = fields.Many2one( body_view_id = fields.Many2one(
'ir.ui.view', 'Body view', domain=[('type', '=', 'qweb')]) "ir.ui.view", "Body view", domain=[("type", "=", "qweb")]
body_view_arch = fields.Text(related='body_view_id.arch') )
body_view_arch = fields.Text(related="body_view_id.arch")
@api.multi @api.multi
def generate_email(self, res_ids, fields=None): def generate_email(self, res_ids, fields=None):
@ -19,24 +23,18 @@ class MailTemplate(models.Model):
if isinstance(res_ids, int): if isinstance(res_ids, int):
res_ids = [res_ids] res_ids = [res_ids]
multi_mode = False multi_mode = False
result = super(MailTemplate, self).generate_email( result = super(MailTemplate, self).generate_email(res_ids, fields=fields)
res_ids, fields=fields
)
for res_id, template in self.get_email_template(res_ids).items(): for res_id, template in self.get_email_template(res_ids).items():
if template.body_type == 'qweb' and\ if template.body_type == "qweb" and (not fields or "body_html" in fields):
(not fields or 'body_html' in fields):
for record in self.env[template.model].browse(res_id): for record in self.env[template.model].browse(res_id):
body_html = template.body_view_id.render({ body_html = template.body_view_id.render(
'object': record, {"object": record, "email_template": template}
'email_template': template, )
})
# Some wizards, like when sending a sales order, need this # Some wizards, like when sending a sales order, need this
# fix to display accents correctly # fix to display accents correctly
body_html = tools.ustr(body_html) body_html = tools.ustr(body_html)
result[res_id]['body_html'] = self.render_post_process( result[res_id]["body_html"] = self.render_post_process(body_html)
body_html result[res_id]["body"] = tools.html_sanitize(
) result[res_id]["body_html"]
result[res_id]['body'] = tools.html_sanitize(
result[res_id]['body_html']
) )
return multi_mode and result or result[res_ids[0]] 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): class TestMailTemplateQweb(TransactionCase):
def test_email_template_qweb(self): 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]) mail_values = template.generate_email([self.env.user.id])
self.assertTrue( self.assertTrue(
# this comes from the called template if everything worked # this comes from the called template if everything worked
'<footer>' in 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' % ( "Did not receive rendered template in response. Got: \n%s\n"
mail_values[self.env.user.id]['body_html'] % (mail_values[self.env.user.id]["body_html"]),
)
) )
# the same method is also called in a non multi mode # the same method is also called in a non multi mode
mail_values = template.generate_email(self.env.user.id) mail_values = template.generate_email(self.env.user.id)
self.assertTrue( self.assertTrue(
# this comes from the called template if everything worked # this comes from the called template if everything worked
'<footer>' in mail_values['body_html'], "<footer>" in mail_values["body_html"],
'Did not receive rendered template in response. Got: \n%s\n' % ( "Did not receive rendered template in response. Got: \n%s\n"
mail_values['body_html'] % (mail_values["body_html"]),
)
) )