Browse Source

Merge pull request #74 from savoirfairelinux/8.0_email_template_multi_report

[8.0] Add mail_template_multi_report
pull/77/head
Eric Caudal 9 years ago
committed by GitHub
parent
commit
88e92604fd
  1. 76
      mail_template_multi_report/README.rst
  2. 5
      mail_template_multi_report/__init__.py
  3. 23
      mail_template_multi_report/__openerp__.py
  4. 96
      mail_template_multi_report/i18n/fr.po
  5. 6
      mail_template_multi_report/models/__init__.py
  6. 57
      mail_template_multi_report/models/email_template.py
  7. 36
      mail_template_multi_report/models/email_template_report_line.py
  8. 3
      mail_template_multi_report/security/ir.model.access.csv
  9. 5
      mail_template_multi_report/tests/__init__.py
  10. 96
      mail_template_multi_report/tests/test_email_template.py
  11. 21
      mail_template_multi_report/views/email_template.xml

76
mail_template_multi_report/README.rst

@ -0,0 +1,76 @@
.. 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
============================
Mail 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.
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/social/8.0
Known issues / Roadmap
======================
The other reports added to the template do not support legacy reports such as rml.
Only qweb reports are available for now.
Bug Tracker
===========
Bugs are tracked on `GitHub Issues
<https://github.com/OCA/social/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 <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
Contributors
------------
* David Dufresne <david.dufresne@savoirfairelinux.com>
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.

5
mail_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

23
mail_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,Odoo Community Association (OCA)',
'website': 'http://www.savoirfairelinux.com',
'license': 'AGPL-3',
'depends': [
'email_template',
],
'data': [
'security/ir.model.access.csv',
'views/email_template.xml',
],
'installable': True,
'application': False,
'auto_install': False,
}

96
mail_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 <david.dufresne@savoirfairelinux.com>\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"

6
mail_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

57
mail_template_multi_report/models/email_template.py

@ -0,0 +1,57 @@
# -*- 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)
report_ext = '.pdf'
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)
result = base64.b64encode(result)
if not report_name:
report_name = 'report.' + report_service
if not report_name.endswith(report_ext):
report_name += report_ext
results[rec.id].setdefault('attachments', [])
results[rec.id]['attachments'].append((report_name, result))
return results

36
mail_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'])],
)

3
mail_template_multi_report/security/ir.model.access.csv

@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_email_template_report_line,email.template,model_email_template_report_line,base.group_user,1,1,1,0
access_email_template_report_line_system,email.template system,model_email_template_report_line,base.group_system,1,1,1,1

5
mail_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

96
mail_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': """\
<?xml version="1.0"?>
<t t-name="mail_template_multi_report.test_report_template">
<t t-call="report.html_container">
<t t-call="report.internal_layout">
<div class="page">
</div>
</t>
</t>
</t>
"""
})
model_data = self.env['ir.model.data'].create({
'module': 'mail_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': 'mail_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)

21
mail_template_multi_report/views/email_template.xml

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="email_template_form" model="ir.ui.view">
<field name="name">email.template.form</field>
<field name="model">email.template</field>
<field name="inherit_id" ref="email_template.email_template_form"/>
<field name="arch" type="xml">
<field name="report_name" position="after">
<field name="report_line_ids">
<tree editable="bottom">
<field name="report_name"/>
<field name="report_template_id"/>
<field name="condition"/>
</tree>
</field>
</field>
</field>
</record>
</data>
</openerp>
Loading…
Cancel
Save