From 95d5020ff7c9a30702b1f88099a3078656be3cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 10 Apr 2018 14:00:14 +0200 Subject: [PATCH] [FIX] mail: raise a missing email error instead of crash when logging without email Currently when posting a message if the author has no email address an user error is raised. Indeed as posting a message goes through the process of creating notifications and sending emails an email address is required on the user. In saas 11.2 a _message_log shortcut has been added to log notes and in saas 11.3 a message_notify has been added to notify peopel of a custom content. Those methods should have the same behavior when author has no email. Currently there is a crash when trying to encode its email. After this commit an exceptions is raised like classic post. This commit is a manual forward-port of 3a978cc64e6b1d8aca7cfd5d4e319bcb302dd262 as well as an improvement to fix the newly-introduced message_notify method. --- addons/mail/models/mail_message.py | 2 +- addons/mail/models/mail_thread.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 740425bb75e..989167905fd 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -28,7 +28,7 @@ class Message(models.Model): def _get_default_from(self): if self.env.user.email: return formataddr((self.env.user.name, self.env.user.email)) - raise UserError(_("Unable to send email, please configure the sender's email address.")) + raise UserError(_("Unable to post message, please configure the sender's email address.")) @api.model def _get_default_author(self): diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 5cdc223cfad..9af7ea554ef 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1973,16 +1973,16 @@ class MailThread(models.AbstractModel): kw_author = kwargs.pop('author_id', False) if kw_author: author = self.env['res.partner'].sudo().browse(kw_author) - email_from = formataddr((author.name, author.email)) else: author = self.env.user.partner_id - email_from = formataddr((author.name, author.email)) + if not author.email: + raise exceptions.UserError(_("Unable to notify message, please configure the sender's email address.")) msg_values = { 'subject': subject, 'body': body, 'author_id': author.id, - 'email_from': email_from, + 'email_from': formataddr((author.name, author.email)), 'message_type': 'notification', 'partner_ids': partner_ids, 'model': False, @@ -2007,16 +2007,16 @@ class MailThread(models.AbstractModel): kw_author = kwargs.pop('author_id', False) if kw_author: author = self.env['res.partner'].sudo().browse(kw_author) - email_from = formataddr((author.name, author.email)) else: author = self.env.user.partner_id - email_from = formataddr((author.name, author.email)) + if not author.email: + raise exceptions.UserError(_("Unable to log message, please configure the sender's email address.")) message_values = { 'subject': subject, 'body': body, 'author_id': author.id, - 'email_from': email_from, + 'email_from': formataddr((author.name, author.email)), 'message_type': message_type, 'model': kwargs.get('model', self._name), 'res_id': self.ids[0] if self.ids else False,