From 6179be14c9a284d94fe95f27c37c356727095802 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Tue, 23 Jul 2019 17:45:19 +0200 Subject: [PATCH] [12.0][ADD] - new module: mail template substitute This module allows you to create substitution rules for mail templates. A typical use case is to replace a standard template by alternative templates when some conditions are met. For instance, it allows to configure alternate templates for different companies. --- mail_template_substitute/README.rst | 91 ++++ mail_template_substitute/__init__.py | 2 + mail_template_substitute/__manifest__.py | 19 + mail_template_substitute/models/__init__.py | 2 + .../models/mail_template.py | 41 ++ .../models/mail_template_substitution_rule.py | 24 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 4 + mail_template_substitute/readme/ROADMAP.rst | 0 mail_template_substitute/readme/USAGE.rst | 11 + .../mail_template_substitution_rule.xml | 16 + .../static/description/index.html | 434 ++++++++++++++++++ .../views/mail_template.xml | 44 ++ mail_template_substitute/wizards/__init__.py | 1 + .../wizards/mail_compose_message.py | 43 ++ .../odoo/addons/mail_template_substitute | 1 + setup/mail_template_substitute/setup.py | 6 + 17 files changed, 740 insertions(+) create mode 100644 mail_template_substitute/README.rst create mode 100644 mail_template_substitute/__init__.py create mode 100644 mail_template_substitute/__manifest__.py create mode 100644 mail_template_substitute/models/__init__.py create mode 100644 mail_template_substitute/models/mail_template.py create mode 100644 mail_template_substitute/models/mail_template_substitution_rule.py create mode 100644 mail_template_substitute/readme/CONTRIBUTORS.rst create mode 100644 mail_template_substitute/readme/DESCRIPTION.rst create mode 100644 mail_template_substitute/readme/ROADMAP.rst create mode 100644 mail_template_substitute/readme/USAGE.rst create mode 100644 mail_template_substitute/security/mail_template_substitution_rule.xml create mode 100644 mail_template_substitute/static/description/index.html create mode 100644 mail_template_substitute/views/mail_template.xml create mode 100644 mail_template_substitute/wizards/__init__.py create mode 100644 mail_template_substitute/wizards/mail_compose_message.py create mode 120000 setup/mail_template_substitute/odoo/addons/mail_template_substitute create mode 100644 setup/mail_template_substitute/setup.py diff --git a/mail_template_substitute/README.rst b/mail_template_substitute/README.rst new file mode 100644 index 000000000..3421c57b1 --- /dev/null +++ b/mail_template_substitute/README.rst @@ -0,0 +1,91 @@ +======================== +Mail Template Substitute +======================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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/12.0/mail_template_substitute + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-12-0/social-12-0-mail_template_substitute + :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/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows you to create substitution rules for mail templates. +A typical use case is to replace a standard template by alternative templates +when some conditions are met. For instance, it allows to configure alternate +templates for different companies. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +#. Go to 'Email' / 'Templates' + +#. Select the desired template you want to substitute + +#. In the substitutions page add a new line + +#. Select the substitution template + +#. Set a domain to specify when this substitution should happen + +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 +~~~~~~~~~~~~ + +* Bejaoui Souheil + +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_substitute/__init__.py b/mail_template_substitute/__init__.py new file mode 100644 index 000000000..aee8895e7 --- /dev/null +++ b/mail_template_substitute/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/mail_template_substitute/__manifest__.py b/mail_template_substitute/__manifest__.py new file mode 100644 index 000000000..5c23b9469 --- /dev/null +++ b/mail_template_substitute/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Mail Template Substitute", + "summary": """ + This module allows to create substitution rules for mail templates. + """, + "version": "12.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV," + "Odoo Community Association (OCA)", + "website": "https://github.com/acsone/social", + "depends": ["base", "mail"], + "data": [ + "security/mail_template_substitution_rule.xml", + "views/mail_template.xml", + ], +} diff --git a/mail_template_substitute/models/__init__.py b/mail_template_substitute/models/__init__.py new file mode 100644 index 000000000..09e1a8f27 --- /dev/null +++ b/mail_template_substitute/models/__init__.py @@ -0,0 +1,2 @@ +from . import mail_template +from . import mail_template_substitution_rule diff --git a/mail_template_substitute/models/mail_template.py b/mail_template_substitute/models/mail_template.py new file mode 100644 index 000000000..3229e0677 --- /dev/null +++ b/mail_template_substitute/models/mail_template.py @@ -0,0 +1,41 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models +from odoo.tools import pycompat +from odoo.tools.safe_eval import safe_eval + + +class MailTemplate(models.Model): + + _inherit = "mail.template" + + mail_template_substitution_rule_ids = fields.One2many( + comodel_name="mail.template.substitution.rule", + inverse_name="mail_template_id", + string="Substitution Rules", + ) + + @api.multi + def _get_substitution_template(self, model_id, active_ids): + self.ensure_one() + if isinstance(active_ids, pycompat.integer_types): + active_ids = [active_ids] + model = self.env[model_id.model] + for ( + substitution_template_rule + ) in self.mail_template_substitution_rule_ids: + domain = safe_eval(substitution_template_rule.domain) + domain.append(("id", "in", active_ids)) + if set(model.search(domain).ids) == set(active_ids): + return substitution_template_rule.substitution_mail_template_id + return False + + @api.multi + def get_email_template(self, res_ids): + substitution_template = self._get_substitution_template( + self.model_id, res_ids + ) + if substitution_template: + return substitution_template.get_email_template(res_ids) + return super().get_email_template(res_ids) diff --git a/mail_template_substitute/models/mail_template_substitution_rule.py b/mail_template_substitute/models/mail_template_substitution_rule.py new file mode 100644 index 000000000..0670493e6 --- /dev/null +++ b/mail_template_substitute/models/mail_template_substitution_rule.py @@ -0,0 +1,24 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class MailTemplateSubstitutionRule(models.Model): + + _name = "mail.template.substitution.rule" + _description = "Mail Template Substitution Rule" + _order = "sequence ASC" + + sequence = fields.Integer(default=10) + mail_template_id = fields.Many2one( + comodel_name="mail.template", required=True, ondelete="cascade" + ) + model = fields.Char(related="mail_template_id.model_id.model", store=True) + domain = fields.Char(string="Domain", required=True, default="[]") + substitution_mail_template_id = fields.Many2one( + comodel_name="mail.template", + required=True, + ondelete="cascade", + domain="[('model', '=', model)]", + ) diff --git a/mail_template_substitute/readme/CONTRIBUTORS.rst b/mail_template_substitute/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..35c03ffe0 --- /dev/null +++ b/mail_template_substitute/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Bejaoui Souheil diff --git a/mail_template_substitute/readme/DESCRIPTION.rst b/mail_template_substitute/readme/DESCRIPTION.rst new file mode 100644 index 000000000..008be85a5 --- /dev/null +++ b/mail_template_substitute/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +This module allows you to create substitution rules for mail templates. +A typical use case is to replace a standard template by alternative templates +when some conditions are met. For instance, it allows to configure alternate +templates for different companies. diff --git a/mail_template_substitute/readme/ROADMAP.rst b/mail_template_substitute/readme/ROADMAP.rst new file mode 100644 index 000000000..e69de29bb diff --git a/mail_template_substitute/readme/USAGE.rst b/mail_template_substitute/readme/USAGE.rst new file mode 100644 index 000000000..d762ef627 --- /dev/null +++ b/mail_template_substitute/readme/USAGE.rst @@ -0,0 +1,11 @@ +To use this module, you need to: + +#. Go to 'Email' / 'Templates' + +#. Select the desired template you want to substitute + +#. In the substitutions page add a new line + +#. Select the substitution template + +#. Set a domain to specify when this substitution should happen diff --git a/mail_template_substitute/security/mail_template_substitution_rule.xml b/mail_template_substitute/security/mail_template_substitution_rule.xml new file mode 100644 index 000000000..98b95eb2e --- /dev/null +++ b/mail_template_substitute/security/mail_template_substitution_rule.xml @@ -0,0 +1,16 @@ + + + + + + + mail.template.substitution.rule access + + + + + + + + diff --git a/mail_template_substitute/static/description/index.html b/mail_template_substitute/static/description/index.html new file mode 100644 index 000000000..91691a08b --- /dev/null +++ b/mail_template_substitute/static/description/index.html @@ -0,0 +1,434 @@ + + + + + + +Mail Template Substitute + + + +
+

