Skip to content
Snippets Groups Projects
Commit 40ae0f26f812 authored by oury.balde's avatar oury.balde
Browse files

:star: Add a new feature on the mail template

You can attach multiple documents as an attachment to the mail template.
Documents will not be regenerated should they have already been
generated beforehand and stored within a field of the object.
parent 18ae830abb44
No related branches found
Tags 13.0.1.0.1
No related merge requests found
......@@ -17,6 +17,8 @@
Define menus and actions should you want to allow changing document types:
For Example, you can attached a new document to an employee.
Also, this module adds the option to generate more than one document in an email template.
![Create](static/src/img/creerdocument.png)
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Document Attachment, for OpenERP
# Document Attachment, an Odoo module
# Copyright (C) 2013 XCG Consulting (http://odoo.consulting)
#
# This program is free software: you can redistribute it and/or modify
......@@ -35,8 +35,8 @@
"domain="[('res_id', '=', id)]" attribute.
""",
"website": "http://odoo.consulting/",
"depends": ["base", "document"],
"depends": ["base", "document", "mail"],
"data": [
"security/ir.model.access.csv",
"views/document_attachment.xml",
"views/ir_actions_report.xml",
......@@ -39,7 +39,8 @@
"data": [
"security/ir.model.access.csv",
"views/document_attachment.xml",
"views/ir_actions_report.xml",
"views/mail_template.xml",
],
"test": [],
"installable": True,
......
# flake8: noqa
from . import document_attachment, ir_actions_report
from . import document_attachment, ir_actions_report, mail_template
##############################################################################
#
# Document Attachment, an Odoo module
# Copyright (C) 2019 XCG Consulting (www.xcg-consulting.fr)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import base64
from odoo import _, api, exceptions, fields, models
from odoo.tools import pycompat
class MailTemplate(models.Model):
_inherit = "mail.template"
doc_to_attach_ids = fields.One2many(
comodel_name="mail.template.doc_to_attach",
inverse_name="template_id",
string="Documents to attach",
help=(
"Documents will not be regenerated should they have already been"
"generated beforehand and stored within a field of the object."
),
)
def _add_report_values(self, values, res_id, report_line):
# Set values[attachments] as default if key is not already in values.
values.setdefault("attachments", [])
record = self.env[self.model].browse(res_id)
# Ensure documents will not be regenerated should they
# have already been generated beforehand and stored within
# a field of the object.
docatt_id = report_line.get_doc_to_attach(record)
if docatt_id:
values["attachments"].append(
(docatt_id.file_id.name, docatt_id.file_id.datas)
)
return values
report_name = self.render_template(
report_line.report_name, self.model, res_id
)
report = report_line.report_template_id
report_service = report.report_name
result, fmt = report.render([res_id])
if result is None:
raise exceptions.UserError(
_("Unsupported report type %s found.") % report.report_type
)
result = base64.b64encode(result)
if not report_name:
report_name = "report." + report_service
ext = "." + fmt
if not report_name.endswith(ext):
report_name += ext
values["attachments"].append((report_name, result))
return values
@api.multi
def generate_email(self, res_ids, fields=None):
results = super(MailTemplate, self).generate_email(
res_ids, fields=fields
)
multi_mode = True
if isinstance(res_ids, pycompat.integer_types):
res_ids = [res_ids]
multi_mode = False
if multi_mode:
return {
res_id: self._add_report_values(values, res_id, report_line)
for res_id, values in results.items()
for report_line in self.doc_to_attach_ids
}
for report_line in self.doc_to_attach_ids:
results.update(
self._add_report_values(results, res_ids[0], report_line)
)
return results
class MailTemplateDocToAttach(models.Model):
_name = "mail.template.doc_to_attach"
template_id = fields.Many2one(
comodel_name="mail.template", string="Email Template"
)
report_name = fields.Char(
string="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.",
)
report_template_id = fields.Many2one(
comodel_name="ir.actions.report",
string=(
"Optional report to print and attach when the"
"document attachment field is not filled or empty in"
"the related object"
),
)
field_id = fields.Many2one(
comodel_name="ir.model.fields",
string="Field for document attachment",
help="Specify the field which contains the document attachment.",
)
@api.model
def get_doc_to_attach(self, record):
"""Helper func used to retrieve a document attachment according to
the given param.
"""
docatt = None
if not record:
return docatt
if self.field_id:
docatt = getattr(record, self.field_id.name)
if (
not docatt
and self.report_template_id
and self.report_template_id.docatt_field_id
):
field_id = self.report_template_id.docatt_field_id
docatt = getattr(record, field_id.name)
return docatt
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<record id="email_template_form" model="ir.ui.view">
<field name="name">email.template.form</field>
<field name="model">mail.template</field>
<field name="inherit_id" ref="mail.email_template_form"/>
<field name="arch" type="xml">
<field name="report_name" position="after">
<field name="doc_to_attach_ids">
<tree editable="bottom">
<field name="report_name"/>
<field name="field_id" domain="[('relation', '=', 'document_attachment')]"
options="{'no_create': True}"/>
<field name="report_template_id"/>
</tree>
</field>
</field>
</field>
</record>
</data>
</odoo>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment