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.

53 lines
1.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. class MailTrackingController(http.Controller):
  11. def _request_metadata(self):
  12. request = http.request.httprequest
  13. return {
  14. 'ip': request.remote_addr,
  15. 'user_agent': request.user_agent,
  16. 'os_family': request.user_agent.platform,
  17. 'ua_family': request.user_agent.browser,
  18. }
  19. @http.route('/mail/tracking/open/<string:db>'
  20. '/<int:tracking_email_id>/blank.gif',
  21. type='http', auth='none')
  22. def mail_tracking_open(self, db, tracking_email_id, **kw):
  23. reg = False
  24. try:
  25. reg = registry(db)
  26. except OperationalError:
  27. _logger.warning("Selected BD '%s' not found", db)
  28. except:
  29. _logger.warning("Selected BD '%s' connection error", db)
  30. if reg:
  31. with reg.cursor() as cr:
  32. env = api.Environment(cr, SUPERUSER_ID, {})
  33. tracking_email = env['mail.tracking.email'].search([
  34. ('id', '=', tracking_email_id),
  35. ])
  36. if tracking_email:
  37. metadata = self._request_metadata()
  38. tracking_email.event_process('open', metadata)
  39. else:
  40. _logger.warning(
  41. "MailTracking email '%s' not found", tracking_email_id)
  42. # Always return GIF blank image
  43. response = werkzeug.wrappers.Response()
  44. response.mimetype = 'image/gif'
  45. response.data = BLANK.decode('base64')
  46. return response