You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import models, api
  5. class MailMessage(models.Model):
  6. _inherit = "mail.message"
  7. def _tracking_status_map_get(self):
  8. return {
  9. 'False': 'waiting',
  10. 'error': 'error',
  11. 'deferred': 'sent',
  12. 'sent': 'sent',
  13. 'delivered': 'delivered',
  14. 'opened': 'opened',
  15. 'rejected': 'error',
  16. 'spam': 'error',
  17. 'unsub': 'opened',
  18. 'bounced': 'error',
  19. 'soft-bounced': 'error',
  20. }
  21. def _partner_tracking_status_get(self, tracking_email):
  22. tracking_status_map = self._tracking_status_map_get()
  23. status = 'unknown'
  24. if tracking_email:
  25. tracking_email_status = str(tracking_email.state)
  26. status = tracking_status_map.get(tracking_email_status, 'unknown')
  27. return status
  28. def tracking_status(self):
  29. res = {}
  30. for message in self:
  31. partner_trackings = []
  32. partners_already = self.env['res.partner']
  33. partners = self.env['res.partner']
  34. trackings = self.env['mail.tracking.email'].search([
  35. ('mail_message_id', '=', message.id),
  36. ])
  37. # Search all trackings for this message
  38. for tracking in trackings:
  39. status = self._partner_tracking_status_get(tracking)
  40. recipient = (
  41. tracking.partner_id.display_name or tracking.recipient)
  42. partner_trackings.append((
  43. status, tracking.id, recipient, tracking.partner_id.id))
  44. if tracking.partner_id:
  45. partners_already |= tracking.partner_id
  46. # Search all recipients for this message
  47. if message.partner_ids:
  48. partners |= message.partner_ids
  49. if message.needaction_partner_ids:
  50. partners |= message.needaction_partner_ids
  51. # Remove recipients already included
  52. partners -= partners_already
  53. for partner in partners:
  54. # If there is partners not included, then status is 'unknown'
  55. partner_trackings.append((
  56. 'unknown', False, partner.display_name, partner.id))
  57. res[message.id] = partner_trackings
  58. return res
  59. @api.model
  60. def _message_read_dict_postprocess(self, messages, message_tree):
  61. res = super(MailMessage, self)._message_read_dict_postprocess(
  62. messages, message_tree)
  63. mail_message_ids = {m.get('id') for m in messages if m.get('id')}
  64. mail_messages = self.browse(mail_message_ids)
  65. partner_trackings = mail_messages.tracking_status()
  66. for message_dict in messages:
  67. mail_message_id = message_dict.get('id', False)
  68. if mail_message_id:
  69. message_dict['partner_trackings'] = \
  70. partner_trackings[mail_message_id]
  71. return res