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.

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