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.

87 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 werkzeug
  5. from psycopg2 import OperationalError
  6. from openerp import api, http, registry, SUPERUSER_ID
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. BLANK = 'R0lGODlhAQABAIAAANvf7wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
  10. def _env_get(db, callback, tracking_id, event_type, **kw):
  11. res = 'NOT FOUND'
  12. reg = False
  13. current = http.request.db and db == http.request.db
  14. env = current and http.request.env
  15. if not env:
  16. with api.Environment.manage():
  17. try:
  18. reg = registry(db)
  19. except OperationalError:
  20. _logger.warning("Selected BD '%s' not found", db)
  21. except: # pragma: no cover
  22. _logger.warning("Selected BD '%s' connection error", db)
  23. if reg:
  24. _logger.info("New environment for database '%s'", db)
  25. with reg.cursor() as new_cr:
  26. new_env = api.Environment(new_cr, SUPERUSER_ID, {})
  27. res = callback(new_env, tracking_id, event_type, **kw)
  28. new_env.cr.commit()
  29. else:
  30. # make sudo when reusing environment
  31. env = env(user=SUPERUSER_ID)
  32. res = callback(env, tracking_id, event_type, **kw)
  33. return res
  34. class MailTrackingController(http.Controller):
  35. def _request_metadata(self):
  36. request = http.request.httprequest
  37. return {
  38. 'ip': request.remote_addr or False,
  39. 'user_agent': request.user_agent or False,
  40. 'os_family': request.user_agent.platform or False,
  41. 'ua_family': request.user_agent.browser or False,
  42. }
  43. def _tracking_open(self, env, tracking_id, event_type, **kw):
  44. tracking_email = env['mail.tracking.email'].search([
  45. ('id', '=', tracking_id),
  46. ])
  47. if tracking_email:
  48. metadata = self._request_metadata()
  49. tracking_email.event_create('open', metadata)
  50. else:
  51. _logger.warning(
  52. "MailTracking email '%s' not found", tracking_id)
  53. def _tracking_event(self, env, tracking_id, event_type, **kw):
  54. metadata = self._request_metadata()
  55. return env['mail.tracking.email'].event_process(
  56. http.request, kw, metadata, event_type=event_type)
  57. @http.route('/mail/tracking/all/<string:db>',
  58. type='http', auth='none')
  59. def mail_tracking_all(self, db, **kw):
  60. return _env_get(db, self._tracking_event, None, None, **kw)
  61. @http.route('/mail/tracking/event/<string:db>/<string:event_type>',
  62. type='http', auth='none')
  63. def mail_tracking_event(self, db, event_type, **kw):
  64. return _env_get(db, self._tracking_event, None, event_type, **kw)
  65. @http.route('/mail/tracking/open/<string:db>'
  66. '/<int:tracking_email_id>/blank.gif',
  67. type='http', auth='none')
  68. def mail_tracking_open(self, db, tracking_email_id, **kw):
  69. _env_get(db, self._tracking_open, tracking_email_id, None, **kw)
  70. # Always return GIF blank image
  71. response = werkzeug.wrappers.Response()
  72. response.mimetype = 'image/gif'
  73. response.data = BLANK.decode('base64')
  74. return response