From 05df3b3029fae62526e31fc11c8eb3667ff06a68 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Fri, 8 Jan 2016 21:29:04 -0600 Subject: [PATCH] smarter request.restful checks content-type for json body --- gluon/globals.py | 26 ++++++++++++++++++-------- gluon/packages/dal | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/gluon/globals.py b/gluon/globals.py index 96ec6225..5581ade8 100644 --- a/gluon/globals.py +++ b/gluon/globals.py @@ -362,20 +362,30 @@ class Request(Storage): redirect(URL(scheme='https', args=self.args, vars=self.vars)) def restful(self): - def wrapper(action, self=self): - def f(_action=action, _self=self, *a, **b): - self.is_restful = True - method = _self.env.request_method - if len(_self.args) and '.' in _self.args[-1]: - _self.args[-1], _, self.extension = self.args[-1].rpartition('.') + def wrapper(action, request=self): + def f(_action=action, *a, **b): + request.is_restful = True + env = request.env + is_json = env.content_type=='application/json' + method = env.request_method + if len(request.args) and '.' in request.args[-1]: + request.args[-1], _, request.extension = request.args[-1].rpartition('.') current.response.headers['Content-Type'] = \ - contenttype('.' + _self.extension.lower()) + contenttype('.' + request.extension.lower()) rest_action = _action().get(method, None) if not (rest_action and method == method.upper() and callable(rest_action)): raise HTTP(405, "method not allowed") try: - return rest_action(*_self.args, **getattr(_self, 'vars', {})) + vars = request.vars + if method == 'POST' and is_json: + body = request.body.read() + if len(body): + vars = sj.loads(body) + res = rest_action(*request.args, **vars) + if is_json and not isinstance(res, str): + res = json(res) + return res except TypeError, e: exc_type, exc_value, exc_traceback = sys.exc_info() if len(traceback.extract_tb(exc_traceback)) == 1: diff --git a/gluon/packages/dal b/gluon/packages/dal index dcfb5f58..598b2e99 160000 --- a/gluon/packages/dal +++ b/gluon/packages/dal @@ -1 +1 @@ -Subproject commit dcfb5f58aa1ff1c87b349c31e67f14a4d9774ebe +Subproject commit 598b2e999f4e32af71f4deb90ced21e5a40c5f9c