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.

52 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2017 Compassion CH (http://www.compassion.ch)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import logging
  5. from odoo.http import JsonRequest, Root, Response
  6. # Monkeypatch type of request rooter to use RESTJsonRequest
  7. old_get_request = Root.get_request
  8. _logger = logging.getLogger(__name__)
  9. try:
  10. import simplejson
  11. except ImportError:
  12. _logger.error("Please install simplejson tu use mail_sendgrid module")
  13. _logger.debug("ImportError details:", exc_info=True)
  14. def get_request(self, httprequest):
  15. if (httprequest.mimetype == "application/json" and
  16. httprequest.environ['PATH_INFO'].startswith('/mail')):
  17. return RESTJsonRequest(httprequest)
  18. return old_get_request(self, httprequest)
  19. Root.get_request = get_request
  20. class RESTJsonRequest(JsonRequest):
  21. """ Special RestJson Handler to enable receiving lists in JSON
  22. body
  23. """
  24. def __init__(self, *args):
  25. try:
  26. super(RESTJsonRequest, self).__init__(*args)
  27. except AttributeError:
  28. # The JSON may contain a list
  29. self.params = dict()
  30. self.context = dict(self.session.context)
  31. def _json_response(self, result=None, error=None):
  32. response = {}
  33. if error is not None:
  34. response['error'] = error
  35. if result is not None:
  36. response['result'] = result
  37. mime = 'application/json'
  38. body = simplejson.dumps(response)
  39. return Response(
  40. body, headers=[('Content-Type', mime),
  41. ('Content-Length', len(body))])