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.

135 lines
5.5 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 odoo import models, api, fields
  7. import odoo.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,
  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.depends('time')
  49. def _compute_date(self):
  50. for email in self:
  51. email.date = fields.Date.to_string(
  52. fields.Date.from_string(email.time))
  53. def _process_data(self, tracking_email, metadata, event_type, state):
  54. ts = time.time()
  55. dt = datetime.utcfromtimestamp(ts)
  56. return {
  57. 'recipient': metadata.get('recipient', tracking_email.recipient),
  58. 'timestamp': metadata.get('timestamp', ts),
  59. 'time': metadata.get('time', fields.Datetime.to_string(dt)),
  60. 'date': metadata.get('date', fields.Date.to_string(dt)),
  61. 'tracking_email_id': tracking_email.id,
  62. 'event_type': event_type,
  63. 'ip': metadata.get('ip', False),
  64. 'url': metadata.get('url', False),
  65. 'user_agent': metadata.get('user_agent', False),
  66. 'mobile': metadata.get('mobile', False),
  67. 'os_family': metadata.get('os_family', False),
  68. 'ua_family': metadata.get('ua_family', False),
  69. 'ua_type': metadata.get('ua_type', False),
  70. 'user_country_id': metadata.get('user_country_id', False),
  71. 'error_type': metadata.get('error_type', False),
  72. 'error_description': metadata.get('error_description', False),
  73. 'error_details': metadata.get('error_details', False),
  74. }
  75. def _process_status(self, tracking_email, metadata, event_type, state):
  76. tracking_email.sudo().write({'state': state})
  77. return self._process_data(tracking_email, metadata, event_type, state)
  78. def _process_bounce(self, tracking_email, metadata, event_type, state):
  79. tracking_email.sudo().write({
  80. 'state': state,
  81. 'bounce_type': metadata.get('bounce_type', False),
  82. 'bounce_description': metadata.get('bounce_description', False),
  83. })
  84. return self._process_data(tracking_email, metadata, event_type, state)
  85. @api.model
  86. def process_delivered(self, tracking_email, metadata):
  87. return self._process_status(
  88. tracking_email, metadata, 'delivered', 'delivered')
  89. @api.model
  90. def process_deferral(self, tracking_email, metadata):
  91. return self._process_status(
  92. tracking_email, metadata, 'deferral', 'deferred')
  93. @api.model
  94. def process_hard_bounce(self, tracking_email, metadata):
  95. return self._process_bounce(
  96. tracking_email, metadata, 'hard_bounce', 'bounced')
  97. @api.model
  98. def process_soft_bounce(self, tracking_email, metadata):
  99. return self._process_bounce(
  100. tracking_email, metadata, 'soft_bounce', 'soft-bounced')
  101. @api.model
  102. def process_open(self, tracking_email, metadata):
  103. return self._process_status(tracking_email, metadata, 'open', 'opened')
  104. @api.model
  105. def process_click(self, tracking_email, metadata):
  106. return self._process_status(
  107. tracking_email, metadata, 'click', 'opened')
  108. @api.model
  109. def process_spam(self, tracking_email, metadata):
  110. return self._process_status(tracking_email, metadata, 'spam', 'spam')
  111. @api.model
  112. def process_unsub(self, tracking_email, metadata):
  113. return self._process_status(tracking_email, metadata, 'unsub', 'unsub')
  114. @api.model
  115. def process_reject(self, tracking_email, metadata):
  116. return self._process_status(
  117. tracking_email, metadata, 'reject', 'rejected')