From c2fd8c95fc60f9313743937e891ec8fd72875984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20D=C3=ADaz?= Date: Wed, 3 Jul 2019 11:12:11 +0200 Subject: [PATCH] [IMP] mail_tracking: Cc --- mail_tracking/README.rst | 2 +- mail_tracking/models/__init__.py | 1 + mail_tracking/models/mail_mail.py | 1 - mail_tracking/models/mail_message.py | 39 ++++++++++++++++- mail_tracking/models/mail_thread.py | 42 +++++++++++++++++++ .../static/src/xml/mail_tracking.xml | 18 +++++--- mail_tracking/tests/test_mail_tracking.py | 28 +++++++++++++ 7 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 mail_tracking/models/mail_thread.py diff --git a/mail_tracking/README.rst b/mail_tracking/README.rst index b13c35d..5c2d059 100644 --- a/mail_tracking/README.rst +++ b/mail_tracking/README.rst @@ -23,7 +23,7 @@ Email tracking :target: https://runbot.odoo-community.org/runbot/205/12.0 :alt: Try me on Runbot -|badge1| |badge2| |badge3| |badge4| |badge5| +|badge1| |badge2| |badge3| |badge4| |badge5| This module shows email notification tracking status for any messages in mail thread (chatter). Each notified partner will have an intuitive icon just diff --git a/mail_tracking/models/__init__.py b/mail_tracking/models/__init__.py index 847f98f..4273600 100644 --- a/mail_tracking/models/__init__.py +++ b/mail_tracking/models/__init__.py @@ -7,3 +7,4 @@ from . import mail_message from . import mail_tracking_email from . import mail_tracking_event from . import res_partner +from . import mail_thread diff --git a/mail_tracking/models/mail_mail.py b/mail_tracking/models/mail_mail.py index 2342d1c..37edb97 100644 --- a/mail_tracking/models/mail_mail.py +++ b/mail_tracking/models/mail_mail.py @@ -4,7 +4,6 @@ import time from datetime import datetime from email.utils import COMMASPACE - from odoo import models, fields diff --git a/mail_tracking/models/mail_message.py b/mail_tracking/models/mail_message.py index a6bcc68..38d10e0 100644 --- a/mail_tracking/models/mail_message.py +++ b/mail_tracking/models/mail_message.py @@ -1,12 +1,18 @@ # Copyright 2016 Antonio Espinosa - +# Copyright 2019 Alexandre Díaz # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import models, api +from odoo import models, api, fields +from odoo.tools import email_split class MailMessage(models.Model): _inherit = "mail.message" + # Recipients + email_cc = fields.Char("Cc", help='Additional recipients that receive a ' + '"Carbon Copy" of the e-mail') + def _tracking_status_map_get(self): return { 'False': 'waiting', @@ -58,10 +64,37 @@ class MailMessage(models.Model): for partner in partners: # If there is partners not included, then status is 'unknown' partner_trackings.append(( - 'unknown', False, partner.name, partner.id)) + 'unknown', False, partner.name, partner.id, partner.email)) res[message.id] = partner_trackings return res + @api.multi + def _get_email_cc(self): + """This method gets all Cc mails and the associated partner if exist. + The result is a dictionary by 'message id' with a list of tuples + (str:email_cc, list:[partner id, partner display_name] or False) + """ + res = {} + ResPartnerObj = self.env['res.partner'] + for message in self: + email_cc_list = email_split(message.email_cc) + email_cc_list_checked = [] + if any(email_cc_list): + partners = ResPartnerObj.search([ + ('email', 'in', email_cc_list) + ]) + email_cc_list = set(email_cc_list) + for partner in partners: + email_cc_list.discard(partner.email) + email_cc_list_checked.append( + (partner.email, [partner.id, partner.display_name])) + for email in email_cc_list: + email_cc_list_checked.append((email, False)) + res.update({ + message.id: email_cc_list_checked + }) + return res + @api.model def _message_read_dict_postprocess(self, messages, message_tree): res = super(MailMessage, self)._message_read_dict_postprocess( @@ -69,9 +102,11 @@ class MailMessage(models.Model): mail_message_ids = {m.get('id') for m in messages if m.get('id')} mail_messages = self.browse(mail_message_ids) partner_trackings = mail_messages.tracking_status() + email_cc = mail_messages._get_email_cc() for message_dict in messages: mail_message_id = message_dict.get('id', False) if mail_message_id: message_dict['partner_trackings'] = \ partner_trackings[mail_message_id] + message_dict['email_cc'] = email_cc[mail_message_id] return res diff --git a/mail_tracking/models/mail_thread.py b/mail_tracking/models/mail_thread.py new file mode 100644 index 0000000..c5ce2ac --- /dev/null +++ b/mail_tracking/models/mail_thread.py @@ -0,0 +1,42 @@ +# Copyright 2019 Alexandre Díaz +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models, api, _ +from email.utils import getaddresses +from odoo.tools import email_split_and_format + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + @api.multi + @api.returns('self', lambda value: value.id) + def message_post(self, *args, **kwargs): + new_message = super().message_post(*args, **kwargs) + email_cc = kwargs.get('cc') + if email_cc: + new_message.sudo().write({ + 'email_cc': email_cc, + }) + return new_message + + @api.multi + def message_get_suggested_recipients(self): + res = super().message_get_suggested_recipients() + ResPartnerObj = self.env['res.partner'] + for record in self: + messages = record.message_ids.filtered('email_cc') + for msg in messages: + email_cc_list = email_split_and_format(msg.email_cc) + for cc in email_cc_list: + email_parts = getaddresses([cc])[0] + partner_id = record.message_partner_info_from_emails( + [email_parts[1]])[0].get('partner_id') + if not partner_id: + res[record.id].append((False, cc, _('Cc'))) + else: + partner = ResPartnerObj.browse(partner_id, + self._prefetch) + record._message_add_suggested_recipient( + res, partner=partner, reason=_('Cc')) + return res diff --git a/mail_tracking/static/src/xml/mail_tracking.xml b/mail_tracking/static/src/xml/mail_tracking.xml index 27d7988..167b453 100644 --- a/mail_tracking/static/src/xml/mail_tracking.xml +++ b/mail_tracking/static/src/xml/mail_tracking.xml @@ -1,37 +1,43 @@