From 621917e6ed24cda2ef66ae2a9b28f871edeae000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Wed, 16 Jun 2021 14:44:10 +0200 Subject: [PATCH] [13.0][ADD] mail_template_multi_attachment This module allows to add more than 1 attachment (limitation of Odoo standard) to be generated during the email template render. These attachments are sent to the customer with the email's body --- mail_template_multi_attachment/README.rst | 88 ++++ mail_template_multi_attachment/__init__.py | 1 + .../__manifest__.py | 17 + .../models/__init__.py | 4 + .../models/mail_template.py | 59 +++ .../models/mail_template_report.py | 27 ++ .../readme/CONFIGURE.rst | 6 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 1 + .../readme/USAGE.rst | 1 + .../security/mail_template_report.xml | 23 + .../static/description/index.html | 432 ++++++++++++++++++ .../tests/__init__.py | 3 + .../tests/test_mail_template.py | 67 +++ .../views/mail_template.xml | 37 ++ .../views/mail_template_report.xml | 62 +++ .../addons/mail_template_multi_attachment | 1 + setup/mail_template_multi_attachment/setup.py | 6 + 18 files changed, 836 insertions(+) create mode 100644 mail_template_multi_attachment/README.rst create mode 100644 mail_template_multi_attachment/__init__.py create mode 100644 mail_template_multi_attachment/__manifest__.py create mode 100644 mail_template_multi_attachment/models/__init__.py create mode 100644 mail_template_multi_attachment/models/mail_template.py create mode 100644 mail_template_multi_attachment/models/mail_template_report.py create mode 100644 mail_template_multi_attachment/readme/CONFIGURE.rst create mode 100644 mail_template_multi_attachment/readme/CONTRIBUTORS.rst create mode 100644 mail_template_multi_attachment/readme/DESCRIPTION.rst create mode 100644 mail_template_multi_attachment/readme/USAGE.rst create mode 100644 mail_template_multi_attachment/security/mail_template_report.xml create mode 100644 mail_template_multi_attachment/static/description/index.html create mode 100644 mail_template_multi_attachment/tests/__init__.py create mode 100644 mail_template_multi_attachment/tests/test_mail_template.py create mode 100644 mail_template_multi_attachment/views/mail_template.xml create mode 100644 mail_template_multi_attachment/views/mail_template_report.xml create mode 120000 setup/mail_template_multi_attachment/odoo/addons/mail_template_multi_attachment create mode 100644 setup/mail_template_multi_attachment/setup.py diff --git a/mail_template_multi_attachment/README.rst b/mail_template_multi_attachment/README.rst new file mode 100644 index 000000000..144215a50 --- /dev/null +++ b/mail_template_multi_attachment/README.rst @@ -0,0 +1,88 @@ +============================== +Mail template multi attachment +============================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/13.0/mail_template_multi_attachment + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-13-0/social-13-0-mail_template_multi_attachment + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/205/13.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module add possibility to generate more than one attachment into your email template. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Just go on an email template and set attachments into "Attachments" tab. + +You can fill the "Simple attachment" if you need only 1 document (like Odoo standard). +You can also fill "Multi attachments" section if you need to add more document generation. + +You can fill only "Multi attachments" section and let empty "Simple attachment" if you want. + +Usage +===== + +You just have to use your email template like usual. New attachments will be generated automatically with the email content. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* François Honoré + +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 `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_template_multi_attachment/__init__.py b/mail_template_multi_attachment/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/mail_template_multi_attachment/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_template_multi_attachment/__manifest__.py b/mail_template_multi_attachment/__manifest__.py new file mode 100644 index 000000000..c17a8816d --- /dev/null +++ b/mail_template_multi_attachment/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Mail template multi attachment", + "summary": """Module who allows to generate multi attachments on + an email template.""", + "version": "13.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/social", + "depends": ["mail"], + "data": [ + "security/mail_template_report.xml", + "views/mail_template_report.xml", + "views/mail_template.xml", + ], +} diff --git a/mail_template_multi_attachment/models/__init__.py b/mail_template_multi_attachment/models/__init__.py new file mode 100644 index 000000000..ad0aed850 --- /dev/null +++ b/mail_template_multi_attachment/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import mail_template +from . import mail_template_report diff --git a/mail_template_multi_attachment/models/mail_template.py b/mail_template_multi_attachment/models/mail_template.py new file mode 100644 index 000000000..eeac56597 --- /dev/null +++ b/mail_template_multi_attachment/models/mail_template.py @@ -0,0 +1,59 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import base64 + +from odoo import _, exceptions, fields, models + + +class MailTemplate(models.Model): + _inherit = "mail.template" + + template_report_ids = fields.One2many( + comodel_name="mail.template.report", inverse_name="mail_template_id", + ) + + # pylint: disable=redefined-outer-name + def generate_email(self, res_ids, fields=None): + """ + Inherit to generate attachments. + Inspired from original mail.template,generate_email(...) from Odoo. + :param res_ids: int or list of int + :param fields: + :return: dict + """ + self.ensure_one() + multi_mode = True + results = super(MailTemplate, self).generate_email(res_ids, fields=fields) + if not self.template_report_ids: + return results + if isinstance(res_ids, int): + multi_mode = False + results = {res_ids: results} + # Generate attachments (inspired from Odoo); Just add new attachments + # into 'attachments' key + for res_id, values in results.items(): + attachments = values.setdefault("attachments", []) + for template_report in self.template_report_ids: + report_name = self._render_template( + template_report.report_name, template_report.model, res_id + ) + report = template_report.report_template_id + report_service = report.report_name + + if report.report_type in ["qweb-html", "qweb-pdf"]: + result, report_format = report.render_qweb_pdf([res_id]) + else: + res = report.render([res_id]) + if not res: + raise exceptions.UserError( + _("Unsupported report type %s found.") % report.report_type + ) + result, report_format = res + result = base64.b64encode(result) + if not report_name: + report_name = "report." + report_service + ext = "." + report_format + if not report_name.endswith(ext): + report_name += ext + attachments.append((report_name, result)) + return multi_mode and results or results[res_ids] diff --git a/mail_template_multi_attachment/models/mail_template_report.py b/mail_template_multi_attachment/models/mail_template_report.py new file mode 100644 index 000000000..19efd451c --- /dev/null +++ b/mail_template_multi_attachment/models/mail_template_report.py @@ -0,0 +1,27 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo import fields, models + + +class MailTemplateReport(models.Model): + """ + Model used to define dynamic report generation on email template + """ + + _name = "mail.template.report" + _description = "Mail template report" + + mail_template_id = fields.Many2one( + comodel_name="mail.template", + string="Mail template", + required=True, + ondelete="cascade", + ) + model = fields.Char(related="mail_template_id.model", store=True) + report_template_id = fields.Many2one( + comodel_name="ir.actions.report", + string="Report", + required=True, + ondelete="cascade", + ) + report_name = fields.Char(translate=True) diff --git a/mail_template_multi_attachment/readme/CONFIGURE.rst b/mail_template_multi_attachment/readme/CONFIGURE.rst new file mode 100644 index 000000000..976acf6f7 --- /dev/null +++ b/mail_template_multi_attachment/readme/CONFIGURE.rst @@ -0,0 +1,6 @@ +Just go on an email template and set attachments into "Attachments" tab. + +You can fill the "Simple attachment" if you need only 1 document (like Odoo standard). +You can also fill "Multi attachments" section if you need to add more document generation. + +You can fill only "Multi attachments" section and let empty "Simple attachment" if you want. diff --git a/mail_template_multi_attachment/readme/CONTRIBUTORS.rst b/mail_template_multi_attachment/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..ab1764c1c --- /dev/null +++ b/mail_template_multi_attachment/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* François Honoré diff --git a/mail_template_multi_attachment/readme/DESCRIPTION.rst b/mail_template_multi_attachment/readme/DESCRIPTION.rst new file mode 100644 index 000000000..8cfce7ef2 --- /dev/null +++ b/mail_template_multi_attachment/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module add possibility to generate more than one attachment into your email template. diff --git a/mail_template_multi_attachment/readme/USAGE.rst b/mail_template_multi_attachment/readme/USAGE.rst new file mode 100644 index 000000000..da0954826 --- /dev/null +++ b/mail_template_multi_attachment/readme/USAGE.rst @@ -0,0 +1 @@ +You just have to use your email template like usual. New attachments will be generated automatically with the email content. diff --git a/mail_template_multi_attachment/security/mail_template_report.xml b/mail_template_multi_attachment/security/mail_template_report.xml new file mode 100644 index 000000000..686112970 --- /dev/null +++ b/mail_template_multi_attachment/security/mail_template_report.xml @@ -0,0 +1,23 @@ + + + + + mail.template.report.system + + + + + + + + + mail.template.report.user + + + + + + + + diff --git a/mail_template_multi_attachment/static/description/index.html b/mail_template_multi_attachment/static/description/index.html new file mode 100644 index 000000000..3f4ccb8ab --- /dev/null +++ b/mail_template_multi_attachment/static/description/index.html @@ -0,0 +1,432 @@ + + + + + + +Mail template multi attachment + + + +
+