Mail Template Substitute

+ + +

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

+

This module allows you to create substitution rules for mail templates. +A typical use case is to replace a standard template by alternative templates +when some conditions are met. For instance, it allows to configure alternate +templates for different companies.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to ‘Email’ / ‘Templates’
  2. +
  3. Select the desired template you want to substitute
  4. +
  5. In the substitutions page add a new line
  6. +
  7. Select the substitution template
  8. +
  9. Set a domain to specify when this substitution should happen
  10. +
+
+
+

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_substitute/views/mail_template.xml b/mail_template_substitute/views/mail_template.xml new file mode 100644 index 000000000..e243662e2 --- /dev/null +++ b/mail_template_substitute/views/mail_template.xml @@ -0,0 +1,44 @@ + + + + + + + mail.template.form (in mail_template_substitute) + + mail.template + + + + + + + + + + +
+ + + + + + + + + + +
+
+
+
+
+
+ + +
diff --git a/mail_template_substitute/wizards/__init__.py b/mail_template_substitute/wizards/__init__.py new file mode 100644 index 000000000..b528d997d --- /dev/null +++ b/mail_template_substitute/wizards/__init__.py @@ -0,0 +1 @@ +from . import mail_compose_message diff --git a/mail_template_substitute/wizards/mail_compose_message.py b/mail_template_substitute/wizards/mail_compose_message.py new file mode 100644 index 000000000..ecf952f11 --- /dev/null +++ b/mail_template_substitute/wizards/mail_compose_message.py @@ -0,0 +1,43 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class MailComposeMessage(models.TransientModel): + + _inherit = 'mail.compose.message' + + @api.model + def _get_substitution_template(self, composition_mode, template, res_ids): + if template: + if composition_mode == 'mass_mail' and self.env.context.get( + 'active_ids' + ): + res_ids = self.env.context.get('active_ids') + res_ids_to_templates = template.get_email_template(res_ids) + if res_ids_to_templates: + return list(res_ids_to_templates.values())[0] + return False + + @api.model + def default_get(self, fields): + result = super(MailComposeMessage, self).default_get(fields) + substitution_template = self._get_substitution_template( + result.get('composition_mode'), + self.env['mail.template'].browse(result.get('template_id')), + [result.get('res_id')], + ) + if substitution_template: + result['template_id'] = substitution_template.id + return result + + @api.multi + @api.onchange('template_id') + def onchange_template_id_wrapper(self): + substitution_template = self._get_substitution_template( + self.composition_mode, self.template_id, [self.res_id] + ) + if substitution_template: + self.template_id = substitution_template + return super().onchange_template_id_wrapper() diff --git a/setup/mail_template_substitute/odoo/addons/mail_template_substitute b/setup/mail_template_substitute/odoo/addons/mail_template_substitute new file mode 120000 index 000000000..dd6903e40 --- /dev/null +++ b/setup/mail_template_substitute/odoo/addons/mail_template_substitute @@ -0,0 +1 @@ +../../../../mail_template_substitute \ No newline at end of file diff --git a/setup/mail_template_substitute/setup.py b/setup/mail_template_substitute/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mail_template_substitute/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)