[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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user