diff options
| author | Thibault Delavallée <tde@odoo.com> | 2016-12-06 13:09:07 +0100 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2016-12-06 15:15:48 +0100 |
| commit | db72451607383cd8f4c6a674868a9f468ae828ef (patch) | |
| tree | 55c0f62635fdcbb1a05369438c3533dc5b7e4d38 | |
| parent | df037e295971650e8843b2ee668501e7aa3e7df6 (diff) | |
[FIX] mail: avoid notifying a message by email when it is already an email
When creating a mail.mail without giving a mail_message_id, a new mail.message
is created. It does then through _notify that notifies people of the message
creation. This leads to mail being sent two times, one when sending the
mail.mail, one when sending the notification.
This commits fixes that behavior. When creating directly a mail.mail without
giving a mail.message, call to _notify is bypassed. This way either an email
is created, either a message that has to be notified is created, but not both.
| -rw-r--r-- | addons/mail/models/mail_mail.py | 2 | ||||
| -rw-r--r-- | addons/mail/models/mail_message.py | 5 | ||||
| -rw-r--r-- | addons/mail/tests/test_mail_message.py | 17 |
3 files changed, 22 insertions, 2 deletions
diff --git a/addons/mail/models/mail_mail.py b/addons/mail/models/mail_mail.py index 7297c763953..26fe8e052e4 100644 --- a/addons/mail/models/mail_mail.py +++ b/addons/mail/models/mail_mail.py @@ -60,6 +60,8 @@ class MailMail(models.Model): # notification field: if not set, set if mail comes from an existing mail.message if 'notification' not in values and values.get('mail_message_id'): values['notification'] = True + if not values.get('mail_message_id'): + self = self.with_context(message_create_from_mail_mail=True) return super(MailMail, self).create(values) @api.multi diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 5529afd8955..c16b67a8d20 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -718,8 +718,9 @@ class Message(models.Model): message = super(Message, self).create(values) - message._notify(force_send=self.env.context.get('mail_notify_force_send', True), - user_signature=self.env.context.get('mail_notify_user_signature', True)) + if not self.env.context.get('message_create_from_mail_mail'): + message._notify(force_send=self.env.context.get('mail_notify_force_send', True), + user_signature=self.env.context.get('mail_notify_user_signature', True)) return message @api.multi diff --git a/addons/mail/tests/test_mail_message.py b/addons/mail/tests/test_mail_message.py index fb9da1f7ccb..cc68a1dd8bc 100644 --- a/addons/mail/tests/test_mail_message.py +++ b/addons/mail/tests/test_mail_message.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +import itertools + from odoo.addons.mail.tests.common import TestMail from odoo.exceptions import AccessError, except_orm from odoo.tools import mute_logger @@ -111,6 +113,21 @@ class TestMailMessage(TestMail): self.assertNotIn('mail.channel', msg.message_id) self.assertNotIn('-%d-' % self.group_pigs.id, msg.message_id) + def test_mail_message_notify_from_mail_mail(self): + # Due ot post-commit hooks, store send emails in every step + self.email_to_list = [] + mail = self.env['mail.mail'].create({ + 'body_html': '<p>Test</p>', + 'email_to': 'test@example.com', + 'partner_ids': [(4, self.user_employee.partner_id.id)] + }) + self.email_to_list.extend(itertools.chain.from_iterable(sent_email['email_to'] for sent_email in self._mails if sent_email.get('email_to'))) + self.assertNotIn(u'Ernest Employee <e.e@example.com>', self.email_to_list) + mail.send() + self.email_to_list.extend(itertools.chain.from_iterable(sent_email['email_to'] for sent_email in self._mails if sent_email.get('email_to'))) + self.assertNotIn(u'Ernest Employee <e.e@example.com>', self.email_to_list) + self.assertIn(u'test@example.com', self.email_to_list) + @mute_logger('odoo.addons.mail.models.mail_mail') def test_mail_message_access_search(self): # Data: various author_ids, partner_ids, documents |
