Browse Source

[ADD] mail_notification_email_template (#54)

* [ADD] mail_notification_email_template

* [FIX] don't try to write null on mail_message_id

* [REN] move demo data to normal data

* [FIX] adapt comment to reality

* [FIX] cope with multiple mails being generated
pull/67/head
Holger Brunn 8 years ago
parent
commit
858d6a32aa
  1. 71
      mail_notification_email_template/README.rst
  2. 4
      mail_notification_email_template/__init__.py
  3. 20
      mail_notification_email_template/__openerp__.py
  4. 18
      mail_notification_email_template/data/email_template.xml
  5. 8
      mail_notification_email_template/data/mail_message_subtype.xml
  6. 5
      mail_notification_email_template/models/__init__.py
  7. 14
      mail_notification_email_template/models/mail_message_subtype.py
  8. 69
      mail_notification_email_template/models/mail_notification.py
  9. BIN
      mail_notification_email_template/static/description/icon.png
  10. 4
      mail_notification_email_template/tests/__init__.py
  11. 24
      mail_notification_email_template/tests/test_mail_notification_email_template.py
  12. 14
      mail_notification_email_template/views/mail_message_subtype.xml

71
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 <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 smashing it by providing a detailed and welcomed feedback
`here <https://github.com/OCA/social/issues/new?body=module:%20mail_notification_email_template%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Credits
=======
Contributors
------------
* Holger Brunn <hbrunn@therp.nl>
Do not contact contributors directly about help with questions or problems concerning this addon, but use the `forum <https://www.odoo.com/forum/help-1>`_, the `community mailing list <mailto:community@mail.odoo.com>`_ or the `appropriate specialized mailinglist <https://odoo-community.org/groups>`_ 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.

4
mail_notification_email_template/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import models

20
mail_notification_email_template/__openerp__.py

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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",
],
}

18
mail_notification_email_template/data/email_template.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data noupdate="1">
<record id="template" model="email.template">
<field name="name">Template for discussion notifications</field>
<field name="model_id" ref="mail.model_mail_notification" />
<field name="subject">${object.message_id.subject|safe}</field>
<field name="body_html"><![CDATA[
<h2>Dear ${object.partner_id.name},</h2>
<p>there's a new message on <a href="${object.record_access_link}">${object.record.name}</a>:</p>
${object.message_id.body|safe}
]]>
</field>
<field name="email_from">${object.message_id.email_from|safe}</field>
<field name="partner_to">${object.partner_id.ids|join(',')}</field>
</record>
</data>
</openerp>

8
mail_notification_email_template/data/mail_message_subtype.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="mail.mt_comment" model="mail.message.subtype">
<field name="template_id" ref="template" />
</record>
</data>
</openerp>

5
mail_notification_email_template/models/__init__.py

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import mail_message_subtype
from . import mail_notification

14
mail_notification_email_template/models/mail_message_subtype.py

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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')

69
mail_notification_email_template/models/mail_notification.py

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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')

BIN
mail_notification_email_template/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 9.2 KiB

4
mail_notification_email_template/tests/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import test_mail_notification_email_template

24
mail_notification_email_template/tests/test_mail_notification_email_template.py

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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('<h2>Dear ' in n.body for n in notifications)

14
mail_notification_email_template/views/mail_message_subtype.xml

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_mail_message_subtype_form" model="ir.ui.view">
<field name="model">mail.message.subtype</field>
<field name="inherit_id" ref="mail.view_mail_message_subtype_form" />
<field name="arch" type="xml">
<field name="description" position="after">
<field name="template_id" context="{'default_model_id': %(mail.model_mail_notification)d}" />
</field>
</field>
</record>
</data>
</openerp>
Loading…
Cancel
Save