diff --git a/mail_template_conditional_attachment/README.rst b/mail_template_conditional_attachment/README.rst new file mode 100644 index 000000000..990643447 --- /dev/null +++ b/mail_template_conditional_attachment/README.rst @@ -0,0 +1,99 @@ +===================================== +Email Template Conditional Attachment +===================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:e946bc852d29c20d8537f93a45fe27469ce18f4e39ae3c4f585c591468ad14a5 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fmail-lightgray.png?logo=github + :target: https://github.com/OCA/mail/tree/18.0/mail_template_conditional_attachment + :alt: OCA/mail +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/mail-18-0/mail-18-0-mail_template_conditional_attachment + :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/mail&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Allows to add conditional attachments to email templates. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use conditional attachments, you need to: + +1. Go to the corresponding Email Template record. +2. You should see the Conditional Attachments under Advance Settings +3. Create a new conditional attachment, and select the domain condition. + +You can attach multiple files under the same condition. + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* La Louve +* Druidoo + +Contributors +------------ + +- Iván Todorovich (https://www.druidoo.io) +- `Trobz `__ + + - Phan Hong Phuc + +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. + +.. |maintainer-ivantodorovich| image:: https://github.com/ivantodorovich.png?size=40px + :target: https://github.com/ivantodorovich + :alt: ivantodorovich + +Current `maintainer `__: + +|maintainer-ivantodorovich| + +This module is part of the `OCA/mail `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_template_conditional_attachment/__init__.py b/mail_template_conditional_attachment/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/mail_template_conditional_attachment/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_template_conditional_attachment/__manifest__.py b/mail_template_conditional_attachment/__manifest__.py new file mode 100644 index 000000000..9e91979a9 --- /dev/null +++ b/mail_template_conditional_attachment/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2016 La Louve +# Copyright 2019 Druidoo +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Email Template Conditional Attachment", + "summary": "Allow to add conditional attachments to email templates", + "version": "18.0.1.0.0", + "category": "Uncategorized", + "website": "https://github.com/OCA/mail", + "author": "La Louve, Druidoo, Odoo Community Association (OCA)", + "maintainers": ["ivantodorovich"], + "license": "AGPL-3", + "depends": [ + "mail", + ], + "data": [ + "views/mail_template.xml", + "security/ir.model.access.csv", + "security/mail_template_condition_attachment_security.xml", + ], +} diff --git a/mail_template_conditional_attachment/i18n/mail_template_conditional_attachment.pot b/mail_template_conditional_attachment/i18n/mail_template_conditional_attachment.pot new file mode 100644 index 000000000..27c780d47 --- /dev/null +++ b/mail_template_conditional_attachment/i18n/mail_template_conditional_attachment.pot @@ -0,0 +1,97 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_template_conditional_attachment +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-11-06 05:00+0000\n" +"PO-Revision-Date: 2025-11-06 05:00+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: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__filter_domain +msgid "Apply on" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__attachment_ids +msgid "Attachments" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template__conditional_attachment_ids +msgid "Conditional Attachment" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model_terms:ir.ui.view,arch_db:mail_template_conditional_attachment.email_template_form +msgid "Conditional Attachments" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__create_uid +msgid "Created by" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__create_date +msgid "Created on" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__name +msgid "Description" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__display_name +msgid "Display Name" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model,name:mail_template_conditional_attachment.model_mail_template +msgid "Email Templates" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__id +msgid "ID" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,help:mail_template_conditional_attachment.field_mail_template_conditional_attachment__filter_domain +msgid "" +"If present, this condition must be satisfied to include the attachments." +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__write_date +msgid "Last Updated on" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__mail_template_id +msgid "Mail Template" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model,name:mail_template_conditional_attachment.model_mail_template_conditional_attachment +msgid "Mail Template Conditional Attachment" +msgstr "" + +#. module: mail_template_conditional_attachment +#: model:ir.model.fields,field_description:mail_template_conditional_attachment.field_mail_template_conditional_attachment__model_name +msgid "Model" +msgstr "" diff --git a/mail_template_conditional_attachment/models/__init__.py b/mail_template_conditional_attachment/models/__init__.py new file mode 100644 index 000000000..277ff4f78 --- /dev/null +++ b/mail_template_conditional_attachment/models/__init__.py @@ -0,0 +1,2 @@ +from . import mail_template +from . import mail_template_conditional_attachment diff --git a/mail_template_conditional_attachment/models/mail_template.py b/mail_template_conditional_attachment/models/mail_template.py new file mode 100644 index 000000000..c439501c0 --- /dev/null +++ b/mail_template_conditional_attachment/models/mail_template.py @@ -0,0 +1,25 @@ +from odoo import fields, models + + +class MailTemplate(models.Model): + _inherit = "mail.template" + + conditional_attachment_ids = fields.One2many( + "mail.template.conditional.attachment", + "mail_template_id", + ) + + def _generate_template_attachments( + self, res_ids, render_fields, render_results=None + ): + render_results = super()._generate_template_attachments( + res_ids, render_fields, render_results + ) + + if self.conditional_attachment_ids: + for res_id in res_ids: + attachment_ids = self.conditional_attachment_ids.get_attachment_ids( + res_id + ) + render_results[res_id]["attachment_ids"] = attachment_ids.ids + return render_results diff --git a/mail_template_conditional_attachment/models/mail_template_conditional_attachment.py b/mail_template_conditional_attachment/models/mail_template_conditional_attachment.py new file mode 100644 index 000000000..0d68b0340 --- /dev/null +++ b/mail_template_conditional_attachment/models/mail_template_conditional_attachment.py @@ -0,0 +1,54 @@ +from odoo import fields, models +from odoo.tools.safe_eval import datetime, dateutil, safe_eval, time + + +class MailTemplateConditionalAttachment(models.Model): + _name = "mail.template.conditional.attachment" + _description = "Mail Template Conditional Attachment" + + name = fields.Char("Description", required=True) + + mail_template_id = fields.Many2one( + "mail.template", + ondelete="cascade", + required=True, + index=True, + ) + + attachment_ids = fields.Many2many( + "ir.attachment", + string="Attachments", + ) + + model_name = fields.Char(related="mail_template_id.model_id.model") + + filter_domain = fields.Char( + "Apply on", + help="If present, this condition must be satisfied " + "to include the attachments.", + ) + + def _get_eval_context(self): + return { + "datetime": datetime, + "dateutil": dateutil, + "time": time, + "uid": self.env.uid, + "user": self.env.user, + } + + def _check_condition(self, res_id): + self.ensure_one() + if self.filter_domain: + domain = [("id", "=", res_id)] + domain += safe_eval(self.filter_domain, self._get_eval_context()) + model = self.env[self.model_name] + return bool(model.search_count(domain)) + else: + return True + + def get_attachment_ids(self, res_id): + self.mapped("mail_template_id").ensure_one() + return self.filtered(lambda r: r._check_condition(res_id)).mapped( + "attachment_ids" + ) diff --git a/mail_template_conditional_attachment/pyproject.toml b/mail_template_conditional_attachment/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/mail_template_conditional_attachment/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/mail_template_conditional_attachment/readme/CONTRIBUTORS.md b/mail_template_conditional_attachment/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..ab0d0d463 --- /dev/null +++ b/mail_template_conditional_attachment/readme/CONTRIBUTORS.md @@ -0,0 +1,4 @@ +- Iván Todorovich \ + () +- [Trobz](https://www.trobz.com) + - Phan Hong Phuc \ diff --git a/mail_template_conditional_attachment/readme/DESCRIPTION.md b/mail_template_conditional_attachment/readme/DESCRIPTION.md new file mode 100644 index 000000000..253440fdc --- /dev/null +++ b/mail_template_conditional_attachment/readme/DESCRIPTION.md @@ -0,0 +1 @@ +Allows to add conditional attachments to email templates. diff --git a/mail_template_conditional_attachment/readme/USAGE.md b/mail_template_conditional_attachment/readme/USAGE.md new file mode 100644 index 000000000..3880dd69a --- /dev/null +++ b/mail_template_conditional_attachment/readme/USAGE.md @@ -0,0 +1,8 @@ +To use conditional attachments, you need to: + +1. Go to the corresponding Email Template record. +2. You should see the Conditional Attachments under Advance Settings +3. Create a new conditional attachment, and select the domain + condition. + +You can attach multiple files under the same condition. diff --git a/mail_template_conditional_attachment/security/ir.model.access.csv b/mail_template_conditional_attachment/security/ir.model.access.csv new file mode 100644 index 000000000..bc1a8e982 --- /dev/null +++ b/mail_template_conditional_attachment/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_mail_template_conditional_attachment_user,mail.template.conditional.attachment.user,model_mail_template_conditional_attachment,base.group_user,1,1,1,1 diff --git a/mail_template_conditional_attachment/security/mail_template_condition_attachment_security.xml b/mail_template_conditional_attachment/security/mail_template_condition_attachment_security.xml new file mode 100644 index 000000000..3b6e85f55 --- /dev/null +++ b/mail_template_conditional_attachment/security/mail_template_condition_attachment_security.xml @@ -0,0 +1,35 @@ + + + + Employees can only modify template condition attachment they have created + + [ + '|', + ('mail_template_id.create_uid', '=', user.id), + ('mail_template_id.user_id', '=', user.id) + ] + + + + + + + + + Mail Template Condition Attachment Editors - Edit All Templates Condition + + [(1, '=', 1)] + + + + + + + diff --git a/mail_template_conditional_attachment/static/description/icon.png b/mail_template_conditional_attachment/static/description/icon.png new file mode 100644 index 000000000..fd60220fb Binary files /dev/null and b/mail_template_conditional_attachment/static/description/icon.png differ diff --git a/mail_template_conditional_attachment/static/description/index.html b/mail_template_conditional_attachment/static/description/index.html new file mode 100644 index 000000000..1a0571125 --- /dev/null +++ b/mail_template_conditional_attachment/static/description/index.html @@ -0,0 +1,441 @@ + + + + + +Email Template Conditional Attachment + + + +
+

