mail_tracking_event.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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="Recipiente", readonly=True)
  14. timestamp = fields.Float(
  15. string='UTC Fecha-hora', readonly=True,
  16. digits=dp.get_precision('MailTracking Timestamp'))
  17. time = fields.Datetime(string="Hora", readonly=True)
  18. date = fields.Date(
  19. string="Fecha", readonly=True, compute="_compute_date", store=True)
  20. tracking_email_id = fields.Many2one(
  21. string='Mensaje', readonly=True, required=True, ondelete='cascade',
  22. comodel_name='mail.tracking.email')
  23. event_type = fields.Selection(string='Tipo de Evento', selection=[
  24. ('sent', 'Enviado'),
  25. ('delivered', 'Entregado'),
  26. ('deferral', 'Diferido'),
  27. ('hard_bounce', 'Rebotado'),
  28. ('soft_bounce', 'Rebotado suavemente'),
  29. ('open', 'Abierto'),
  30. ('click', 'Clickado'),
  31. ('spam', 'Spam'),
  32. ('unsub', 'Desuscripto'),
  33. ('reject', 'Rechazado'),
  34. ], readonly=True)
  35. smtp_server = fields.Char(string='SMTP-Servidor', readonly=True)
  36. url = fields.Char(string='Clickado en URL', readonly=True)
  37. ip = fields.Char(string='IP del Usuario', readonly=True)
  38. user_agent = fields.Char(string='Navegador', readonly=True)
  39. mobile = fields.Boolean(string='Es Movil?', readonly=True)
  40. os_family = fields.Char(string='Sistema Operativo', readonly=True)
  41. ua_family = fields.Char(string='Familia de Navegador', readonly=True)
  42. ua_type = fields.Char(string='Tipo de Navegador', readonly=True)
  43. user_country_id = fields.Many2one(string='País de Usuario', readonly=True,
  44. comodel_name='res.country')
  45. error_type = fields.Char(string='Error - Tipo', readonly=True)
  46. error_description = fields.Char(string='Descripción del Error', readonly=True)
  47. error_details = fields.Text(string='Detalles del Error', 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')