Browse Source

Added make_response

pull/112/head
Armin Ronacher 15 years ago
parent
commit
6fc1492357
  1. 2
      CHANGES
  2. 2
      docs/api.rst
  3. 2
      flask/__init__.py
  4. 42
      flask/helpers.py
  5. 18
      tests/flask_tests.py

2
CHANGES

@ -26,6 +26,8 @@ Release date to be announced, codename to be decided.
- the endpoint for the :meth:`flask.Module.add_url_rule` method - the endpoint for the :meth:`flask.Module.add_url_rule` method
is now optional to be consistent with the function of the is now optional to be consistent with the function of the
same name on the application object. same name on the application object.
- added a :func:`flask.make_response` function that simplifies
creating response object instances in views.
Version 0.5.2 Version 0.5.2
------------- -------------

2
docs/api.rst

@ -228,6 +228,8 @@ Useful Functions and Classes
.. autofunction:: redirect .. autofunction:: redirect
.. autofunction:: make_response
.. autofunction:: send_file .. autofunction:: send_file
.. autofunction:: send_from_directory .. autofunction:: send_from_directory

2
flask/__init__.py

@ -19,7 +19,7 @@ from .app import Flask, Request, Response
from .config import Config from .config import Config
from .helpers import url_for, jsonify, json_available, flash, \ from .helpers import url_for, jsonify, json_available, flash, \
send_file, send_from_directory, get_flashed_messages, \ send_file, send_from_directory, get_flashed_messages, \
get_template_attribute get_template_attribute, make_response
from .globals import current_app, g, request, session, _request_ctx_stack from .globals import current_app, g, request, session, _request_ctx_stack
from .module import Module from .module import Module
from .templating import render_template, render_template_string from .templating import render_template, render_template_string

42
flask/helpers.py

@ -101,6 +101,48 @@ def jsonify(*args, **kwargs):
indent=None if request.is_xhr else 2), mimetype='application/json') indent=None if request.is_xhr else 2), mimetype='application/json')
def make_response(*args):
"""Sometimes it is necessary to set additional headers in a view. Because
views do not have to return response objects but can return a value that
is converted into a response object by Flask itself, it becomes tricky to
add headers to it. This function can be called instead of using a return
and you will get a response object which you can use to attach headers.
If view looked like this and you want to add a new header::
def index():
return render_template('index.html', foo=42)
You can now do something like this::
def index():
response = make_response(render_template('index.html', foo=42))
response.headers['X-Parachutes'] = 'parachutes are cool'
return response
This function accepts the very same arguments you can return from a
view function. This for example creates a response with a 404 error
code::
response = make_response(render_template('not_found.html', 404))
Internally this function does the following things:
- if no arguments are passed, it creates a new response argument
- if one argument is passed, :meth:`flask.Flask.make_response`
is invoked with it.
- if more than one argument is passed, the arguments are passed
to the :meth:`flask.Flask.make_response` function as tuple.
.. versionadded:: 0.6
"""
if not args:
return current_app.response_class()
if len(args) == 1:
args = args[0]
return current_app.make_response(args)
def url_for(endpoint, **values): def url_for(endpoint, **values):
"""Generates a URL to the given endpoint with the method provided. """Generates a URL to the given endpoint with the method provided.
The endpoint is relative to the active module if modules are in use. The endpoint is relative to the active module if modules are in use.

18
tests/flask_tests.py

@ -393,6 +393,24 @@ class BasicFunctionalityTestCase(unittest.TestCase):
assert rv.status_code == 400 assert rv.status_code == 400
assert rv.mimetype == 'text/plain' assert rv.mimetype == 'text/plain'
def test_make_response(self):
app = flask.Flask(__name__)
with app.test_request_context():
rv = flask.make_response()
assert rv.status_code == 200
assert rv.data == ''
assert rv.mimetype == 'text/html'
rv = flask.make_response('Awesome')
assert rv.status_code == 200
assert rv.data == 'Awesome'
assert rv.mimetype == 'text/html'
rv = flask.make_response('W00t', 404)
assert rv.status_code == 404
assert rv.data == 'W00t'
assert rv.mimetype == 'text/html'
def test_url_generation(self): def test_url_generation(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
@app.route('/hello/<name>', methods=['POST']) @app.route('/hello/<name>', methods=['POST'])

Loading…
Cancel
Save