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.

136 lines
5.6 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. import time
  5. from datetime import datetime
  6. from openerp import models, api, fields
  7. import openerp.addons.decimal_precision as dp
  8. class MailTrackingEvent(models.Model):
  9. _name = "mail.tracking.event"
  10. _order = 'timestamp desc'
  11. _rec_name = 'event_type'
  12. _description = 'MailTracking event'
  13. recipient = fields.Char(string="Recipient", readonly=True)
  14. timestamp = fields.Float(
  15. string='UTC timestamp', readonly=True,
  16. digits=dp.get_precision('MailTracking Timestamp'))
  17. time = fields.Datetime(string="Time", readonly=True)
  18. date = fields.Date(
  19. string="Date", readonly=True, compute="_compute_date", store=True)
  20. tracking_email_id = fields.Many2one(
  21. string='Message', readonly=True, required=True, ondelete='cascade',
  22. comodel_name='mail.tracking.email')
  23. event_type = fields.Selection(string='Event type', selection=[
  24. ('sent', 'Sent'),
  25. ('delivered', 'Delivered'),
  26. ('deferral', 'Deferral'),
  27. ('hard_bounce', 'Hard bounce'),
  28. ('soft_bounce', 'Soft bounce'),
  29. ('open', 'Open'),
  30. ('click', 'Clicked'),
  31. ('spam', 'Spam'),
  32. ('unsub', 'Unsubscribed'),
  33. ('reject', 'Rejected'),
  34. ], readonly=True)
  35. smtp_server = fields.Char(string='SMTP server', readonly=True)
  36. url = fields.Char(string='Clicked URL', readonly=True)
  37. ip = fields.Char(string='User IP', readonly=True)
  38. user_agent = fields.Char(string='User agent', readonly=True)
  39. mobile = fields.Boolean(string='Is mobile?', readonly=True)
  40. os_family = fields.Char(string='Operating system family', readonly=True)
  41. ua_family = fields.Char(string='User agent family', readonly=True)
  42. ua_type = fields.Char(string='User agent type', readonly=True)
  43. user_country_id = fields.Many2one(string='User country', readonly=True,
  44. comodel_name='res.country')
  45. error_type = fields.Char(string='Error type', readonly=True)
  46. error_description = fields.Char(string='Error description', readonly=True)
  47. error_details = fields.Text(string='Error details', readonly=True)
  48. @api.multi
  49. @api.depends('time')
  50. def _compute_date(self):
  51. for email in self:
  52. email.date = fields.Date.to_string(
  53. fields.Date.from_string(email.time))
  54. def _process_data(self, tracking_email, metadata, event_type, state):
  55. ts = time.time()
  56. dt = datetime.utcfromtimestamp(ts)
  57. return {
  58. 'recipient': metadata.get('recipient', tracking_email.recipient),
  59. 'timestamp': metadata.get('timestamp', ts),
  60. 'time': metadata.get('time', fields.Datetime.to_string(dt)),
  61. 'date': metadata.get('date', fields.Date.to_string(dt)),
  62. 'tracking_email_id': tracking_email.id,
  63. 'event_type': event_type,
  64. 'ip': metadata.get('ip', False),
  65. 'url': metadata.get('url', False),
  66. 'user_agent': metadata.get('user_agent', False),
  67. 'mobile': metadata.get('mobile', False),
  68. 'os_family': metadata.get('os_family', False),
  69. 'ua_family': metadata.get('ua_family', False),
  70. 'ua_type': metadata.get('ua_type', False),
  71. 'user_country_id': metadata.get('user_country_id', False),
  72. 'error_type': metadata.get('error_type', False),
  73. 'error_description': metadata.get('error_description', False),
  74. 'error_details': metadata.get('error_details', False),
  75. }
  76. def _process_status(self, tracking_email, metadata, event_type, state):
  77. tracking_email.sudo().write({'state': state})
  78. return self._process_data(tracking_email, metadata, event_type, state)
  79. def _process_bounce(self, tracking_email, metadata, event_type, state):
  80. tracking_email.sudo().write({
  81. 'state': state,
  82. 'bounce_type': metadata.get('bounce_type', False),
  83. 'bounce_description': metadata.get('bounce_description', False),
  84. })
  85. return self._process_data(tracking_email, metadata, event_type, state)
  86. @api.model
  87. def process_delivered(self, tracking_email, metadata):
  88. return self._process_status(
  89. tracking_email, metadata, 'delivered', 'delivered')
  90. @api.model
  91. def process_deferral(self, tracking_email, metadata):
  92. return self._process_status(
  93. tracking_email, metadata, 'deferral', 'deferred')
  94. @api.model
  95. def process_hard_bounce(self, tracking_email, metadata):
  96. return self._process_bounce(
  97. tracking_email, metadata, 'hard_bounce', 'bounced')
  98. @api.model
  99. def process_soft_bounce(self, tracking_email, metadata):
  100. return self._process_bounce(
  101. tracking_email, metadata, 'soft_bounce', 'soft-bounced')
  102. @api.model
  103. def process_open(self, tracking_email, metadata):
  104. return self._process_status(tracking_email, metadata, 'open', 'opened')
  105. @api.model
  106. def process_click(self, tracking_email, metadata):
  107. return self._process_status(
  108. tracking_email, metadata, 'click', 'opened')
  109. @api.model
  110. def process_spam(self, tracking_email, metadata):
  111. return self._process_status(tracking_email, metadata, 'spam', 'spam')
  112. @api.model
  113. def process_unsub(self, tracking_email, metadata):
  114. return self._process_status(tracking_email, metadata, 'unsub', 'unsub')
  115. @api.model
  116. def process_reject(self, tracking_email, metadata):
  117. return self._process_status(
  118. tracking_email, metadata, 'reject', 'rejected')