diff --git a/mail_notification_email_template/README.rst b/mail_notification_email_template/README.rst new file mode 100644 index 00000000..cc8bd643 --- /dev/null +++ b/mail_notification_email_template/README.rst @@ -0,0 +1,71 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +==================================== +Use email templates in notifications +==================================== + +This module was written to allow you to send notification rendered with an +email template. + +Configuration +============= + +To configure this module, you need to: + +* go to Settings/Technical/Email/Subtypes +* open the subtype for which you want to change the notification +* select an email template +* within the email template, ``object`` refers to the notification, + from there you have access to the message the notification is for via + ``${object.message_id}``, to the partner who is to be notified via + ``${object.partner_id}`` and indirectly to the record the message is + about via ``${object.record}``. To generate links to the record, use + ``${object.record_access_link}``. + +Also have a look at the template assigned by default to the discussion subtype +for the minimal fields you should set. + +Usage +===== + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/205/8.0 + +For further information, please visit: + +* https://www.odoo.com/forum/help-1 + +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 +`here `_. + +Credits +======= + +Contributors +------------ + +* Holger Brunn + +Do not contact contributors directly about help with questions or problems concerning this addon, but use the `forum `_, the `community mailing list `_ or the `appropriate specialized mailinglist `_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues. + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/mail_notification_email_template/__init__.py b/mail_notification_email_template/__init__.py new file mode 100644 index 00000000..7eda98a2 --- /dev/null +++ b/mail_notification_email_template/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/mail_notification_email_template/__openerp__.py b/mail_notification_email_template/__openerp__.py new file mode 100644 index 00000000..ef2941e2 --- /dev/null +++ b/mail_notification_email_template/__openerp__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Use email templates in notifications", + "version": "8.0.1.0.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Social Network", + "summary": "Allows to configure message subtypes with mail templates", + "depends": [ + 'mail', + 'email_template', + ], + "data": [ + "data/email_template.xml", + "data/mail_message_subtype.xml", + "views/mail_message_subtype.xml", + ], +} diff --git a/mail_notification_email_template/data/email_template.xml b/mail_notification_email_template/data/email_template.xml new file mode 100644 index 00000000..feac86da --- /dev/null +++ b/mail_notification_email_template/data/email_template.xml @@ -0,0 +1,18 @@ + + + + + Template for discussion notifications + + ${object.message_id.subject|safe} + Dear ${object.partner_id.name}, +

there's a new message on ${object.record.name}:

