123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- # -*- coding: utf-8 -*-
- # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
- # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
- import time
- from datetime import datetime
- from openerp import models, api, fields
- import openerp.addons.decimal_precision as dp
- class MailTrackingEvent(models.Model):
- _name = "mail.tracking.event"
- _order = 'timestamp desc'
- _rec_name = 'event_type'
- _description = 'MailTracking event'
- recipient = fields.Char(string="Recipiente", readonly=True)
- timestamp = fields.Float(
- string='UTC Fecha-hora', readonly=True,
- digits=dp.get_precision('MailTracking Timestamp'))
- time = fields.Datetime(string="Hora", readonly=True)
- date = fields.Date(
- string="Fecha", readonly=True, compute="_compute_date", store=True)
- tracking_email_id = fields.Many2one(
- string='Mensaje', readonly=True, required=True, ondelete='cascade',
- comodel_name='mail.tracking.email')
- event_type = fields.Selection(string='Tipo de Evento', selection=[
- ('sent', 'Enviado'),
- ('delivered', 'Entregado'),
- ('deferral', 'Diferido'),
- ('hard_bounce', 'Rebotado'),
- ('soft_bounce', 'Rebotado suavemente'),
- ('open', 'Abierto'),
- ('click', 'Clickado'),
- ('spam', 'Spam'),
- ('unsub', 'Desuscripto'),
- ('reject', 'Rechazado'),
- ], readonly=True)
- smtp_server = fields.Char(string='SMTP-Servidor', readonly=True)
- url = fields.Char(string='Clickado en URL', readonly=True)
- ip = fields.Char(string='IP del Usuario', readonly=True)
- user_agent = fields.Char(string='Navegador', readonly=True)
- mobile = fields.Boolean(string='Es Movil?', readonly=True)
- os_family = fields.Char(string='Sistema Operativo', readonly=True)
- ua_family = fields.Char(string='Familia de Navegador', readonly=True)
- ua_type = fields.Char(string='Tipo de Navegador', readonly=True)
- user_country_id = fields.Many2one(string='País de Usuario', readonly=True,
- comodel_name='res.country')
- error_type = fields.Char(string='Error - Tipo', readonly=True)
- error_description = fields.Char(string='Descripción del Error', readonly=True)
- error_details = fields.Text(string='Detalles del Error', readonly=True)
- @api.multi
- @api.depends('time')
- def _compute_date(self):
- for email in self:
- email.date = fields.Date.to_string(
- fields.Date.from_string(email.time))
- def _process_data(self, tracking_email, metadata, event_type, state):
- ts = time.time()
- dt = datetime.utcfromtimestamp(ts)
- return {
- 'recipient': metadata.get('recipient', tracking_email.recipient),
- 'timestamp': metadata.get('timestamp', ts),
- 'time': metadata.get('time', fields.Datetime.to_string(dt)),
- 'date': metadata.get('date', fields.Date.to_string(dt)),
- 'tracking_email_id': tracking_email.id,
- 'event_type': event_type,
- 'ip': metadata.get('ip', False),
- 'url': metadata.get('url', False),
- 'user_agent': metadata.get('user_agent', False),
- 'mobile': metadata.get('mobile', False),
- 'os_family': metadata.get('os_family', False),
- 'ua_family': metadata.get('ua_family', False),
- 'ua_type': metadata.get('ua_type', False),
- 'user_country_id': metadata.get('user_country_id', False),
- 'error_type': metadata.get('error_type', False),
- 'error_description': metadata.get('error_description', False),
- 'error_details': metadata.get('error_details', False),
- }
- def _process_status(self, tracking_email, metadata, event_type, state):
- tracking_email.sudo().write({'state': state})
- return self._process_data(tracking_email, metadata, event_type, state)
- def _process_bounce(self, tracking_email, metadata, event_type, state):
- tracking_email.sudo().write({
- 'state': state,
- 'bounce_type': metadata.get('bounce_type', False),
- 'bounce_description': metadata.get('bounce_description', False),
- })
- return self._process_data(tracking_email, metadata, event_type, state)
- @api.model
- def process_delivered(self, tracking_email, metadata):
- return self._process_status(
- tracking_email, metadata, 'delivered', 'delivered')
- @api.model
- def process_deferral(self, tracking_email, metadata):
- return self._process_status(
- tracking_email, metadata, 'deferral', 'deferred')
- @api.model
- def process_hard_bounce(self, tracking_email, metadata):
- return self._process_bounce(
- tracking_email, metadata, 'hard_bounce', 'bounced')
- @api.model
- def process_soft_bounce(self, tracking_email, metadata):
- return self._process_bounce(
- tracking_email, metadata, 'soft_bounce', 'soft-bounced')
- @api.model
- def process_open(self, tracking_email, metadata):
- return self._process_status(tracking_email, metadata, 'open', 'opened')
- @api.model
- def process_click(self, tracking_email, metadata):
- return self._process_status(
- tracking_email, metadata, 'click', 'opened')
- @api.model
- def process_spam(self, tracking_email, metadata):
- return self._process_status(tracking_email, metadata, 'spam', 'spam')
- @api.model
- def process_unsub(self, tracking_email, metadata):
- return self._process_status(tracking_email, metadata, 'unsub', 'unsub')
- @api.model
- def process_reject(self, tracking_email, metadata):
- return self._process_status(
- tracking_email, metadata, 'reject', 'rejected')
|