2019-07-03 11:12:11 +02:00
|
|
|
# Copyright 2019 Alexandre Díaz
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
|
|
|
from email.utils import getaddresses
|
2019-11-18 11:46:59 +01:00
|
|
|
|
2019-07-05 14:20:27 +02:00
|
|
|
from lxml import etree
|
2019-07-03 11:12:11 +02:00
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
from odoo import _, api, fields, models
|
|
|
|
from odoo.tools import email_split_and_format
|
|
|
|
|
2019-07-03 11:12:11 +02:00
|
|
|
|
|
|
|
class MailThread(models.AbstractModel):
|
|
|
|
_inherit = "mail.thread"
|
|
|
|
|
2019-07-05 14:20:27 +02:00
|
|
|
failed_message_ids = fields.One2many(
|
2019-11-18 11:46:59 +01:00
|
|
|
"mail.message",
|
|
|
|
"res_id",
|
|
|
|
string="Failed Messages",
|
|
|
|
domain=lambda self: [("model", "=", self._name)]
|
|
|
|
+ self._get_failed_message_domain(),
|
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
|
|
|
|
def _get_failed_message_domain(self):
|
|
|
|
"""Domain used to display failed messages on the 'failed_messages'
|
|
|
|
widget"""
|
2019-11-18 11:46:59 +01:00
|
|
|
failed_states = self.env["mail.message"].get_failed_states()
|
2019-10-17 03:28:42 +02:00
|
|
|
return [
|
2019-11-18 11:46:59 +01:00
|
|
|
("mail_tracking_needs_action", "=", True),
|
|
|
|
("mail_tracking_ids.state", "in", list(failed_states)),
|
2019-10-17 03:28:42 +02:00
|
|
|
]
|
2019-07-05 14:20:27 +02:00
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
@api.returns("self", lambda value: value.id)
|
2019-07-03 11:12:11 +02:00
|
|
|
def message_post(self, *args, **kwargs):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Adds CC recipient to the message.
|
|
|
|
|
2020-03-19 20:30:34 +01:00
|
|
|
Because Odoo implementation avoid store 'from, to, cc' recipients we
|
|
|
|
ensure that this information its written into the mail.message record.
|
2019-10-17 03:28:42 +02:00
|
|
|
"""
|
2020-03-19 20:30:34 +01:00
|
|
|
kwargs.update(
|
|
|
|
{"email_cc": kwargs.get("cc", False), "email_to": kwargs.get("to", False)}
|
|
|
|
)
|
|
|
|
return super().message_post(*args, **kwargs)
|
2019-07-03 11:12:11 +02:00
|
|
|
|
2019-11-18 11:34:02 +01:00
|
|
|
def _message_get_suggested_recipients(self):
|
2020-03-19 20:30:34 +01:00
|
|
|
"""Adds email 'extra' recipients as suggested recipients.
|
2019-10-17 03:28:42 +02:00
|
|
|
|
|
|
|
If the recipient has a res.partner, use it.
|
|
|
|
"""
|
2019-11-18 11:34:02 +01:00
|
|
|
res = super()._message_get_suggested_recipients()
|
2020-03-19 20:30:34 +01:00
|
|
|
self._add_extra_recipients_suggestions(res, "email_cc", _("Cc"))
|
|
|
|
self._add_extra_recipients_suggestions(res, "email_to", _("Anon. To"))
|
|
|
|
return res
|
|
|
|
|
|
|
|
def _add_extra_recipients_suggestions(self, suggestions, field_mail, reason):
|
2019-11-18 11:46:59 +01:00
|
|
|
ResPartnerObj = self.env["res.partner"]
|
2020-03-19 20:30:34 +01:00
|
|
|
aliases = self.env["mail.alias"].get_aliases()
|
|
|
|
email_extra_formated_list = []
|
2019-07-03 11:12:11 +02:00
|
|
|
for record in self:
|
2020-03-19 20:30:34 +01:00
|
|
|
emails_extra = record.message_ids.mapped(field_mail)
|
|
|
|
for email in emails_extra:
|
|
|
|
email_extra_formated_list.extend(email_split_and_format(email))
|
|
|
|
email_extra_formated_list = set(email_extra_formated_list)
|
|
|
|
email_extra_list = [x[1] for x in getaddresses(email_extra_formated_list)]
|
|
|
|
partners_info = self._message_partner_info_from_emails(email_extra_list)
|
|
|
|
for pinfo in partners_info:
|
|
|
|
partner_id = pinfo["partner_id"]
|
|
|
|
email = pinfo["full_name"]
|
2019-07-22 17:07:50 +02:00
|
|
|
if not partner_id:
|
2020-03-19 20:30:34 +01:00
|
|
|
if email not in aliases:
|
|
|
|
self._message_add_suggested_recipient(
|
|
|
|
suggestions, email=email, reason=reason
|
|
|
|
)
|
2019-07-22 17:07:50 +02:00
|
|
|
else:
|
2019-11-18 11:34:02 +01:00
|
|
|
partner = ResPartnerObj.browse(partner_id)
|
2020-03-19 20:30:34 +01:00
|
|
|
self._message_add_suggested_recipient(
|
|
|
|
suggestions, partner=partner, reason=reason
|
2019-11-18 11:46:59 +01:00
|
|
|
)
|
2019-07-05 14:20:27 +02:00
|
|
|
|
|
|
|
@api.model
|
2019-11-28 22:58:09 +01:00
|
|
|
def _fields_view_get(
|
2019-11-18 11:46:59 +01:00
|
|
|
self, view_id=None, view_type="form", toolbar=False, submenu=False
|
|
|
|
):
|
2019-07-05 14:20:27 +02:00
|
|
|
"""Add filters for failed messages.
|
|
|
|
|
|
|
|
These filters will show up on any form or search views of any
|
|
|
|
model inheriting from ``mail.thread``.
|
|
|
|
"""
|
2019-11-28 22:58:09 +01:00
|
|
|
res = super()._fields_view_get(
|
2019-11-18 11:46:59 +01:00
|
|
|
view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu
|
|
|
|
)
|
|
|
|
if view_type not in {"search", "form"}:
|
2019-07-05 14:20:27 +02:00
|
|
|
return res
|
2019-11-18 11:46:59 +01:00
|
|
|
doc = etree.XML(res["arch"])
|
|
|
|
if view_type == "search":
|
2019-07-05 14:20:27 +02:00
|
|
|
# Modify view to add new filter element
|
|
|
|
nodes = doc.xpath("//search")
|
|
|
|
if nodes:
|
|
|
|
# Create filter element
|
|
|
|
new_filter = etree.Element(
|
2019-11-18 11:46:59 +01:00
|
|
|
"filter",
|
|
|
|
{
|
|
|
|
"string": _("Failed sent messages"),
|
|
|
|
"name": "failed_message_ids",
|
|
|
|
"domain": str(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
"failed_message_ids.mail_tracking_ids.state",
|
|
|
|
"in",
|
|
|
|
list(self.env["mail.message"].get_failed_states()),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"failed_message_ids.mail_tracking_needs_action",
|
|
|
|
"=",
|
|
|
|
True,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
nodes[0].append(etree.Element("separator"))
|
2019-07-05 14:20:27 +02:00
|
|
|
nodes[0].append(new_filter)
|
2019-11-18 11:46:59 +01:00
|
|
|
elif view_type == "form":
|
2019-07-05 14:20:27 +02:00
|
|
|
# Modify view to add new field element
|
2019-11-18 11:46:59 +01:00
|
|
|
nodes = doc.xpath("//field[@name='message_ids' and @widget='mail_thread']")
|
2019-07-05 14:20:27 +02:00
|
|
|
if nodes:
|
|
|
|
# Create field
|
2019-11-18 11:46:59 +01:00
|
|
|
field_failed_messages = etree.Element(
|
|
|
|
"field",
|
|
|
|
{"name": "failed_message_ids", "widget": "mail_failed_message"},
|
|
|
|
)
|
2019-07-05 14:20:27 +02:00
|
|
|
nodes[0].addprevious(field_failed_messages)
|
2019-11-18 11:46:59 +01:00
|
|
|
res["arch"] = etree.tostring(doc, encoding="unicode")
|
2019-07-05 14:20:27 +02:00
|
|
|
return res
|