-
-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] new module: mail_template_conditional_report
This module is used to add condition (domain) on dynamic reports during generation of mail.template. When the mail is generated (based on the mail template), all dynamic attachments are generated (by standard Odoo) then the domain is applied (based on the record) to know if the attachment should be dropped or not.
- Loading branch information
Showing
19 changed files
with
942 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
================================ | ||
Mail template conditional report | ||
================================ | ||
|
||
.. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! This file is generated by oca-gen-addon-readme !! | ||
!! changes will be overwritten. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! source digest: sha256:4bd805708cf352d692d761056951ab35cbddf507666a72f42fa2bb37b13c0586 | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
:target: https://odoo-community.org/page/development-status | ||
:alt: Beta | ||
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png | ||
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
:alt: License: AGPL-3 | ||
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github | ||
:target: https://github.com/OCA/social/tree/18.0/mail_template_conditional_report | ||
:alt: OCA/social | ||
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
:target: https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_template_conditional_report | ||
:alt: Translate me on Weblate | ||
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png | ||
:target: https://runboat.odoo-community.org/builds?repo=OCA/social&target_branch=18.0 | ||
:alt: Try me on Runboat | ||
|
||
|badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
||
This module has been developed to enable you to conditionally select the | ||
reports to be attached in an email template according to criteria in the | ||
email template subject. | ||
|
||
To do this, we've added to the mail.template object the ability to | ||
choose the ir.attchment that should be linked in the email template and | ||
the ability to set conditions for each document. | ||
|
||
**Table of contents** | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Use Cases / Context | ||
=================== | ||
|
||
This module is useful for customising the attachments you want to | ||
include according to the profile of the third party or the type of | ||
transaction. | ||
|
||
Example: you could have one reports per type of account.move | ||
(in_invoice, out_invoice,...). Instead of having one mail template per | ||
type of account.move, you could use the same and add the domain on the | ||
attachment to determine which should be generated. | ||
|
||
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 to smash it by providing a detailed and welcomed | ||
`feedback <https://github.com/OCA/social/issues/new?body=module:%20mail_template_conditional_report%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
------- | ||
|
||
* ACSONE SA/NV | ||
|
||
Contributors | ||
------------ | ||
|
||
- `Acsone <https://www.acsone.eu>`__: | ||
|
||
- François Honoré [email protected] | ||
|
||
Maintainers | ||
----------- | ||
|
||
This module is maintained by the OCA. | ||
|
||
.. image:: https://odoo-community.org/logo.png | ||
:alt: Odoo Community Association | ||
:target: https://odoo-community.org | ||
|
||
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. | ||
|
||
This module is part of the `OCA/social <https://github.com/OCA/social/tree/18.0/mail_template_conditional_report>`_ project on GitHub. | ||
|
||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import models | ||
from .hooks.pre_init_hook import _pre_init_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2025 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
{ | ||
"name": "Mail template conditional report", | ||
"summary": "Add a domain to print dynamic attachments on mail template", | ||
"version": "18.0.1.0.0", | ||
"category": "Mail", | ||
"website": "https://github.com/OCA/social", | ||
"author": "ACSONE SA/NV, Odoo Community Association (OCA)", | ||
"license": "AGPL-3", | ||
"installable": True, | ||
"depends": ["mail"], | ||
"data": [ | ||
"views/mail_template.xml", | ||
"security/mail_template_report.xml", | ||
], | ||
"demo": [ | ||
"demo/ir_actions_report.xml", | ||
"demo/mail_template.xml", | ||
], | ||
"pre_init_hook": "_pre_init_hook", | ||
} |
24 changes: 24 additions & 0 deletions
24
mail_template_conditional_report/demo/ir_actions_report.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- Copyright 2025 ACSONE SA/NV | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<odoo> | ||
<record id="report_ir_model_overview2" model="ir.actions.report"> | ||
<field name="name">Model Overview 2</field> | ||
<field name="model">ir.model</field> | ||
<field name="report_type">qweb-pdf</field> | ||
<field name="report_name">base.report_irmodeloverview</field> | ||
<field name="report_file">base.report_irmodeloverview</field> | ||
<field name="binding_model_id" ref="base.model_ir_model" /> | ||
<field name="binding_type">report</field> | ||
</record> | ||
|
||
<record id="report_ir_model_overview3" model="ir.actions.report"> | ||
<field name="name">Model Overview 3</field> | ||
<field name="model">ir.model</field> | ||
<field name="report_type">qweb-pdf</field> | ||
<field name="report_name">base.report_irmodeloverview</field> | ||
<field name="report_file">base.report_irmodeloverview</field> | ||
<field name="binding_model_id" ref="base.model_ir_model" /> | ||
<field name="binding_type">report</field> | ||
</record> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- Copyright 2025 ACSONE SA/NV | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<odoo> | ||
<record id="mail_template_demo_hello_world" model="mail.template"> | ||
<field name="name">Hello world</field> | ||
<field name="model_id" ref="base.model_ir_model" /> | ||
<field name="email_from" /> | ||
<field name="subject">You shouldn't receive this</field> | ||
<field name="body_html" type="html"> | ||
<div>But I think you receive it</div> | ||
</field> | ||
<field name="mail_template_report_ids"> | ||
<record id="mail_template_report_hello_world1" model="mail.template.report"> | ||
<field | ||
name="ir_actions_report_id" | ||
ref="base.report_ir_model_overview" | ||
/> | ||
<field name="filter_domain" /> | ||
</record> | ||
</field> | ||
</record> | ||
<record id="mail_template_demo_promote" model="mail.template"> | ||
<field name="name">Promotion</field> | ||
<field name="model_id" ref="base.model_ir_model" /> | ||
<field name="email_from" /> | ||
<field name="subject">You have been promoted 🥳</field> | ||
<field name="body_html" type="html"> | ||
<div | ||
>Sorry this email is not for you! But please continue your good job!💪</div> | ||
</field> | ||
<field name="mail_template_report_ids"> | ||
<record id="mail_template_report_promote1" model="mail.template.report"> | ||
<field | ||
name="ir_actions_report_id" | ||
ref="base.report_ir_model_overview" | ||
/> | ||
<field name="filter_domain">[(1, '=', 1)]</field> | ||
</record> | ||
<record id="mail_template_report_promote2" model="mail.template.report"> | ||
<field name="ir_actions_report_id" ref="report_ir_model_overview2" /> | ||
<field name="filter_domain">[(0, '=', 1)]</field> | ||
</record> | ||
<record id="mail_template_report_promote3" model="mail.template.report"> | ||
<field name="ir_actions_report_id" ref="report_ir_model_overview3" /> | ||
<field name="filter_domain">[(1, '=', 1)]</field> | ||
</record> | ||
</field> | ||
</record> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import pre_init_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2025 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo.tools.sql import drop_constraint | ||
|
||
|
||
def _pre_init_hook(env): | ||
drop_constraint( | ||
env.cr, | ||
"mail_template_ir_actions_report_rel", | ||
"mail_template_ir_actions_report_rel_pkey", | ||
) | ||
query = """ | ||
ALTER TABLE mail_template_ir_actions_report_rel | ||
ADD COLUMN id SERIAL PRIMARY KEY; | ||
""" | ||
env.cr.execute(query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import mail_template | ||
from . import mail_template_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2025 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo import fields, models | ||
|
||
|
||
class MailTemplate(models.Model): | ||
_inherit = "mail.template" | ||
|
||
mail_template_report_ids = fields.One2many( | ||
comodel_name="mail.template.report", | ||
inverse_name="mail_template_id", | ||
string="Dynamic reports", | ||
) | ||
|
||
def _generate_template_attachments( | ||
self, res_ids, render_fields, render_results=None | ||
): | ||
results = super()._generate_template_attachments( | ||
res_ids, render_fields, render_results=render_results | ||
) | ||
self._filter_generated_attachments(results) | ||
return results | ||
|
||
def _filter_generated_attachments(self, results): | ||
""" | ||
On given result, check the report should be removed or not. | ||
:param results: dict | ||
:return: | ||
""" | ||
for res_id in results.keys(): | ||
values = results.setdefault(res_id, {}) | ||
attachments = values.pop("attachments", []) | ||
validated_attachments = [] | ||
for template_report, attachment in zip( | ||
self.mail_template_report_ids, attachments, strict=True | ||
): | ||
record = self.env[template_report.model_name].browse(res_id) | ||
if record.filtered_domain(template_report._get_eval_domain()): | ||
validated_attachments.append(attachment) | ||
values.update({"attachments": validated_attachments}) |
66 changes: 66 additions & 0 deletions
66
mail_template_conditional_report/models/mail_template_report.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2025 ACSONE SA/NV (<http://acsone.eu>) | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
from odoo import fields, models | ||
from odoo.osv.expression import TRUE_DOMAIN | ||
from odoo.tools.safe_eval import datetime, safe_eval | ||
|
||
|
||
class MailTemplateReport(models.Model): | ||
""" | ||
Model used to represent existing relation between | ||
report (ir.actions.report) and mail template (mail.template) | ||
""" | ||
|
||
_name = "mail.template.report" | ||
_table = "mail_template_ir_actions_report_rel" | ||
_description = "Mail template report relation" | ||
|
||
mail_template_id = fields.Many2one( | ||
comodel_name="mail.template", | ||
string="Mail template", | ||
required=True, | ||
ondelete="cascade", | ||
) | ||
ir_actions_report_id = fields.Many2one( | ||
comodel_name="ir.actions.report", | ||
string="Report", | ||
required=True, | ||
ondelete="cascade", | ||
domain="[('model','=', model_name)]", | ||
) | ||
model_name = fields.Char( | ||
related="mail_template_id.model_id.model", | ||
store=True, | ||
) | ||
filter_domain = fields.Char( | ||
string="Domain", | ||
help="Filter/Domain to apply on record to print to know if " | ||
"the report must be added on the mail.\n" | ||
"Technically, dynamics reports are always generated and then removed " | ||
"from attachments if the record doesn't match to the domain.\n" | ||
"Computed fields can be used (because filtered_domain(...) is used).", | ||
default=False, | ||
) | ||
|
||
_sql_constraints = [ | ||
( | ||
"unique_template_report", | ||
"unique(mail_template_id, ir_actions_report_id)", | ||
"The relation between the mail template and the report already exists!", | ||
), | ||
] | ||
|
||
def _get_eval_domain(self): | ||
self.ensure_one() | ||
if not self.filter_domain: | ||
return TRUE_DOMAIN | ||
return safe_eval( | ||
self.filter_domain, | ||
{ | ||
"user": self.env.user.with_context({}), # pylint: disable=context-overridden | ||
"companies": self.env.companies, | ||
"company": self.env.company, | ||
"datetime": datetime, | ||
"context_today": fields.Date.context_today(self), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build-system] | ||
requires = ["whool"] | ||
build-backend = "whool.buildapi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This module is useful for customising the attachments you want to include according to the profile of the third party or the type of transaction. | ||
|
||
Example: you could have one reports per type of account.move (in_invoice, out_invoice,...). | ||
Instead of having one mail template per type of account.move, you could use the same and add the domain on the attachment to determine which should be generated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- [Acsone](https://www.acsone.eu): | ||
- François Honoré <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This module has been developed to enable you to conditionally select the reports to be attached in an email template according to criteria in the email template subject. | ||
|
||
To do this, we've added to the mail.template object the ability to choose the ir.attchment that should be linked in the email template and the ability to set conditions for each document. |
14 changes: 14 additions & 0 deletions
14
mail_template_conditional_report/security/mail_template_report.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- Copyright 2025 ACSONE SA/NV | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<odoo noupdate="1"> | ||
<record id="access_mail_template_report" model="ir.model.access"> | ||
<field name="name">access_mail_template_report</field> | ||
<field name="model_id" ref="model_mail_template_report" /> | ||
<field name="group_id" ref="base.group_user" /> | ||
<field name="perm_read">1</field> | ||
<field name="perm_write">1</field> | ||
<field name="perm_create">1</field> | ||
<field name="perm_unlink">1</field> | ||
</record> | ||
</odoo> |
Oops, something went wrong.