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.

86 lines
3.3 KiB

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