From 192adb47e6c637c94bf0f29188dbd00216cf5223 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Tue, 10 Oct 2017 12:15:47 -0700 Subject: [PATCH] [ADD] base_json_request: Module to allow standard JSON requests * Create a module that circumvents the JSON-RPC requirement for all requests with application/json --- base_json_request/README.rst | 50 ++++++++++++++++++++++++++ base_json_request/__init__.py | 4 +++ base_json_request/__manifest__.py | 18 ++++++++++ base_json_request/hooks.py | 13 +++++++ base_json_request/http.py | 60 +++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 base_json_request/README.rst create mode 100644 base_json_request/__init__.py create mode 100644 base_json_request/__manifest__.py create mode 100644 base_json_request/hooks.py create mode 100644 base_json_request/http.py diff --git a/base_json_request/README.rst b/base_json_request/README.rst new file mode 100644 index 0000000..5451ed3 --- /dev/null +++ b/base_json_request/README.rst @@ -0,0 +1,50 @@ +.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 + +================= +Base JSON Request +================= + +This module allows you to receive JSON requests in Odoo that are not +RPC. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/210/10.0 + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Dave Lasley + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/base_json_request/__init__.py b/base_json_request/__init__.py new file mode 100644 index 0000000..c484ab2 --- /dev/null +++ b/base_json_request/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from .hooks import post_load diff --git a/base_json_request/__manifest__.py b/base_json_request/__manifest__.py new file mode 100644 index 0000000..05ee396 --- /dev/null +++ b/base_json_request/__manifest__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + 'name': 'Base JSON Request', + 'summary': 'Allows you to receive JSON requests that are not RPC.', + 'version': '10.0.1.0.0', + 'category': 'Authentication', + 'website': 'https://laslabs.com/', + 'author': 'LasLabs, Odoo Community Association (OCA)', + 'license': 'LGPL-3', + 'installable': True, + 'depends': [ + 'web', + ], + 'post_load': 'post_load', +} diff --git a/base_json_request/hooks.py b/base_json_request/hooks.py new file mode 100644 index 0000000..84e827e --- /dev/null +++ b/base_json_request/hooks.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import http + +from .http import _handle_exception, __init__ + + +def post_load(): + """Monkey patch HTTP methods.""" + http.JsonRequest._handle_exception = _handle_exception + http.JsonRequest.__init__ = __init__ diff --git a/base_json_request/http.py b/base_json_request/http.py new file mode 100644 index 0000000..f5b1713 --- /dev/null +++ b/base_json_request/http.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +import json + +from werkzeug.exceptions import BadRequest + +from odoo import http + + +old_handle_exception = http.JsonRequest._handle_exception +old_init = http.JsonRequest.__init__ + + +def __init__(self, *args): + try: + old_init(self, *args) + except BadRequest as e: + try: + args = self.httprequest.args + self.jsonrequest = args + self.params = json.loads(self.jsonrequest.get('params', "{}")) + self.context = self.params.pop('context', + dict(self.session.context)) + except ValueError: + raise e + + +def _handle_exception(self, exception): + """ Override the original method to handle Werkzeug exceptions. + + Args: + exception (Exception): Exception object that is being thrown. + + Returns: + BaseResponse: JSON Response. + """ + + # For some reason a try/except here still raised... + code = getattr(exception, 'code', None) + if code is None: + return old_handle_exception( + self, exception, + ) + + error = { + 'data': http.serialize_exception(exception), + 'code': code, + } + + try: + error['message'] = exception.description + except AttributeError: + try: + error['message'] = exception.message + except AttributeError: + error['message'] = 'Internal Server Error' + + return self._json_response(error=error)