Email Template Conditional Attachment

+ + +

Beta License: AGPL-3 OCA/mail Translate me on Weblate Try me on Runboat

+

Allows to add conditional attachments to email templates.

+

Table of contents

+ +
+

Usage

+

To use conditional attachments, you need to:

+
    +
  1. Go to the corresponding Email Template record.
  2. +
  3. You should see the Conditional Attachments under Advance Settings
  4. +
  5. Create a new conditional attachment, and select the domain condition.
  6. +
+

You can attach multiple files under the same condition.

+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • La Louve
  • +
  • Druidoo
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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.

+

Current maintainer:

+

ivantodorovich

+

This module is part of the OCA/mail project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mail_template_conditional_attachment/tests/__init__.py b/mail_template_conditional_attachment/tests/__init__.py new file mode 100644 index 000000000..aaca6f3da --- /dev/null +++ b/mail_template_conditional_attachment/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_template_conditional_attachment diff --git a/mail_template_conditional_attachment/tests/test_mail_template_conditional_attachment.py b/mail_template_conditional_attachment/tests/test_mail_template_conditional_attachment.py new file mode 100644 index 000000000..39b16efb3 --- /dev/null +++ b/mail_template_conditional_attachment/tests/test_mail_template_conditional_attachment.py @@ -0,0 +1,83 @@ +from odoo.addons.mail.tests.test_mail_template import TestMailTemplate + + +class TestMailTemplateConditionalAttachment(TestMailTemplate): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.attachment_1 = cls.env["ir.attachment"].create( + { + "name": "Attachment 1", + "type": "binary", + "datas": "dGVzdA==", # base64 for 'test' + "mimetype": "text/plain", + } + ) + cls.attachment_2 = cls.env["ir.attachment"].create( + { + "name": "Attachment 2", + "type": "binary", + "datas": "dGVzdDI=", # base64 for 'test2' + "mimetype": "text/plain", + } + ) + cls.conditional_attachment_1 = cls.env[ + "mail.template.conditional.attachment" + ].create( + { + "name": "Conditional Attachment 1", + "mail_template_id": cls.mail_template.id, + "attachment_ids": [(6, 0, [cls.attachment_1.id])], + } + ) + cls.conditional_attachment_2 = cls.env[ + "mail.template.conditional.attachment" + ].create( + { + "name": "Conditional Attachment 2", + "mail_template_id": cls.mail_template.id, + "attachment_ids": [(6, 0, [cls.attachment_2.id])], + } + ) + + def test_01_conditional_attachment_no_condition(self): + """Test that attachments are added when no condition is set.""" + attachments = self.conditional_attachment_1.get_attachment_ids( + self.user_employee.partner_id.id + ) + self.assertIn(self.attachment_1, attachments) + attachments = self.conditional_attachment_2.get_attachment_ids( + self.user_employee.partner_id.id + ) + self.assertIn(self.attachment_2, attachments) + + def test_02_conditional_attachment_with_condition(self): + """Test that attachments are conditionally added based on the domain.""" + self.conditional_attachment_1.filter_domain = "[('is_company', '=', True)]" + self.conditional_attachment_2.filter_domain = "[('is_company', '=', False)]" + + # The partner is not a company, so only attachment 2 should be added + attachments = self.conditional_attachment_1.get_attachment_ids( + self.user_employee.partner_id.id + ) + self.assertNotIn(self.attachment_1, attachments) + + attachments = self.conditional_attachment_2.get_attachment_ids( + self.user_employee.partner_id.id + ) + self.assertIn(self.attachment_2, attachments) + + # Change the partner to a company and test again + company_partner = self.env["res.partner"].create( + {"name": "Company Partner", "is_company": True} + ) + + attachments = self.conditional_attachment_1.get_attachment_ids( + company_partner.id + ) + self.assertIn(self.attachment_1, attachments) + + attachments = self.conditional_attachment_2.get_attachment_ids( + company_partner.id + ) + self.assertNotIn(self.attachment_2, attachments) diff --git a/mail_template_conditional_attachment/views/mail_template.xml b/mail_template_conditional_attachment/views/mail_template.xml new file mode 100644 index 000000000..bce55ab7f --- /dev/null +++ b/mail_template_conditional_attachment/views/mail_template.xml @@ -0,0 +1,43 @@ + + + + mail.template + + + + + + + + + + + +
+ +
+
+ + + + + +
+
+
+
+
+
+
+