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.

102 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3: Antiun Ingenieria S.L. - Antonio Espinosa
  3. # See README.rst file on addon root folder for more details
  4. import datetime
  5. import json
  6. import logging
  7. from openerp import models, fields, api
  8. _logger = logging.getLogger(__name__)
  9. class MailMandrillMessage(models.Model):
  10. _name = 'mail.mandrill.message'
  11. _order = 'timestamp desc'
  12. _rec_name = 'name'
  13. name = fields.Char(string='Subject', readonly=True)
  14. mandrill_id = fields.Char(string='Mandrill message ID', required=True,
  15. readonly=True)
  16. timestamp = fields.Integer(string='Mandrill UTC timestamp', readonly=True)
  17. time = fields.Datetime(string='Mandrill time', readonly=True)
  18. date = fields.Date(string='Mandrill date', readonly=True)
  19. recipient = fields.Char(string='Recipient email', readonly=True)
  20. sender = fields.Char(string='Sender email', readonly=True)
  21. state = fields.Selection([
  22. ('deferred', 'Deferred'),
  23. ('sent', 'Sent'),
  24. ('opened', 'Opened'),
  25. ('rejected', 'Rejected'),
  26. ('spam', 'Spam'),
  27. ('unsub', 'Unsubscribed'),
  28. ('bounced', 'Bounced'),
  29. ('soft-bounced', 'Soft bounced'),
  30. ], string='State', index=True, readonly=True,
  31. help=" * The 'Sent' status indicates that message was succesfully "
  32. "delivered to recipient Mail Exchange (MX) server.\n"
  33. " * The 'Opened' status indicates that message was opened or "
  34. "clicked by recipient.\n"
  35. " * The 'Rejected' status indicates that recipient email "
  36. "address is blacklisted by Mandrill. It is recomended to "
  37. "delete this email address.\n"
  38. " * The 'Spam' status indicates that Mandrill consider this "
  39. "message as spam.\n"
  40. " * The 'Unsubscribed' status indicates that recipient has "
  41. "requested to be unsubscribed from this message.\n"
  42. " * The 'Bounced' status indicates that message was bounced "
  43. "by recipient Mail Exchange (MX) server.\n"
  44. " * The 'Soft bounced' status indicates that message was soft "
  45. "bounced by recipient Mail Exchange (MX) server.\n")
  46. bounce_type = fields.Char(string='Bounce type', readonly=True)
  47. bounce_description = fields.Char(string='Bounce description',
  48. readonly=True)
  49. tags = fields.Char(string='Tags', readonly=True)
  50. metadata = fields.Text(string='Metadata', readonly=True)
  51. event_ids = fields.One2many(
  52. string='Mandrill events',
  53. comodel_name='mail.mandrill.event', inverse_name='message_id')
  54. def _message_prepare(self, message_id, event_type, event):
  55. msg = event.get('msg')
  56. ts = msg.get('ts', 0)
  57. time = datetime.datetime.fromtimestamp(ts)
  58. tags = msg.get('tags', [])
  59. metadata = msg.get('metadata', {})
  60. metatext = json.dumps(metadata, indent=4) if metadata else False
  61. return {
  62. 'mandrill_id': message_id,
  63. 'timestamp': ts,
  64. 'time': time.strftime('%Y-%m-%d %H:%M:%S') if ts else False,
  65. 'date': time.strftime('%Y-%m-%d') if ts else False,
  66. 'recipient': msg.get('email', False),
  67. 'sender': msg.get('sender', False),
  68. 'name': msg.get('subject', False),
  69. 'tags': ', '.join(tags) if tags else False,
  70. 'metadata': metatext,
  71. }
  72. def _event_prepare(self, message, event_type, event):
  73. m_event = self.env['mail.mandrill.event']
  74. method = getattr(m_event, 'process_' + event_type, None)
  75. if method and hasattr(method, '__call__'):
  76. return method(message, event)
  77. else:
  78. _logger.info('Unknown event type: %s' % event_type)
  79. return False
  80. @api.model
  81. def process(self, message_id, event_type, event):
  82. if not (message_id and event_type and event):
  83. return False
  84. msg = event.get('msg')
  85. message = self.search([('mandrill_id', '=', message_id)])
  86. if msg and not message:
  87. data = self._message_prepare(message_id, event_type, event)
  88. message = self.create(data) if data else False
  89. if message:
  90. m_event = self.env['mail.mandrill.event']
  91. data = self._event_prepare(message, event_type, event)
  92. return m_event.create(data) if data else False
  93. return False