Skip to content

Commit

Permalink
[ADD] new module: mail_template_conditional_report
Browse files Browse the repository at this point in the history
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
acsonefho committed Jan 3, 2025
1 parent 8c81513 commit 9e95147
Show file tree
Hide file tree
Showing 18 changed files with 904 additions and 0 deletions.
79 changes: 79 additions & 0 deletions mail_template_conditional_report/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
================================
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 is used to add a domain on the relation between mail
template and attachments.

**Table of contents**

.. contents::
:local:

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.
2 changes: 2 additions & 0 deletions mail_template_conditional_report/__init__.py
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
22 changes: 22 additions & 0 deletions mail_template_conditional_report/__manifest__.py
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 mail_template_conditional_report/demo/ir_actions_report.xml
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>
50 changes: 50 additions & 0 deletions mail_template_conditional_report/demo/mail_template.xml
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>
1 change: 1 addition & 0 deletions mail_template_conditional_report/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import pre_init_hook
16 changes: 16 additions & 0 deletions mail_template_conditional_report/hooks/pre_init_hook.py
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)
2 changes: 2 additions & 0 deletions mail_template_conditional_report/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import mail_template
from . import mail_template_report
40 changes: 40 additions & 0 deletions mail_template_conditional_report/models/mail_template.py
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 mail_template_conditional_report/models/mail_template_report.py
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),
},
)
3 changes: 3 additions & 0 deletions mail_template_conditional_report/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
3 changes: 3 additions & 0 deletions mail_template_conditional_report/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Acsone](https://www.acsone.eu):
- François Honoré <[email protected]>

1 change: 1 addition & 0 deletions mail_template_conditional_report/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module is used to add a domain on the relation between mail template and attachments.
14 changes: 14 additions & 0 deletions mail_template_conditional_report/security/mail_template_report.xml
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>
Loading

0 comments on commit 9e95147

Please sign in to comment.