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.

80 lines
3.3 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,
  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. @api.multi
  46. @api.depends('time')
  47. def _compute_date(self):
  48. for email in self:
  49. email.date = fields.Date.to_string(
  50. fields.Date.from_string(email.time))
  51. def _process_action(self, tracking_email, metadata, event_type, state):
  52. ts = time.time()
  53. dt = datetime.utcfromtimestamp(ts)
  54. tracking_email.sudo().write({'state': state})
  55. return {
  56. 'recipient': metadata.get('recipient', tracking_email.recipient),
  57. 'timestamp': metadata.get('ts', ts),
  58. 'time': metadata.get('time', fields.Datetime.to_string(dt)),
  59. 'date': metadata.get('date', fields.Date.to_string(dt)),
  60. 'tracking_email_id': tracking_email.id,
  61. 'event_type': event_type,
  62. 'ip': metadata.get('ip', False),
  63. 'user_agent': metadata.get('user_agent', False),
  64. 'mobile': metadata.get('mobile', False),
  65. 'os_family': metadata.get('os_family', False),
  66. 'ua_family': metadata.get('ua_family', False),
  67. 'ua_type': metadata.get('ua_type', False),
  68. 'user_country_id': metadata.get('user_country_id', False),
  69. }
  70. @api.model
  71. def process_open(self, tracking_email, metadata):
  72. return self._process_action(tracking_email, metadata, 'open', 'opened')