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.

47 lines
1.4 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 json
  5. import logging
  6. from odoo.http import JsonRequest, Root, Response
  7. # Monkeypatch type of request rooter to use RESTJsonRequest
  8. old_get_request = Root.get_request
  9. _logger = logging.getLogger(__name__)
  10. def get_request(self, httprequest):
  11. if (httprequest.mimetype == "application/json" and
  12. httprequest.environ['PATH_INFO'].startswith('/mail')):
  13. return RESTJsonRequest(httprequest)
  14. return old_get_request(self, httprequest)
  15. Root.get_request = get_request
  16. class RESTJsonRequest(JsonRequest):
  17. """ Special RestJson Handler to enable receiving lists in JSON
  18. body
  19. """
  20. def __init__(self, *args):
  21. try:
  22. super(RESTJsonRequest, self).__init__(*args)
  23. except AttributeError:
  24. # The JSON may contain a list
  25. self.params = dict()
  26. self.context = dict(self.session.context)
  27. def _json_response(self, result=None, error=None):
  28. response = {}
  29. if error is not None:
  30. response['error'] = error
  31. if result is not None:
  32. response['result'] = result
  33. mime = 'application/json'
  34. body = json.dumps(response)
  35. return Response(
  36. body, headers=[('Content-Type', mime),
  37. ('Content-Length', len(body))])