Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes issue #376.

  • Loading branch information...
commit 2f310c5b865e1509c392b50b686d0141135ca65b 1 parent e78e2a1
@tomekwojcik authored
Showing with 80 additions and 0 deletions.
  1. +33 −0 flask/templating.py
  2. +47 −0 flask/testsuite/blueprints.py
View
33 flask/templating.py
@@ -41,6 +41,39 @@ def __init__(self, app, **options):
options['loader'] = app.create_global_jinja_loader()
BaseEnvironment.__init__(self, **options)
self.app = app
+
+ def _load_template(self, name, globals):
+ loader = None
+ reqctx = _request_ctx_stack.top
+ if reqctx.request.blueprint == None:
+ cache_name = 'app::' + name
+ loader = reqctx.app.jinja_loader
+ else:
+ cache_name = reqctx.request.blueprint + '::' + name
+ loader = reqctx.app.blueprints[reqctx.request.blueprint].jinja_loader
+
+ if self.loader is None and self.loader is None:
+ raise TypeError('no loader for this environment specified')
+ if self.cache is not None:
+ template = self.cache.get(cache_name)
+ if template is not None and (not self.auto_reload or \
+ template.is_up_to_date):
+ return template
+
+ template = None
+ if loader is not None:
+ try:
+ template = loader.load(self, name, globals)
+ except TemplateNotFound:
+ pass
+
+ if template is None:
+ template = self.loader.load(self, name, globals)
+
+ if self.cache is not None:
+ self.cache[cache_name] = template
+
+ return template
class DispatchingJinjaLoader(BaseLoader):
View
47 flask/testsuite/blueprints.py
@@ -650,9 +650,56 @@ def index():
rv = app.test_client().get('/')
self.assert_equal(rv.data, 'dcba')
+class Blueprint2TestCase(FlaskTestCase):
+
+ def test_templates_and_static(self):
+ from blueprintapp2 import app
+ c = app.test_client()
+
+ rv = c.get('/app_level')
+ self.assert_equal(rv.data, 'Hello from app level view!')
+ rv = c.get('/')
+ self.assert_equal(rv.data, 'Hello from the Frontend')
+ rv = c.get('/admin/')
+ self.assert_equal(rv.data, 'Hello from the Admin')
+ rv = c.get('/admin/index2')
+ self.assert_equal(rv.data, 'Hello from the Admin')
+ rv = c.get('/admin/static/test.txt')
+ self.assert_equal(rv.data.strip(), 'Admin File')
+ rv = c.get('/admin/static/css/test.css')
+ self.assert_equal(rv.data.strip(), '/* nested file */')
+
+ # try/finally, in case other tests use this app for Blueprint tests.
+ max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT']
+ try:
+ expected_max_age = 3600
+ if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == expected_max_age:
+ expected_max_age = 7200
+ app.config['SEND_FILE_MAX_AGE_DEFAULT'] = expected_max_age
+ rv = c.get('/admin/static/css/test.css')
+ cc = parse_cache_control_header(rv.headers['Cache-Control'])
+ self.assert_equal(cc.max_age, expected_max_age)
+ finally:
+ app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default
+
+ with app.test_request_context():
+ self.assert_equal(flask.url_for('admin.static', filename='test.txt'),
+ '/admin/static/test.txt')
+
+ with app.test_request_context():
+ try:
+ flask.render_template('missing.html')
+ except TemplateNotFound, e:
+ self.assert_equal(e.name, 'missing.html')
+ else:
+ self.assert_(0, 'expected exception')
+
+ with flask.Flask(__name__).test_request_context():
+ self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested')
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(BlueprintTestCase))
+ suite.addTest(unittest.makeSuite(Blueprint2TestCase))
suite.addTest(unittest.makeSuite(ModuleTestCase))
return suite

0 comments on commit 2f310c5

Please sign in to comment.
Something went wrong with that request. Please try again.