+ ${object.message_id.body|safe} + ]]> +
+ ${object.message_id.email_from|safe} + ${object.partner_id.ids|join(',')} +
+
+
diff --git a/mail_notification_email_template/data/mail_message_subtype.xml b/mail_notification_email_template/data/mail_message_subtype.xml new file mode 100644 index 00000000..e944cf60 --- /dev/null +++ b/mail_notification_email_template/data/mail_message_subtype.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/mail_notification_email_template/models/__init__.py b/mail_notification_email_template/models/__init__.py new file mode 100644 index 00000000..9a9db255 --- /dev/null +++ b/mail_notification_email_template/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import mail_message_subtype +from . import mail_notification diff --git a/mail_notification_email_template/models/mail_message_subtype.py b/mail_notification_email_template/models/mail_message_subtype.py new file mode 100644 index 00000000..64d32d19 --- /dev/null +++ b/mail_notification_email_template/models/mail_message_subtype.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp import fields, models + + +class MailMessageSubtype(models.Model): + _inherit = 'mail.message.subtype' + + template_id = fields.Many2one( + 'email.template', string='Notification template', + domain=[('model_id.model', '=', 'mail.message.subtype')], + help='This template will be used to render notifications sent out ' + 'for this subtype') diff --git a/mail_notification_email_template/models/mail_notification.py b/mail_notification_email_template/models/mail_notification.py new file mode 100644 index 00000000..c6e20ec0 --- /dev/null +++ b/mail_notification_email_template/models/mail_notification.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from lxml import etree +from openerp import api, fields, models + + +class MailNotification(models.Model): + _inherit = 'mail.notification' + + record = fields.Reference( + selection=lambda self: [ + (m.model, m.name) for m in self.env['ir.model'].search([]) + ], + compute='_compute_record') + record_access_link = fields.Char(compute='_compute_record') + + @api.multi + def _notify_email(self, message_id, force_send=False, user_signature=True): + if not self.mapped('message_id.subtype_id.template_id'): + return super(MailNotification, self)._notify_email( + message_id, force_send=force_send, + user_signature=user_signature) + message_ids = [] + for this in self: + if not this.mapped('message_id.subtype_id.template_id'): + super(MailNotification, this)._notify_email( + message_id, force_send=force_send, + user_signature=user_signature) + continue + message = this.message_id + if not this.get_partners_to_email(message): + continue + custom_values = { + 'references': message.parent_id.message_id, + } + if message.res_id and hasattr( + self.env[message.model], 'message_get_email_values' + ): + message_values = self.env[message.model].browse( + message.res_id + ).message_get_email_values(message) + # message_get_email_values is guessed to @api.one + if message_values and isinstance(message_values, list): + message_values = message_values[0] + custom_values.update(message_values) + message_id = message.subtype_id.template_id.send_mail(this.id) + if 'mail_message_id' in custom_values: + custom_values.pop('mail_message_id') + self.env['mail.mail'].browse(message_id).write(custom_values) + message_ids.append(message_id) + return message_ids or True + + @api.multi + def _compute_record(self): + for this in self: + if not this.message_id.model or not this.message_id.res_id: + continue + this.record = self.env[this.message_id.model].browse( + this.message_id.res_id) + link_html = self.env['mail.mail']._get_partner_access_link( + self.env['mail.mail'].new({ + 'notification': True, + 'mail_message_id': this.message_id.id, + }), + this.partner_id + ) + for a in etree.HTML(link_html).xpath('//a[@href]'): + this.record_access_link = a.get('href') diff --git a/mail_notification_email_template/static/description/icon.png b/mail_notification_email_template/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/mail_notification_email_template/static/description/icon.png differ diff --git a/mail_notification_email_template/tests/__init__.py b/mail_notification_email_template/tests/__init__.py new file mode 100644 index 00000000..8fe97489 --- /dev/null +++ b/mail_notification_email_template/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import test_mail_notification_email_template diff --git a/mail_notification_email_template/tests/test_mail_notification_email_template.py b/mail_notification_email_template/tests/test_mail_notification_email_template.py new file mode 100644 index 00000000..f9a2edd5 --- /dev/null +++ b/mail_notification_email_template/tests/test_mail_notification_email_template.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp.tests.common import TransactionCase + + +class TestMailNotificationEmailTemplate(TransactionCase): + def test_mail_notification_email_template(self): + # we install a template for discussions, so we simply post + # something somewhere. We know the demo user is subscribed on the + # whole company group + demo_partner = self.env.ref('base.partner_demo') + demo_partner.write({'notify_email': 'always'}) + demo_partner_mails = self.env['mail.mail'].search([ + ('recipient_ids', '=', demo_partner.id), + ]) + self.env.ref('mail.group_all_employees').message_post( + body='hello world', type='comment', subtype='mail.mt_comment') + notifications = self.env['mail.mail'].search([ + ('recipient_ids', '=', demo_partner.id), + ]) - demo_partner_mails + self.assertTrue(notifications) + # check that our template was used + self.assertTrue('

Dear ' in n.body for n in notifications) diff --git a/mail_notification_email_template/views/mail_message_subtype.xml b/mail_notification_email_template/views/mail_message_subtype.xml new file mode 100644 index 00000000..ca32650c --- /dev/null +++ b/mail_notification_email_template/views/mail_message_subtype.xml @@ -0,0 +1,14 @@ + + + + + mail.message.subtype + + + + + + + + +