Files
odoo_source/addons/mail/models/mail_cc_mixin.py
T
Julien Castiaux dbb6cfdbf8 [REF] mail: remove deprecated email formataddr
[PEP 594] is going to depreciate the legacy `email.message.Message` API
and its related modules. Among them the `email.utils.formataddr`
function, this function takes a `(name, email)` pair and returns a
string value suitable for From, To and Cc headers.

The stdlib function is capable of handling several character encoding
and two binary-to-ascii encoding. Odoo only uses `utf-8` which is
compatible with the base64 b2a encoding. The re-implementation of that
function has been simplified to only support base64-ed utf-8 and ascii.

[PEP 594]: https://www.python.org/dev/peps/pep-0594/\#email-lagacy-api
2019-09-17 11:32:54 +00:00

51 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, fields, models, tools
class MailCCMixin(models.AbstractModel):
_name = 'mail.thread.cc'
_inherit = 'mail.thread'
_description = 'Email CC management'
email_cc = fields.Char('Email cc', help='List of cc from incoming emails.')
def _mail_cc_sanitized_raw_dict(self, cc_string):
'''return a dict of sanitize_email:raw_email from a string of cc'''
if not cc_string:
return {}
return {tools.email_normalize(email): tools.formataddr((name, tools.email_normalize(email)))
for (name, email) in tools.email_split_tuples(cc_string)}
@api.model
def message_new(self, msg_dict, custom_values=None):
if custom_values is None:
custom_values = {}
cc_values = {
'email_cc': ", ".join(self._mail_cc_sanitized_raw_dict(msg_dict.get('cc')).values()),
}
cc_values.update(custom_values)
return super(MailCCMixin, self).message_new(msg_dict, cc_values)
def message_update(self, msg_dict, update_vals=None):
'''Adds cc email to self.email_cc while trying to keep email as raw as possible but unique'''
if update_vals is None:
update_vals = {}
cc_values = {}
new_cc = self._mail_cc_sanitized_raw_dict(msg_dict.get('cc'))
if new_cc:
old_cc = self._mail_cc_sanitized_raw_dict(self.email_cc)
new_cc.update(old_cc)
cc_values['email_cc'] = ", ".join(new_cc.values())
cc_values.update(update_vals)
return super(MailCCMixin, self).message_update(msg_dict, cc_values)
def _message_get_suggested_recipients(self):
recipients = super(MailCCMixin, self)._message_get_suggested_recipients()
for record in self:
if record.email_cc:
for email in tools.email_split_and_format(record.email_cc):
record._message_add_suggested_recipient(recipients, email=email, reason=_('CC Email'))
return recipients