Merge branch 'master' of github.com:mitsuhiko/flask

This commit is contained in:
Armin Ronacher 2012-06-27 12:25:21 +01:00
commit 2e816f554a
21 changed files with 166 additions and 56 deletions

View file

@ -1490,7 +1490,7 @@ class Flask(_PackageBoundObject):
"""
funcs = self.url_default_functions.get(None, ())
if '.' in endpoint:
bp = endpoint.split('.', 1)[0]
bp = endpoint.rsplit('.', 1)[0]
funcs = chain(funcs, self.url_default_functions.get(bp, ()))
for func in funcs:
func(endpoint, values)

View file

@ -40,7 +40,7 @@ def after_this_request(f):
@app.route('/')
def index():
@after_this_request
def add_header():
def add_header(response):
response.headers['X-Foo'] = 'Parachute'
return response
return 'Hello World!'

View file

@ -118,31 +118,9 @@ def jsonify(*args, **kwargs):
information about this, have a look at :ref:`json-security`.
.. versionadded:: 0.2
.. versionadded:: 0.9
If the ``padded`` argument is true, the JSON object will be padded
for JSONP calls and the response mimetype will be changed to
``application/javascript``. By default, the request arguments ``callback``
and ``jsonp`` will be used as the name for the callback function.
This will work with jQuery and most other JavaScript libraries
by default.
If the ``padded`` argument is a string, jsonify will look for
the request argument with the same name and use that value as the
callback-function name.
"""
if __debug__:
_assert_have_json()
if 'padded' in kwargs:
if isinstance(kwargs['padded'], str):
callback = request.args.get(kwargs['padded']) or 'jsonp'
else:
callback = request.args.get('callback') or \
request.args.get('jsonp') or 'jsonp'
del kwargs['padded']
json_str = json.dumps(dict(*args, **kwargs), indent=None)
content = str(callback) + "(" + json_str + ")"
return current_app.response_class(content, mimetype='application/javascript')
return current_app.response_class(json.dumps(dict(*args, **kwargs),
indent=None if request.is_xhr else 2), mimetype='application/json')
@ -529,7 +507,7 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
rv.cache_control.public = True
if cache_timeout is None:
cache_timeout = current_app.get_send_file_max_age(filename)
if cache_timeout:
if cache_timeout is not None:
rv.cache_control.max_age = cache_timeout
rv.expires = int(time() + cache_timeout)

View file

@ -953,6 +953,29 @@ class BasicFunctionalityTestCase(FlaskTestCase):
self.assert_equal(c.get('/de/').data, '/de/about')
self.assert_equal(c.get('/de/about').data, '/foo')
self.assert_equal(c.get('/foo').data, '/en/about')
def test_inject_blueprint_url_defaults(self):
app = flask.Flask(__name__)
bp = flask.Blueprint('foo.bar.baz', __name__,
template_folder='template')
@bp.url_defaults
def bp_defaults(endpoint, values):
values['page'] = 'login'
@bp.route('/<page>')
def view(page): pass
app.register_blueprint(bp)
values = dict()
app.inject_url_defaults('foo.bar.baz.view', values)
expected = dict(page='login')
self.assert_equal(values, expected)
with app.test_request_context('/somepage'):
url = flask.url_for('foo.bar.baz.view')
expected = '/login'
self.assert_equal(url, expected)
def test_debug_mode_complains_after_first_request(self):
app = flask.Flask(__name__)

View file

@ -73,28 +73,11 @@ class JSONTestCase(FlaskTestCase):
@app.route('/dict')
def return_dict():
return flask.jsonify(d)
@app.route("/unpadded")
def return_padded_false():
return flask.jsonify(d, padded=False)
@app.route("/padded")
def return_padded_true():
return flask.jsonify(d, padded=True)
@app.route("/padded_custom")
def return_padded_json_custom_callback():
return flask.jsonify(d, padded='my_func_name')
c = app.test_client()
for url in '/kw', '/dict', '/unpadded':
for url in '/kw', '/dict':
rv = c.get(url)
self.assert_equal(rv.mimetype, 'application/json')
self.assert_equal(flask.json.loads(rv.data), d)
for get_arg in 'callback=funcName', 'jsonp=funcName':
rv = c.get('/padded?' + get_arg)
self.assert_( rv.data.startswith("funcName(") )
self.assert_( rv.data.endswith(")") )
rv_json = rv.data.split('(')[1].split(')')[0]
self.assert_equal(flask.json.loads(rv_json), d)
rv = c.get('/padded_custom?my_func_name=funcName')
self.assert_( rv.data.startswith("funcName(") )
def test_json_attr(self):
app = flask.Flask(__name__)