Mail template multi attachment

+ + +

Beta License: AGPL-3 OCA/social Translate me on Weblate Try me on Runbot

+

This module add possibility to generate more than one attachment into your email template.

+

Table of contents

+ +
+

Configuration

+

Just go on an email template and set attachments into “Attachments” tab.

+

You can fill the “Simple attachment” if you need only 1 document (like Odoo standard). +You can also fill “Multi attachments” section if you need to add more document generation.

+

You can fill only “Multi attachments” section and let empty “Simple attachment” if you want.

+
+
+

Usage

+

You just have to use your email template like usual. New attachments will be generated automatically with the email content.

+
+
+

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

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+
+

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.

+

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

+

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

+
+
+
+ + diff --git a/mail_template_multi_attachment/tests/__init__.py b/mail_template_multi_attachment/tests/__init__.py new file mode 100644 index 000000000..6302d7cd9 --- /dev/null +++ b/mail_template_multi_attachment/tests/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import test_mail_template diff --git a/mail_template_multi_attachment/tests/test_mail_template.py b/mail_template_multi_attachment/tests/test_mail_template.py new file mode 100644 index 000000000..f31c1203a --- /dev/null +++ b/mail_template_multi_attachment/tests/test_mail_template.py @@ -0,0 +1,67 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo.tests.common import SavepointCase + + +class TestMailTemplate(SavepointCase): + """ + Tests for mail.template + """ + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.MailTemplate = cls.env["mail.template"] + cls.partner = cls.env.ref("base.res_partner_12") + cls.report1 = cls.env.ref("base.report_ir_model_overview") + cls.report2 = cls.env.ref("base.report_ir_model_overview").copy( + {"name": cls.report1.name + " Test copy"} + ) + mail_tmpl_values = { + "name": "TestTemplate", + "subject": "About ${object.name}", + "body_html": "

Hello ${object.name}

", + "model_id": cls.env["ir.model"]._get(cls.report1.model).id, + "user_signature": False, + "report_template": cls.report1.id, + "report_name": "Report 1", + "template_report_ids": [ + ( + 0, + False, + {"report_template_id": cls.report2.id, "report_name": "Report 2"}, + ), + ], + } + cls.mail_template = cls.MailTemplate.create(mail_tmpl_values) + + def test_multi_generation1(self): + """ + Ensure number of attachment match with what's setup on mail template. + Don't check the content of the attachment, it's not the purpose + of this module. + :return: + """ + results = self.mail_template.generate_email(self.partner.id) + self.assertEqual(2, len(results.get("attachments"))) + + def test_multi_generation2(self): + """ + Ensure the mail generation (standard) still working even without + template_report_ids + :return: + """ + self.mail_template.write({"template_report_ids": [(6, False, [])]}) + results = self.mail_template.generate_email(self.partner.id) + self.assertEqual(1, len(results.get("attachments"))) + + def test_multi_generation3(self): + """ + Ensure the mail generation with only template_report_ids filled + works + :return: + """ + self.mail_template.write({"report_template": False, "report_name": False}) + results = self.mail_template.generate_email(self.partner.id) + self.assertEqual(1, len(results.get("attachments"))) diff --git a/mail_template_multi_attachment/views/mail_template.xml b/mail_template_multi_attachment/views/mail_template.xml new file mode 100644 index 000000000..ad57070dc --- /dev/null +++ b/mail_template_multi_attachment/views/mail_template.xml @@ -0,0 +1,37 @@ + + + + + mail.template.form (in mail_template_multi_attachment) + mail.template + + + + + + + + + + + + + + + + + + + + + + diff --git a/mail_template_multi_attachment/views/mail_template_report.xml b/mail_template_multi_attachment/views/mail_template_report.xml new file mode 100644 index 000000000..0409dabb8 --- /dev/null +++ b/mail_template_multi_attachment/views/mail_template_report.xml @@ -0,0 +1,62 @@ + + + + + + mail.template.report.form (in mail_template_multi_attachment) + + mail.template.report + +
+ + + + + + + + + + + +
+
+
+ + + mail.template.report.tree (in mail_template_multi_attachment) + + mail.template.report + + + + + + + + + + + + mail.template.report.tree (in mail_template_multi_attachment) + + mail.template.report + + + + + + + + +
diff --git a/setup/mail_template_multi_attachment/odoo/addons/mail_template_multi_attachment b/setup/mail_template_multi_attachment/odoo/addons/mail_template_multi_attachment new file mode 120000 index 000000000..56c80682e --- /dev/null +++ b/setup/mail_template_multi_attachment/odoo/addons/mail_template_multi_attachment @@ -0,0 +1 @@ +../../../../mail_template_multi_attachment \ No newline at end of file diff --git a/setup/mail_template_multi_attachment/setup.py b/setup/mail_template_multi_attachment/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mail_template_multi_attachment/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)