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.

79 lines
3.1 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 openerp 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. @api.multi
  29. def tracking_status(self):
  30. res = {}
  31. for message in self:
  32. partner_trackings = []
  33. partners_already = self.env['res.partner']
  34. partners = self.env['res.partner']
  35. trackings = self.env['mail.tracking.email'].search([
  36. ('mail_message_id', '=', message.id),
  37. ])
  38. # Search all trackings for this message
  39. for tracking in trackings:
  40. status = self._partner_tracking_status_get(tracking)
  41. recipient = (
  42. tracking.partner_id.display_name or tracking.recipient)
  43. partner_trackings.append((
  44. status, tracking.id, recipient, tracking.partner_id.id))
  45. if tracking.partner_id:
  46. partners_already |= tracking.partner_id
  47. # Search all recipients for this message
  48. if message.partner_ids:
  49. partners |= message.partner_ids
  50. if message.needaction_partner_ids:
  51. partners |= message.needaction_partner_ids
  52. # Remove recipients already included
  53. partners -= partners_already
  54. for partner in partners:
  55. # If there is partners not included, then status is 'unknown'
  56. partner_trackings.append((
  57. 'unknown', False, partner.display_name, partner.id))
  58. res[message.id] = partner_trackings
  59. return res
  60. @api.model
  61. def _message_read_dict_postprocess(self, messages, message_tree):
  62. res = super(MailMessage, self)._message_read_dict_postprocess(
  63. messages, message_tree)
  64. mail_message_ids = {m.get('id') for m in messages if m.get('id')}
  65. mail_messages = self.browse(mail_message_ids)
  66. partner_trackings = mail_messages.tracking_status()
  67. for message_dict in messages:
  68. mail_message_id = message_dict.get('id', False)
  69. if mail_message_id:
  70. message_dict['partner_trackings'] = \
  71. partner_trackings[mail_message_id]
  72. return res