|
|
@ -9,6 +9,8 @@ |
|
|
|
:license: BSD, see LICENSE for more details. |
|
|
|
:license: BSD, see LICENSE for more details. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import pytest |
|
|
|
|
|
|
|
|
|
|
|
import flask |
|
|
|
import flask |
|
|
|
import unittest |
|
|
|
import unittest |
|
|
|
import logging |
|
|
|
import logging |
|
|
@ -17,334 +19,324 @@ from jinja2 import TemplateNotFound |
|
|
|
from tests import TestFlask |
|
|
|
from tests import TestFlask |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestTemplating(TestFlask): |
|
|
|
def test_context_processing(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_context_processing(self): |
|
|
|
@app.context_processor |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def context_processor(): |
|
|
|
@app.context_processor |
|
|
|
return {'injected_value': 42} |
|
|
|
def context_processor(): |
|
|
|
@app.route('/') |
|
|
|
return {'injected_value': 42} |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('context_template.html', value=23) |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('context_template.html', value=23) |
|
|
|
assert rv.data == b'<p>23|42' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'<p>23|42' |
|
|
|
def test_original_win(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_original_win(self): |
|
|
|
@app.route('/') |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template_string('{{ config }}', config=42) |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template_string('{{ config }}', config=42) |
|
|
|
assert rv.data == b'42' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'42' |
|
|
|
def test_request_less_rendering(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_request_less_rendering(self): |
|
|
|
app.config['WORLD_NAME'] = 'Special World' |
|
|
|
app = flask.Flask(__name__) |
|
|
|
@app.context_processor |
|
|
|
app.config['WORLD_NAME'] = 'Special World' |
|
|
|
def context_processor(): |
|
|
|
@app.context_processor |
|
|
|
return dict(foo=42) |
|
|
|
def context_processor(): |
|
|
|
|
|
|
|
return dict(foo=42) |
|
|
|
with app.app_context(): |
|
|
|
|
|
|
|
rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} ' |
|
|
|
with app.app_context(): |
|
|
|
'{{ foo }}') |
|
|
|
rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} ' |
|
|
|
assert rv == 'Hello Special World 42' |
|
|
|
'{{ foo }}') |
|
|
|
|
|
|
|
assert rv == 'Hello Special World 42' |
|
|
|
def test_standard_context(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_standard_context(self): |
|
|
|
app.secret_key = 'development key' |
|
|
|
app = flask.Flask(__name__) |
|
|
|
@app.route('/') |
|
|
|
app.secret_key = 'development key' |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
flask.g.foo = 23 |
|
|
|
def index(): |
|
|
|
flask.session['test'] = 'aha' |
|
|
|
flask.g.foo = 23 |
|
|
|
return flask.render_template_string(''' |
|
|
|
flask.session['test'] = 'aha' |
|
|
|
{{ request.args.foo }} |
|
|
|
return flask.render_template_string(''' |
|
|
|
{{ g.foo }} |
|
|
|
{{ request.args.foo }} |
|
|
|
{{ config.DEBUG }} |
|
|
|
{{ g.foo }} |
|
|
|
{{ session.test }} |
|
|
|
{{ config.DEBUG }} |
|
|
|
''') |
|
|
|
{{ session.test }} |
|
|
|
rv = app.test_client().get('/?foo=42') |
|
|
|
''') |
|
|
|
assert rv.data.split() == [b'42', b'23', b'False', b'aha'] |
|
|
|
rv = app.test_client().get('/?foo=42') |
|
|
|
|
|
|
|
assert rv.data.split() == [b'42', b'23', b'False', b'aha'] |
|
|
|
def test_escaping(): |
|
|
|
|
|
|
|
text = '<p>Hello World!' |
|
|
|
def test_escaping(self): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
text = '<p>Hello World!' |
|
|
|
@app.route('/') |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('escaping_template.html', text=text, |
|
|
|
def index(): |
|
|
|
html=flask.Markup(text)) |
|
|
|
return flask.render_template('escaping_template.html', text=text, |
|
|
|
lines = app.test_client().get('/').data.splitlines() |
|
|
|
html=flask.Markup(text)) |
|
|
|
assert lines == [ |
|
|
|
lines = app.test_client().get('/').data.splitlines() |
|
|
|
b'<p>Hello World!', |
|
|
|
assert lines == [ |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!', |
|
|
|
b'<p>Hello World!' |
|
|
|
b'<p>Hello World!', |
|
|
|
] |
|
|
|
b'<p>Hello World!' |
|
|
|
|
|
|
|
] |
|
|
|
def test_no_escaping(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_no_escaping(self): |
|
|
|
with app.test_request_context(): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
assert flask.render_template_string( |
|
|
|
with app.test_request_context(): |
|
|
|
'{{ foo }}', foo='<test>') == '<test>' |
|
|
|
assert flask.render_template_string( |
|
|
|
assert flask.render_template('mail.txt', foo='<test>') == \ |
|
|
|
'{{ foo }}', foo='<test>') == '<test>' |
|
|
|
'<test> Mail' |
|
|
|
assert flask.render_template('mail.txt', foo='<test>') == \ |
|
|
|
|
|
|
|
'<test> Mail' |
|
|
|
def test_macros(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_macros(self): |
|
|
|
with app.test_request_context(): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
macro = flask.get_template_attribute('_macro.html', 'hello') |
|
|
|
with app.test_request_context(): |
|
|
|
assert macro('World') == 'Hello World!' |
|
|
|
macro = flask.get_template_attribute('_macro.html', 'hello') |
|
|
|
|
|
|
|
assert macro('World') == 'Hello World!' |
|
|
|
def test_template_filter(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_filter(self): |
|
|
|
@app.template_filter() |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def my_reverse(s): |
|
|
|
@app.template_filter() |
|
|
|
return s[::-1] |
|
|
|
def my_reverse(s): |
|
|
|
assert 'my_reverse' in app.jinja_env.filters.keys() |
|
|
|
return s[::-1] |
|
|
|
assert app.jinja_env.filters['my_reverse'] == my_reverse |
|
|
|
assert 'my_reverse' in app.jinja_env.filters.keys() |
|
|
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' |
|
|
|
assert app.jinja_env.filters['my_reverse'] == my_reverse |
|
|
|
|
|
|
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' |
|
|
|
def test_add_template_filter(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_filter(self): |
|
|
|
def my_reverse(s): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return s[::-1] |
|
|
|
def my_reverse(s): |
|
|
|
app.add_template_filter(my_reverse) |
|
|
|
return s[::-1] |
|
|
|
assert 'my_reverse' in app.jinja_env.filters.keys() |
|
|
|
app.add_template_filter(my_reverse) |
|
|
|
assert app.jinja_env.filters['my_reverse'] == my_reverse |
|
|
|
assert 'my_reverse' in app.jinja_env.filters.keys() |
|
|
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' |
|
|
|
assert app.jinja_env.filters['my_reverse'] == my_reverse |
|
|
|
|
|
|
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' |
|
|
|
def test_template_filter_with_name(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_filter_with_name(self): |
|
|
|
@app.template_filter('strrev') |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def my_reverse(s): |
|
|
|
@app.template_filter('strrev') |
|
|
|
return s[::-1] |
|
|
|
def my_reverse(s): |
|
|
|
assert 'strrev' in app.jinja_env.filters.keys() |
|
|
|
return s[::-1] |
|
|
|
assert app.jinja_env.filters['strrev'] == my_reverse |
|
|
|
assert 'strrev' in app.jinja_env.filters.keys() |
|
|
|
assert app.jinja_env.filters['strrev']('abcd') == 'dcba' |
|
|
|
assert app.jinja_env.filters['strrev'] == my_reverse |
|
|
|
|
|
|
|
assert app.jinja_env.filters['strrev']('abcd') == 'dcba' |
|
|
|
def test_add_template_filter_with_name(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_filter_with_name(self): |
|
|
|
def my_reverse(s): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return s[::-1] |
|
|
|
def my_reverse(s): |
|
|
|
app.add_template_filter(my_reverse, 'strrev') |
|
|
|
return s[::-1] |
|
|
|
assert 'strrev' in app.jinja_env.filters.keys() |
|
|
|
app.add_template_filter(my_reverse, 'strrev') |
|
|
|
assert app.jinja_env.filters['strrev'] == my_reverse |
|
|
|
assert 'strrev' in app.jinja_env.filters.keys() |
|
|
|
assert app.jinja_env.filters['strrev']('abcd') == 'dcba' |
|
|
|
assert app.jinja_env.filters['strrev'] == my_reverse |
|
|
|
|
|
|
|
assert app.jinja_env.filters['strrev']('abcd') == 'dcba' |
|
|
|
def test_template_filter_with_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_filter_with_template(self): |
|
|
|
@app.template_filter() |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def super_reverse(s): |
|
|
|
@app.template_filter() |
|
|
|
return s[::-1] |
|
|
|
def super_reverse(s): |
|
|
|
@app.route('/') |
|
|
|
return s[::-1] |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
assert rv.data == b'dcba' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
def test_add_template_filter_with_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_filter_with_template(self): |
|
|
|
def super_reverse(s): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return s[::-1] |
|
|
|
def super_reverse(s): |
|
|
|
app.add_template_filter(super_reverse) |
|
|
|
return s[::-1] |
|
|
|
@app.route('/') |
|
|
|
app.add_template_filter(super_reverse) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
assert rv.data == b'dcba' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
def test_template_filter_with_name_and_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_filter_with_name_and_template(self): |
|
|
|
@app.template_filter('super_reverse') |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def my_reverse(s): |
|
|
|
@app.template_filter('super_reverse') |
|
|
|
return s[::-1] |
|
|
|
def my_reverse(s): |
|
|
|
@app.route('/') |
|
|
|
return s[::-1] |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
assert rv.data == b'dcba' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
def test_add_template_filter_with_name_and_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_filter_with_name_and_template(self): |
|
|
|
def my_reverse(s): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return s[::-1] |
|
|
|
def my_reverse(s): |
|
|
|
app.add_template_filter(my_reverse, 'super_reverse') |
|
|
|
return s[::-1] |
|
|
|
@app.route('/') |
|
|
|
app.add_template_filter(my_reverse, 'super_reverse') |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_filter.html', value='abcd') |
|
|
|
assert rv.data == b'dcba' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
def test_template_test(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_test(self): |
|
|
|
@app.template_test() |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def boolean(value): |
|
|
|
@app.template_test() |
|
|
|
return isinstance(value, bool) |
|
|
|
def boolean(value): |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
return isinstance(value, bool) |
|
|
|
assert app.jinja_env.tests['boolean'] == boolean |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
assert app.jinja_env.tests['boolean'] == boolean |
|
|
|
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
def test_add_template_test(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_test(self): |
|
|
|
def boolean(value): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return isinstance(value, bool) |
|
|
|
def boolean(value): |
|
|
|
app.add_template_test(boolean) |
|
|
|
return isinstance(value, bool) |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
app.add_template_test(boolean) |
|
|
|
assert app.jinja_env.tests['boolean'] == boolean |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
assert app.jinja_env.tests['boolean'] == boolean |
|
|
|
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
def test_template_test_with_name(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_test_with_name(self): |
|
|
|
@app.template_test('boolean') |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def is_boolean(value): |
|
|
|
@app.template_test('boolean') |
|
|
|
return isinstance(value, bool) |
|
|
|
def is_boolean(value): |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
return isinstance(value, bool) |
|
|
|
assert app.jinja_env.tests['boolean'] == is_boolean |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
assert app.jinja_env.tests['boolean'] == is_boolean |
|
|
|
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
def test_add_template_test_with_name(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_test_with_name(self): |
|
|
|
def is_boolean(value): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return isinstance(value, bool) |
|
|
|
def is_boolean(value): |
|
|
|
app.add_template_test(is_boolean, 'boolean') |
|
|
|
return isinstance(value, bool) |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
app.add_template_test(is_boolean, 'boolean') |
|
|
|
assert app.jinja_env.tests['boolean'] == is_boolean |
|
|
|
assert 'boolean' in app.jinja_env.tests.keys() |
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
assert app.jinja_env.tests['boolean'] == is_boolean |
|
|
|
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
def test_template_test_with_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_test_with_template(self): |
|
|
|
@app.template_test() |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def boolean(value): |
|
|
|
@app.template_test() |
|
|
|
return isinstance(value, bool) |
|
|
|
def boolean(value): |
|
|
|
@app.route('/') |
|
|
|
return isinstance(value, bool) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
assert b'Success!' in rv.data |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
def test_add_template_test_with_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_test_with_template(self): |
|
|
|
def boolean(value): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return isinstance(value, bool) |
|
|
|
def boolean(value): |
|
|
|
app.add_template_test(boolean) |
|
|
|
return isinstance(value, bool) |
|
|
|
@app.route('/') |
|
|
|
app.add_template_test(boolean) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
assert b'Success!' in rv.data |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
def test_template_test_with_name_and_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_template_test_with_name_and_template(self): |
|
|
|
@app.template_test('boolean') |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def is_boolean(value): |
|
|
|
@app.template_test('boolean') |
|
|
|
return isinstance(value, bool) |
|
|
|
def is_boolean(value): |
|
|
|
@app.route('/') |
|
|
|
return isinstance(value, bool) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
assert b'Success!' in rv.data |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
def test_add_template_test_with_name_and_template(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_test_with_name_and_template(self): |
|
|
|
def is_boolean(value): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
return isinstance(value, bool) |
|
|
|
def is_boolean(value): |
|
|
|
app.add_template_test(is_boolean, 'boolean') |
|
|
|
return isinstance(value, bool) |
|
|
|
@app.route('/') |
|
|
|
app.add_template_test(is_boolean, 'boolean') |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
def index(): |
|
|
|
rv = app.test_client().get('/') |
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
assert b'Success!' in rv.data |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
def test_add_template_global(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_add_template_global(self): |
|
|
|
@app.template_global() |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def get_stuff(): |
|
|
|
@app.template_global() |
|
|
|
return 42 |
|
|
|
def get_stuff(): |
|
|
|
assert 'get_stuff' in app.jinja_env.globals.keys() |
|
|
|
return 42 |
|
|
|
assert app.jinja_env.globals['get_stuff'] == get_stuff |
|
|
|
assert 'get_stuff' in app.jinja_env.globals.keys() |
|
|
|
assert app.jinja_env.globals['get_stuff'](), 42 |
|
|
|
assert app.jinja_env.globals['get_stuff'] == get_stuff |
|
|
|
with app.app_context(): |
|
|
|
assert app.jinja_env.globals['get_stuff'](), 42 |
|
|
|
rv = flask.render_template_string('{{ get_stuff() }}') |
|
|
|
with app.app_context(): |
|
|
|
assert rv == '42' |
|
|
|
rv = flask.render_template_string('{{ get_stuff() }}') |
|
|
|
|
|
|
|
assert rv == '42' |
|
|
|
def test_custom_template_loader(): |
|
|
|
|
|
|
|
class MyFlask(flask.Flask): |
|
|
|
def test_custom_template_loader(self): |
|
|
|
def create_global_jinja_loader(self): |
|
|
|
class MyFlask(flask.Flask): |
|
|
|
from jinja2 import DictLoader |
|
|
|
def create_global_jinja_loader(self): |
|
|
|
return DictLoader({'index.html': 'Hello Custom World!'}) |
|
|
|
from jinja2 import DictLoader |
|
|
|
app = MyFlask(__name__) |
|
|
|
return DictLoader({'index.html': 'Hello Custom World!'}) |
|
|
|
@app.route('/') |
|
|
|
app = MyFlask(__name__) |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template('index.html') |
|
|
|
def index(): |
|
|
|
c = app.test_client() |
|
|
|
return flask.render_template('index.html') |
|
|
|
rv = c.get('/') |
|
|
|
c = app.test_client() |
|
|
|
assert rv.data == b'Hello Custom World!' |
|
|
|
rv = c.get('/') |
|
|
|
|
|
|
|
assert rv.data == b'Hello Custom World!' |
|
|
|
def test_iterable_loader(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_iterable_loader(self): |
|
|
|
@app.context_processor |
|
|
|
app = flask.Flask(__name__) |
|
|
|
def context_processor(): |
|
|
|
@app.context_processor |
|
|
|
return {'whiskey': 'Jameson'} |
|
|
|
def context_processor(): |
|
|
|
@app.route('/') |
|
|
|
return {'whiskey': 'Jameson'} |
|
|
|
def index(): |
|
|
|
@app.route('/') |
|
|
|
return flask.render_template( |
|
|
|
def index(): |
|
|
|
['no_template.xml', # should skip this one |
|
|
|
return flask.render_template( |
|
|
|
'simple_template.html', # should render this |
|
|
|
['no_template.xml', # should skip this one |
|
|
|
'context_template.html'], |
|
|
|
'simple_template.html', # should render this |
|
|
|
value=23) |
|
|
|
'context_template.html'], |
|
|
|
|
|
|
|
value=23) |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'<h1>Jameson</h1>' |
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
|
|
|
assert rv.data == b'<h1>Jameson</h1>' |
|
|
|
def test_templates_auto_reload(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
def test_templates_auto_reload(self): |
|
|
|
assert app.config['TEMPLATES_AUTO_RELOAD'] |
|
|
|
app = flask.Flask(__name__) |
|
|
|
assert app.jinja_env.auto_reload |
|
|
|
assert app.config['TEMPLATES_AUTO_RELOAD'] |
|
|
|
app = flask.Flask(__name__) |
|
|
|
assert app.jinja_env.auto_reload |
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = False |
|
|
|
app = flask.Flask(__name__) |
|
|
|
assert not app.jinja_env.auto_reload |
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = False |
|
|
|
|
|
|
|
assert not app.jinja_env.auto_reload |
|
|
|
def test_template_loader_debugging(): |
|
|
|
|
|
|
|
from blueprintapp import app |
|
|
|
def test_template_loader_debugging(self): |
|
|
|
|
|
|
|
from blueprintapp import app |
|
|
|
called = [] |
|
|
|
|
|
|
|
class _TestHandler(logging.Handler): |
|
|
|
called = [] |
|
|
|
def handle(x, record): |
|
|
|
class _TestHandler(logging.Handler): |
|
|
|
called.append(True) |
|
|
|
def handle(x, record): |
|
|
|
text = str(record.msg) |
|
|
|
called.append(True) |
|
|
|
assert '1: trying loader of application "blueprintapp"' in text |
|
|
|
text = str(record.msg) |
|
|
|
assert ('2: trying loader of blueprint "admin" ' |
|
|
|
assert '1: trying loader of application "blueprintapp"' in text |
|
|
|
'(blueprintapp.apps.admin)') in text |
|
|
|
assert ('2: trying loader of blueprint "admin" ' |
|
|
|
assert ('trying loader of blueprint "frontend" ' |
|
|
|
'(blueprintapp.apps.admin)') in text |
|
|
|
'(blueprintapp.apps.frontend)') in text |
|
|
|
assert ('trying loader of blueprint "frontend" ' |
|
|
|
assert 'Error: the template could not be found' in text |
|
|
|
'(blueprintapp.apps.frontend)') in text |
|
|
|
assert ('looked up from an endpoint that belongs to ' |
|
|
|
assert 'Error: the template could not be found' in text |
|
|
|
'the blueprint "frontend"') in text |
|
|
|
assert ('looked up from an endpoint that belongs to ' |
|
|
|
assert 'See http://flask.pocoo.org/docs/blueprints/#templates' in text |
|
|
|
'the blueprint "frontend"') in text |
|
|
|
|
|
|
|
assert 'See http://flask.pocoo.org/docs/blueprints/#templates' in text |
|
|
|
with app.test_client() as c: |
|
|
|
|
|
|
|
try: |
|
|
|
with app.test_client() as c: |
|
|
|
old_load_setting = app.config['EXPLAIN_TEMPLATE_LOADING'] |
|
|
|
try: |
|
|
|
old_handlers = app.logger.handlers[:] |
|
|
|
old_load_setting = app.config['EXPLAIN_TEMPLATE_LOADING'] |
|
|
|
app.logger.handlers = [_TestHandler()] |
|
|
|
old_handlers = app.logger.handlers[:] |
|
|
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = True |
|
|
|
app.logger.handlers = [_TestHandler()] |
|
|
|
|
|
|
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = True |
|
|
|
with pytest.raises(TemplateNotFound) as excinfo: |
|
|
|
|
|
|
|
c.get('/missing') |
|
|
|
try: |
|
|
|
|
|
|
|
c.get('/missing') |
|
|
|
assert 'missing_template.html' in str(excinfo.value) |
|
|
|
except TemplateNotFound as e: |
|
|
|
finally: |
|
|
|
assert 'missing_template.html' in str(e) |
|
|
|
app.logger.handlers[:] = old_handlers |
|
|
|
else: |
|
|
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting |
|
|
|
self.fail('Expected template not found exception.') |
|
|
|
|
|
|
|
finally: |
|
|
|
assert len(called) == 1 |
|
|
|
app.logger.handlers[:] = old_handlers |
|
|
|
|
|
|
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert len(called) == 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def suite(): |
|
|
|
|
|
|
|
suite = unittest.TestSuite() |
|
|
|
|
|
|
|
suite.addTest(unittest.makeSuite(TestTemplating)) |
|
|
|
|
|
|
|
return suite |
|
|
|
|
|
|
|