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.

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