diff --git a/email_template_qweb/README.rst b/email_template_qweb/README.rst new file mode 100644 index 00000000..a7351572 --- /dev/null +++ b/email_template_qweb/README.rst @@ -0,0 +1,71 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +======================== +QWeb for email templates +======================== + +This module was written to allow you to write email templates in QWeb instead +of jinja2. The advantage here is that with QWeb, you can make use of +inheritance and the ``call`` statement, which allows you to reuse designs and +snippets in multiple templates, making your development process simpler. + +Usage +===== + +To use this module, you need to: + +* Select `QWeb` in the field `Body templating engine` +* Select a QWeb view to be used to render the body field +* Apart from QWeb's 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. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/149/8.0 + +Demo data contains an example on how to separate corporate identity from a +template's content. + +For further information, please visit: + +* https://www.odoo.com/forum/help-1 + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed feedback +`here `_. + +Credits +======= + +Contributors +------------ + +* Holger Brunn +* Dave Lasley + +Do not contact contributors directly about help with questions or problems concerning +this addon, but use the `forum `_, +the `community mailing list `_, +or the `appropriate specialized mailinglist `_ for help, +and the bug tracker linked in `Bug Tracker`_ above for technical issues. + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/email_template_qweb/__init__.py b/email_template_qweb/__init__.py new file mode 100644 index 00000000..7eda98a2 --- /dev/null +++ b/email_template_qweb/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/email_template_qweb/__openerp__.py b/email_template_qweb/__openerp__.py new file mode 100644 index 00000000..7fe4e9db --- /dev/null +++ b/email_template_qweb/__openerp__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "QWeb for email templates", + "version": "9.0.1.0.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Marketing", + "summary": "Use the QWeb templating mechanism for emails", + "depends": [ + 'mail', + ], + "demo": [ + "demo/ir_ui_view.xml", + "demo/mail_template.xml", + ], + "data": [ + "views/mail_template.xml", + ], +} diff --git a/email_template_qweb/demo/ir_ui_view.xml b/email_template_qweb/demo/ir_ui_view.xml new file mode 100644 index 00000000..bf7bff5d --- /dev/null +++ b/email_template_qweb/demo/ir_ui_view.xml @@ -0,0 +1,39 @@ + + + + + diff --git a/email_template_qweb/demo/mail_template.xml b/email_template_qweb/demo/mail_template.xml new file mode 100644 index 00000000..d53b6aac --- /dev/null +++ b/email_template_qweb/demo/mail_template.xml @@ -0,0 +1,10 @@ + + + + QWeb demo + qweb + + + QWeb demo email + + diff --git a/email_template_qweb/models/__init__.py b/email_template_qweb/models/__init__.py new file mode 100644 index 00000000..ed59ea76 --- /dev/null +++ b/email_template_qweb/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import mail_template diff --git a/email_template_qweb/models/mail_template.py b/email_template_qweb/models/mail_template.py new file mode 100644 index 00000000..978e3bf2 --- /dev/null +++ b/email_template_qweb/models/mail_template.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp import api, fields, tools, models + + +class MailTemplate(models.Model): + _inherit = 'mail.template' + + body_type = fields.Selection( + [('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']) + + @api.multi + def generate_email(self, res_ids, fields=None): + result = super(MailTemplate, self).generate_email( + res_ids, fields=fields + ) + for record_id, this in self.get_email_template(res_ids).iteritems(): + if this.body_type == 'qweb' and\ + (not fields or 'body_html' in fields): + for record in self.env[this.model].browse(record_id): + result[record_id]['body_html'] = self.render_post_process( + this.body_view_id.render({ + 'object': record, + 'email_template': this, + }) + ) + result[record_id]['body'] = tools.html_sanitize( + result[record_id]['body_html'] + ) + return result diff --git a/email_template_qweb/static/description/icon.png b/email_template_qweb/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/email_template_qweb/static/description/icon.png differ diff --git a/email_template_qweb/tests/__init__.py b/email_template_qweb/tests/__init__.py new file mode 100644 index 00000000..4e89ae38 --- /dev/null +++ b/email_template_qweb/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import test_mail_template_qweb diff --git a/email_template_qweb/tests/test_mail_template_qweb.py b/email_template_qweb/tests/test_mail_template_qweb.py new file mode 100644 index 00000000..c80773e8 --- /dev/null +++ b/email_template_qweb/tests/test_mail_template_qweb.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp.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]) + self.assertTrue( + # this comes from the called template if everything worked + '