2019-10-17 03:28:42 +02:00
|
|
|
# Copyright 2019 Alexandre Díaz
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
from odoo import api, models
|
2019-10-17 03:28:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MailResendMessage(models.TransientModel):
|
|
|
|
_inherit = "mail.resend.message"
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def default_get(self, fields):
|
|
|
|
rec = super().default_get(fields)
|
2019-11-18 11:46:59 +01:00
|
|
|
message_id = self._context.get("mail_message_to_resend")
|
2019-10-17 03:28:42 +02:00
|
|
|
if message_id:
|
2019-11-18 11:46:59 +01:00
|
|
|
MailMessageObj = self.env["mail.message"]
|
2019-10-17 03:28:42 +02:00
|
|
|
mail_message_id = MailMessageObj.browse(message_id)
|
|
|
|
failed_states = MailMessageObj.get_failed_states()
|
|
|
|
tracking_ids = mail_message_id.mail_tracking_ids.filtered(
|
2019-11-18 11:46:59 +01:00
|
|
|
lambda x: x.state in failed_states
|
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
if any(tracking_ids):
|
2019-11-18 11:46:59 +01:00
|
|
|
partner_ids = [
|
|
|
|
(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"partner_id": tracking.partner_id.id,
|
|
|
|
"name": tracking.partner_id.name,
|
|
|
|
"email": tracking.partner_id.email,
|
|
|
|
"resend": True,
|
|
|
|
"message": tracking.error_description,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
for tracking in tracking_ids
|
|
|
|
]
|
|
|
|
rec["partner_ids"].extend(partner_ids)
|
2019-10-17 03:28:42 +02:00
|
|
|
return rec
|
|
|
|
|
|
|
|
def resend_mail_action(self):
|
|
|
|
for wizard in self:
|
2019-11-18 11:46:59 +01:00
|
|
|
to_send = wizard.partner_ids.filtered("resend").mapped("partner_id")
|
2019-10-17 03:28:42 +02:00
|
|
|
if to_send:
|
|
|
|
# Set as reviewed
|
|
|
|
wizard.mail_message_id.mail_tracking_needs_action = False
|
|
|
|
# Reset mail.tracking.email state
|
2019-11-18 11:46:59 +01:00
|
|
|
tracking_ids = wizard.mail_message_id.mail_tracking_ids.filtered(
|
|
|
|
lambda x: x.partner_id in to_send
|
|
|
|
)
|
|
|
|
tracking_ids.write({"state": False})
|
2019-10-17 03:28:42 +02:00
|
|
|
# Send bus notifications to update Discuss and
|
|
|
|
# mail_failed_messages widget
|
2019-11-14 18:41:24 +01:00
|
|
|
notification = {
|
2019-11-18 11:46:59 +01:00
|
|
|
"type": "toggle_tracking_status",
|
|
|
|
"message_ids": [self.mail_message_id.id],
|
|
|
|
"needs_actions": False,
|
2019-11-14 18:41:24 +01:00
|
|
|
}
|
2019-11-18 11:46:59 +01:00
|
|
|
self.env["bus.bus"].sendone(
|
|
|
|
(self._cr.dbname, "res.partner", self.env.user.partner_id.id),
|
|
|
|
notification,
|
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
super().resend_mail_action()
|