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