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.

77 lines
3.0 KiB

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