From 073f0a3b430e62fa1e963371dd49665178a07d94 Mon Sep 17 00:00:00 2001 From: dufresnedavid Date: Thu, 23 Jun 2016 11:48:35 -0400 Subject: [PATCH] [8.0] Add mail_template_multi_report --- email_template_multi_report/README.rst | 73 ++++++++++++++ email_template_multi_report/__init__.py | 5 + email_template_multi_report/__openerp__.py | 23 +++++ .../i18n/email_template_multi_report.pot | 88 +++++++++++++++++ email_template_multi_report/i18n/fr.po | 96 +++++++++++++++++++ .../models/__init__.py | 6 ++ .../models/email_template.py | 62 ++++++++++++ .../models/email_template_report_line.py | 36 +++++++ email_template_multi_report/tests/__init__.py | 5 + .../tests/test_email_template.py | 96 +++++++++++++++++++ .../views/email_template.xml | 21 ++++ 11 files changed, 511 insertions(+) create mode 100644 email_template_multi_report/README.rst create mode 100644 email_template_multi_report/__init__.py create mode 100644 email_template_multi_report/__openerp__.py create mode 100644 email_template_multi_report/i18n/email_template_multi_report.pot create mode 100644 email_template_multi_report/i18n/fr.po create mode 100644 email_template_multi_report/models/__init__.py create mode 100644 email_template_multi_report/models/email_template.py create mode 100644 email_template_multi_report/models/email_template_report_line.py create mode 100644 email_template_multi_report/tests/__init__.py create mode 100644 email_template_multi_report/tests/test_email_template.py create mode 100644 email_template_multi_report/views/email_template.xml diff --git a/email_template_multi_report/README.rst b/email_template_multi_report/README.rst new file mode 100644 index 00000000..596761b4 --- /dev/null +++ b/email_template_multi_report/README.rst @@ -0,0 +1,73 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +============================ +Email Template Multi Reports +============================ + +This module adds the option to generate more than one report in an email template. + +Also, this module adds the options to attach a report to the email depending on a condition. + +Configuration +============= + +To configure this module, you need to: + +1. Go to Settings -> Technical -> Email -> Templates +2. In the email template form view, go to the Advanced Settings tab. +3. Add extra reports in the Other Reports field. + +The other reports added to the template do not support legacy reports such as rml. +Only qweb reports are available for now. + +If the field Condition is filled, then the report will attached depending on +the evaluation of the condition. + +Usage +===== + +Open a email template wizard and select your template. Your extra reports are added automatically. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/{repo_id}/{branch} + + +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. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* David Dufresne + + +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_multi_report/__init__.py b/email_template_multi_report/__init__.py new file mode 100644 index 00000000..3e7cdd47 --- /dev/null +++ b/email_template_multi_report/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/email_template_multi_report/__openerp__.py b/email_template_multi_report/__openerp__.py new file mode 100644 index 00000000..13d432e3 --- /dev/null +++ b/email_template_multi_report/__openerp__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Email Template Multi Report', + 'version': '8.0.1.0.0', + 'category': 'Marketing', + 'summary': 'Multiple Reports in Email Templates', + 'author': 'Savoir-faire Linux', + 'maintainer': 'Savoir-faire Linux,Odoo Community Association (OCA)', + 'website': 'http://www.savoirfairelinux.com', + 'license': 'AGPL-3', + 'depends': [ + 'email_template', + ], + 'data': [ + 'views/email_template.xml', + ], + 'installable': True, + 'application': False, + 'auto_install': False, +} diff --git a/email_template_multi_report/i18n/email_template_multi_report.pot b/email_template_multi_report/i18n/email_template_multi_report.pot new file mode 100644 index 00000000..f5b23ff8 --- /dev/null +++ b/email_template_multi_report/i18n/email_template_multi_report.pot @@ -0,0 +1,88 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * email_template_multi_report +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-06-23 18:31+0000\n" +"PO-Revision-Date: 2016-06-23 18:31+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: email_template_multi_report +#: help:email.template.report.line,condition:0 +msgid "An expression evaluated to determine if the report is to be attached to the email. If blank, the report will always be attached." +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,condition:0 +msgid "Condition" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,create_uid:0 +msgid "Created by" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,create_date:0 +msgid "Created on" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,template_id:0 +msgid "Email Template" +msgstr "" + +#. module: email_template_multi_report +#: model:ir.model,name:email_template_multi_report.model_email_template_report_line +msgid "Email Template Report Line" +msgstr "" + +#. module: email_template_multi_report +#: model:ir.model,name:email_template_multi_report.model_email_template +msgid "Email Templates" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,id:0 +msgid "ID" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,write_uid:0 +msgid "Last Updated by" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,write_date:0 +msgid "Last Updated on" +msgstr "" + +#. module: email_template_multi_report +#: help:email.template.report.line,report_name:0 +msgid "Name to use for the generated report file (may contain placeholders)\n" +"The extension can be omitted and will then come from the report type." +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,report_template_id:0 +msgid "Optional report to print and attach" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template,report_line_ids:0 +msgid "Other Reports" +msgstr "" + +#. module: email_template_multi_report +#: field:email.template.report.line,report_name:0 +msgid "Report Filename" +msgstr "" + diff --git a/email_template_multi_report/i18n/fr.po b/email_template_multi_report/i18n/fr.po new file mode 100644 index 00000000..eb3c3738 --- /dev/null +++ b/email_template_multi_report/i18n/fr.po @@ -0,0 +1,96 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * email_template_multi_report +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-06-23 18:31+0000\n" +"PO-Revision-Date: 2016-06-23 14:35-0500\n" +"Last-Translator: David Dufresne \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: \n" +"X-Generator: Poedit 1.5.4\n" + +#. module: email_template_multi_report +#: help:email.template.report.line,condition:0 +msgid "" +"An expression evaluated to determine if the report is to be attached to the " +"email. If blank, the report will always be attached." +msgstr "" +"Une expression évaluée pour déterminer si le rapport doit être attaché au " +"courriel. Si vide, le rapport sera toujours attaché." + +#. module: email_template_multi_report +#: field:email.template.report.line,condition:0 +msgid "Condition" +msgstr "Condition" + +#. module: email_template_multi_report +#: field:email.template.report.line,create_uid:0 +msgid "Created by" +msgstr "Créé par" + +#. module: email_template_multi_report +#: field:email.template.report.line,create_date:0 +msgid "Created on" +msgstr "Créé le" + +#. module: email_template_multi_report +#: field:email.template.report.line,template_id:0 +msgid "Email Template" +msgstr "Modèle de courriel" + +#. module: email_template_multi_report +#: model:ir.model,name:email_template_multi_report.model_email_template_report_line +msgid "Email Template Report Line" +msgstr "Ligne de rapport de modèle de courriel" + +#. module: email_template_multi_report +#: model:ir.model,name:email_template_multi_report.model_email_template +msgid "Email Templates" +msgstr "Modèles de courriel" + +#. module: email_template_multi_report +#: field:email.template.report.line,id:0 +msgid "ID" +msgstr "ID" + +#. module: email_template_multi_report +#: field:email.template.report.line,write_uid:0 +msgid "Last Updated by" +msgstr "Mis-à-jour par" + +#. module: email_template_multi_report +#: field:email.template.report.line,write_date:0 +msgid "Last Updated on" +msgstr "Mis-à-jour le" + +#. module: email_template_multi_report +#: help:email.template.report.line,report_name:0 +msgid "" +"Name to use for the generated report file (may contain placeholders)\n" +"The extension can be omitted and will then come from the report type." +msgstr "" +"Nom à utiliser pour le fichier de rapport généré (peut contenir des " +"variables)\n" +"L'extension peut être omise: celle du rapport sera utilisée." + +#. module: email_template_multi_report +#: field:email.template.report.line,report_template_id:0 +msgid "Optional report to print and attach" +msgstr "Rapport facultatif à imprimer et à joindre" + +#. module: email_template_multi_report +#: field:email.template,report_line_ids:0 +msgid "Other Reports" +msgstr "Autres rapports" + +#. module: email_template_multi_report +#: field:email.template.report.line,report_name:0 +msgid "Report Filename" +msgstr "Nom du fichier du rapport" diff --git a/email_template_multi_report/models/__init__.py b/email_template_multi_report/models/__init__.py new file mode 100644 index 00000000..6a9108c6 --- /dev/null +++ b/email_template_multi_report/models/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import email_template +from . import email_template_report_line diff --git a/email_template_multi_report/models/email_template.py b/email_template_multi_report/models/email_template.py new file mode 100644 index 00000000..6f5d6fa7 --- /dev/null +++ b/email_template_multi_report/models/email_template.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 + +from openerp import api, fields, models +from openerp.tools.safe_eval import safe_eval + + +class EmailTemplate(models.Model): + + _inherit = 'email.template' + + report_line_ids = fields.One2many( + 'email.template.report.line', 'template_id', string='Other Reports') + + @api.model + def generate_email_batch(self, template_id, res_ids, fields=None): + results = super(EmailTemplate, self).generate_email_batch( + template_id, res_ids, fields=fields) + + template = self.browse(template_id) + + for report_line in template.report_line_ids: + + records = self.env[template.model_id.model].browse(res_ids) + + for rec in records: + condition = report_line.condition + + if condition and condition.strip(): + + condition_result = self.render_template( + condition, template.model, rec.id) + + if not condition_result or not safe_eval(condition_result): + continue + + report_name = self.render_template( + report_line.report_name, template.model, rec.id) + + report = report_line.report_template_id + report_service = report.report_name + + result = self.env['report'].get_pdf(rec, report_service) + report_format = 'pdf' + + result = base64.b64encode(result) + + if not report_name: + report_name = 'report.' + report_service + + ext = "." + report_format + + if not report_name.endswith(ext): + report_name += ext + + results[rec.id].setdefault('attachments', []) + results[rec.id]['attachments'].append((report_name, result)) + + return results diff --git a/email_template_multi_report/models/email_template_report_line.py b/email_template_multi_report/models/email_template_report_line.py new file mode 100644 index 00000000..669f5997 --- /dev/null +++ b/email_template_multi_report/models/email_template_report_line.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import fields, models + + +class EmailTemplateReportLine(models.Model): + """Email Template Report Line""" + + _name = 'email.template.report.line' + _description = __doc__ + + template_id = fields.Many2one( + 'email.template', string='Email Template' + ) + + report_name = fields.Char( + 'Report Filename', translate=True, + help="Name to use for the generated report " + "file (may contain placeholders)\n" + "The extension can be omitted and will then come from the report type." + ) + + condition = fields.Char( + 'Condition', + help="An expression evaluated to determine if the report is " + "to be attached to the email. If blank, the report will always be " + "attached." + ) + + report_template_id = fields.Many2one( + 'ir.actions.report.xml', + 'Optional report to print and attach', + domain=[('report_type', 'in', ['qweb-html', 'qweb-pdf'])], + ) diff --git a/email_template_multi_report/tests/__init__.py b/email_template_multi_report/tests/__init__.py new file mode 100644 index 00000000..cd3e453f --- /dev/null +++ b/email_template_multi_report/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_email_template diff --git a/email_template_multi_report/tests/test_email_template.py b/email_template_multi_report/tests/test_email_template.py new file mode 100644 index 00000000..94844e63 --- /dev/null +++ b/email_template_multi_report/tests/test_email_template.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +# © 2016 Savoir-faire Linux +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests import common + + +class TestEmailTemplate(common.TransactionCase): + + def setUp(self): + super(TestEmailTemplate, self).setUp() + + self.report_view = self.env['ir.ui.view'].create({ + 'name': 'test_report_template', + 'mode': 'primary', + 'type': 'qweb', + 'arch': """\ + + + + +
+
+
+
+
+ """ + }) + + model_data = self.env['ir.model.data'].create({ + 'module': 'email_template_multi_report', + 'model': 'ir.ui.view', + 'name': 'test_report_template', + 'res_id': self.report_view.id, + }) + + model_data.clear_caches() + + self.report = self.env['ir.actions.report.xml'].create({ + 'name': 'Test Report 1', + 'model': 'res.partner', + 'report_type': 'qweb-html', + 'report_name': 'email_template_multi_report.test_report_template', + }) + + self.template = self.env['email.template'].create({ + 'name': 'Test Email Template', + 'model_id': self.env.ref('base.model_res_partner').id, + 'report_line_ids': [(0, 0, { + 'report_name': '${object.name}', + 'report_template_id': self.report.id, + })] + }) + + self.partner = self.env['res.partner'].create({ + 'name': 'Test Partner', + 'customer': True, + }) + + def test_01_generate_email_batch(self): + res = self.env['email.template'].generate_email_batch( + self.template.id, [self.partner.id]) + + self.assertEquals(len(res[self.partner.id]['attachments']), 1) + + def test_02_generate_email_batch_with_standard_report(self): + self.template.write({ + 'report_name': '${object.name}', + 'report_template': self.report.id, + }) + + res = self.env['email.template'].generate_email_batch( + self.template.id, [self.partner.id]) + + self.assertEquals(len(res[self.partner.id]['attachments']), 2) + + def test_03_report_condition_true(self): + self.template.report_line_ids[0].write({ + 'condition': "${object.customer}", + }) + + res = self.env['email.template'].generate_email_batch( + self.template.id, [self.partner.id]) + + self.assertEquals(len(res[self.partner.id]['attachments']), 1) + + def test_04_report_condition_false(self): + self.template.report_line_ids[0].write({ + 'condition': "${object.supplier}", + }) + + res = self.env['email.template'].generate_email_batch( + self.template.id, [self.partner.id]) + + res[self.partner.id].setdefault('attachments', []) + self.assertEquals(len(res[self.partner.id]['attachments']), 0) diff --git a/email_template_multi_report/views/email_template.xml b/email_template_multi_report/views/email_template.xml new file mode 100644 index 00000000..939cc044 --- /dev/null +++ b/email_template_multi_report/views/email_template.xml @@ -0,0 +1,21 @@ + + + + + email.template.form + email.template + + + + + + + + + + + + + + +