From f195459cf1ed2a4639766c0f87c70c6d47269dcd Mon Sep 17 00:00:00 2001 From: elishowk Date: Wed, 1 Jun 2011 18:06:25 +0200 Subject: [PATCH 1/2] added status code argument through jsonify --- flask/helpers.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/flask/helpers.py b/flask/helpers.py index a25dcadd..40dd4089 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -74,7 +74,7 @@ def _endpoint_from_view_func(view_func): return view_func.__name__ -def jsonify(*args, **kwargs): +def jsonify(status, *args, **kwargs): """Creates a :class:`~flask.Response` with the JSON representation of the given arguments with an `application/json` mimetype. The arguments to this function are the same as to the :class:`dict` constructor. @@ -83,7 +83,7 @@ def jsonify(*args, **kwargs): @app.route('/_get_current_user') def get_current_user(): - return jsonify(username=g.user.username, + return jsonify(200, username=g.user.username, email=g.user.email, id=g.user.id) @@ -104,7 +104,8 @@ def jsonify(*args, **kwargs): if __debug__: _assert_have_json() return current_app.response_class(json.dumps(dict(*args, **kwargs), - indent=None if request.is_xhr else 2), mimetype='application/json') + indent=None if request.is_xhr else 2), mimetype='application/json', + status=status) def make_response(*args): From 29169e52eea00d7ba7f1e5274d5a42c9440f6ba4 Mon Sep 17 00:00:00 2001 From: elishowk Date: Wed, 1 Jun 2011 18:09:43 +0200 Subject: [PATCH 2/2] updated test for jsonify status code argument --- tests/flask_tests.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/flask_tests.py b/tests/flask_tests.py index b723f217..e2aa6792 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -815,18 +815,20 @@ class JSONTestCase(unittest.TestCase): def test_jsonify(self): d = dict(a=23, b=42, c=[1, 2, 3]) + status=200 app = flask.Flask(__name__) @app.route('/kw') def return_kwargs(): - return flask.jsonify(**d) + return flask.jsonify(status, **d) @app.route('/dict') def return_dict(): - return flask.jsonify(d) + return flask.jsonify(status, d) c = app.test_client() for url in '/kw', '/dict': rv = c.get(url) assert rv.mimetype == 'application/json' assert flask.json.loads(rv.data) == d + assert rv.status_code == status def test_json_attr(self): app = flask.Flask(__name__)