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.

167 lines
6.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3: Antiun Ingenieria S.L. - Antonio Espinosa
  3. # See README.rst file on addon root folder for more details
  4. import datetime
  5. from openerp import models, fields, api
  6. class MailMandrillEvent(models.Model):
  7. _name = 'mail.mandrill.event'
  8. _order = 'timestamp desc'
  9. _rec_name = 'event_type'
  10. timestamp = fields.Integer(string='Mandrill UTC timestamp', readonly=True)
  11. time = fields.Datetime(string='Mandrill time', readonly=True)
  12. date = fields.Date(string='Mandrill date', readonly=True)
  13. event_type = fields.Selection(string='Event type', selection=[
  14. ('send', 'Sent'),
  15. ('deferral', 'Deferral'),
  16. ('hard_bounce', 'Hard bounce'),
  17. ('soft_bounce', 'Soft bounce'),
  18. ('open', 'Opened'),
  19. ('click', 'Clicked'),
  20. ('spam', 'Spam'),
  21. ('unsub', 'Unsubscribed'),
  22. ('reject', 'Rejected'),
  23. ], readonly=True)
  24. url = fields.Char(string='Clicked URL', readonly=True)
  25. ip = fields.Char(string='User IP', readonly=True)
  26. user_agent = fields.Char(string='User agent', readonly=True)
  27. mobile = fields.Boolean(string='Is mobile?', readonly=True)
  28. os_family = fields.Char(string='Operating system family', readonly=True)
  29. ua_family = fields.Char(string='User agent family', readonly=True)
  30. ua_type = fields.Char(string='User agent type', readonly=True)
  31. user_country_id = fields.Many2one(string='User country', readonly=True,
  32. comodel_name='res.country')
  33. message_id = fields.Many2one(string='Message', readonly=True,
  34. comodel_name='mail.mandrill.message')
  35. def _country_search(self, country_code, state_name):
  36. country = False
  37. if country_code:
  38. country = self.env['res.country'].search([
  39. ('code', 'ilike', country_code),
  40. ])
  41. if not country and state_name:
  42. state = self.env['res.country.state'].search([
  43. ('name', 'ilike', state_name),
  44. ])
  45. if state:
  46. country = state.country_id
  47. if country:
  48. return country.id
  49. return False
  50. def _process_bounce(self, message, event, event_type):
  51. msg = event.get('msg')
  52. bounce_type = msg.get('bounce_description', False) if msg else False
  53. bounce_description = msg.get('diag', False) if msg else False
  54. message.write({
  55. 'state': 'bounced',
  56. 'bounce_type': bounce_type,
  57. 'bounce_description': bounce_description,
  58. })
  59. ts = event.get('ts', 0)
  60. time = datetime.datetime.fromtimestamp(ts)
  61. return {
  62. 'message_id': message.id,
  63. 'event_type': event_type,
  64. 'timestamp': ts,
  65. 'time': time.strftime('%Y-%m-%d %H:%M:%S') if ts else False,
  66. 'date': time.strftime('%Y-%m-%d') if ts else False,
  67. }
  68. def _process_status(self, message, event, event_type, state):
  69. message.write({
  70. 'state': state,
  71. })
  72. ts = event.get('ts', 0)
  73. time = datetime.datetime.fromtimestamp(ts)
  74. return {
  75. 'message_id': message.id,
  76. 'event_type': event_type,
  77. 'timestamp': ts,
  78. 'time': time.strftime('%Y-%m-%d %H:%M:%S') if ts else False,
  79. 'date': time.strftime('%Y-%m-%d') if ts else False,
  80. }
  81. def _process_action(self, message, event, event_type, state):
  82. message.write({
  83. 'state': state,
  84. })
  85. ts = event.get('ts', 0)
  86. url = event.get('url', False)
  87. ip = event.get('ip', False)
  88. user_agent = event.get('user_agent', False)
  89. os_family = False
  90. ua_family = False
  91. ua_type = False
  92. mobile = False
  93. country_code = False
  94. state = False
  95. location = event.get('location')
  96. if location:
  97. country_code = location.get('country_short', False)
  98. state = location.get('region', False)
  99. ua_parsed = event.get('user_agent_parsed')
  100. if ua_parsed:
  101. os_family = ua_parsed.get('os_family', False)
  102. ua_family = ua_parsed.get('ua_family', False)
  103. ua_type = ua_parsed.get('type', False)
  104. mobile = ua_parsed.get('mobile', False)
  105. country_id = self._country_search(country_code, state)
  106. time = datetime.datetime.fromtimestamp(ts)
  107. return {
  108. 'message_id': message.id,
  109. 'event_type': event_type,
  110. 'timestamp': ts,
  111. 'time': time.strftime('%Y-%m-%d %H:%M:%S') if ts else False,
  112. 'date': time.strftime('%Y-%m-%d') if ts else False,
  113. 'user_country_id': country_id,
  114. 'ip': ip,
  115. 'url': url,
  116. 'mobile': mobile,
  117. 'user_agent': user_agent,
  118. 'os_family': os_family,
  119. 'ua_family': ua_family,
  120. 'ua_type': ua_type,
  121. }
  122. @api.model
  123. def process_send(self, message, event):
  124. return self._process_status(message, event, 'send', 'sent')
  125. @api.model
  126. def process_deferral(self, message, event):
  127. return self._process_status(message, event, 'deferral', 'deferred')
  128. @api.model
  129. def process_hard_bounce(self, message, event):
  130. return self._process_bounce(message, event, 'hard_bounce')
  131. @api.model
  132. def process_soft_bounce(self, message, event):
  133. return self._process_bounce(message, event, 'soft_bounce')
  134. @api.model
  135. def process_open(self, message, event):
  136. return self._process_action(message, event, 'open', 'opened')
  137. @api.model
  138. def process_click(self, message, event):
  139. return self._process_action(message, event, 'click', 'opened')
  140. @api.model
  141. def process_spam(self, message, event):
  142. return self._process_status(message, event, 'spam', 'spam')
  143. @api.model
  144. def process_unsub(self, message, event):
  145. return self._process_status(message, event, 'unsub', 'unsub')
  146. @api.model
  147. def process_reject(self, message, event):
  148. return self._process_status(message, event, 'reject', 'rejected')