-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADD] base_json_request: Module to allow standard JSON requests #3
Open
lasley
wants to merge
1
commit into
OCA:10.0
Choose a base branch
from
LasLabs:release/10.0/base_json_request
base: 10.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<https://github.com/OCA/server-tools/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 <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_. | ||
|
||
Contributors | ||
------------ | ||
|
||
* Dave Lasley <[email protected]> | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from .hooks import post_load |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not |
||
'website': 'https://laslabs.com/', | ||
'author': 'LasLabs, Odoo Community Association (OCA)', | ||
'license': 'LGPL-3', | ||
'installable': True, | ||
'depends': [ | ||
'web', | ||
], | ||
'post_load': 'post_load', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add note that
route
needs to be defined withtype=json