Chnaged a bunch of behavior in blueprints for it to be more flexible. Improved backwards compat.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 59a92eb · Browse files · 6 files·+50 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+50 added−54 removed
- tests:+1 added−1 removed
- other:+49 added−53 removed
Provenance
- Commit
abe1378- Tree
9e60804- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
59a92eb
6 files changed+50 added−54 removed
modified:flask/app.py+4 added-3 removed
204 unmodified lines205 #: .. versionadded:: 0.7206 test_client_class = None207 208 def __init__(self, import_name, static_path=None, static_url_path=None,209- static_folder='static'):210- _PackageBoundObject.__init__(self, import_name)209+ static_folder='static', template_folder='templates'):210+ _PackageBoundObject.__init__(self, import_name,211+ template_folder=template_folder)212 if static_path is not None:213 from warnings import warn214 warn(DeprecationWarning('static_path is now called '215 'static_url_path'), stacklevel=2)240 unmodified lines456 )457 rv.filters['tojson'] = _tojson_filter458 return rv459 459- def create_jinja_loader(self):460+ def create_global_jinja_loader(self):461 """Creates the loader for the Jinja2 environment. Can be used to462 override just the loader and keeping the rest unchanged.463 464 .. versionadded:: 0.7819 unmodified linesmodified:flask/blueprints.py+3 added-3 removed
54 unmodified lines55 warn_on_modifications = False56 _got_registered_once = False57 58 def __init__(self, name, import_name, static_folder=None,59- static_url_path=None, url_prefix=None,60- subdomain=None):61- _PackageBoundObject.__init__(self, import_name)59+ static_url_path=None, template_folder=None,60+ url_prefix=None, subdomain=None):61+ _PackageBoundObject.__init__(self, import_name, template_folder)62 self.name = name63 self.url_prefix = url_prefix64 self.subdomain = subdomain65 self.static_folder = static_folder187 unmodified linesmodified:flask/helpers.py+6 added-5 removed
483 unmodified lines484 485 486 class _PackageBoundObject(object):487 488- template_folder = 'templates'489-490- def __init__(self, import_name):488+ def __init__(self, import_name, template_folder=None):489 #: The name of the package or module. Do not change this once490 #: it was set by the constructor.491 self.import_name = import_name492 493+ #: location of the templates. `None` if templates should not be494+ #: exposed.495+ self.template_folder = template_folder496+497 #: Where is the app root located?498 self.root_path = _get_package_path(self.import_name)499 500 self._static_folder = None44 unmodified lines545 .. versionadded:: 0.5546 """547 if not self.has_static_folder:548 raise RuntimeError('No static folder for this object')547- return send_from_directory(os.path.join(self.root_path, 'static'),548- filename)549+ return send_from_directory(self.static_folder, filename)550 551 def open_resource(self, resource):552 """Opens a resource from the application's resource folder. To see553 how this works, consider the following folder structure::20 unmodified linesmodified:flask/module.py+1 added-2 removed
9 unmodified lines10 """11 12 import os13 14-from .helpers import _PackageBoundObject, _endpoint_from_view_func14 from .blueprints import Blueprint15 16 17 def blueprint_is_module(bp):17 unmodified lines35 assert '.' in import_name, 'name required if package name ' \36 'does not point to a submodule'37 name = import_name.rsplit('.', 1)[1]38 Blueprint.__init__(self, name, import_name, url_prefix=url_prefix,40- subdomain=subdomain)39+ subdomain=subdomain, template_folder='templates')40 41 if os.path.isdir(os.path.join(self.root_path, 'static')):42 self._static_folder = 'static'modified:flask/templating.py+35 added-40 removed
36 unmodified lines37 """38 39 def __init__(self, app, **options):40 if 'loader' not in options:41- options['loader'] = app.create_jinja_loader()41+ options['loader'] = app.create_global_jinja_loader()42 BaseEnvironment.__init__(self, **options)43 self.app = app44 45- def join_path(self, template, parent):46- if template and template[0] == ':':47- template = parent.split(':', 1)[0] + template48- return template45 50-46 class DispatchingJinjaLoader(BaseLoader):47 """A loader that looks for templates in the application and all53- the module folders.48+ the blueprint folders.49 """50 51 def __init__(self, app):52 self.app = app53 54 def get_source(self, environment, template):60- # newstyle template support. blueprints are explicit and no further61- # magic is involved. If the template cannot be loaded by the62- # blueprint loader it just gives up, no further steps involved.63- if ':' in template:64- blueprint_name, local_template = template.split(':', 1)65- local_template = posixpath.normpath(local_template)66- blueprint = self.app.blueprints.get(blueprint_name)67- if blueprint is None:68- raise TemplateNotFound(template)69- loader = blueprint.jinja_loader70- if loader is not None:71- return loader.get_source(environment, local_template)55+ for loader, local_name in self._iter_loaders(template):56+ try:57+ return loader.get_source(environment, local_name)58+ except TemplateNotFound:59+ pass60 73- # if modules are enabled we call into the old style template lookup74- # and try that before we go with the real deal.75- loader = None61+ raise TemplateNotFound(template)62+63+ def _iter_loaders(self, template):64+ loader = self.app.jinja_loader65+ if loader is not None:66+ yield loader, template67+68+ # old style module based loaders in case we are dealing with a69+ # blueprint that is an old style module70 try:77- module, name = posixpath.normpath(template).split('/', 1)71+ module, local_name = posixpath.normpath(template).split('/', 1)72 blueprint = self.app.blueprints[module]73 if blueprint_is_module(blueprint):74 loader = blueprint.jinja_loader75+ if loader is not None:76+ yield loader, local_name81- except (ValueError, KeyError, TemplateNotFound):77+ except (ValueError, KeyError):78 pass83- try:79+80+ for blueprint in self.app.blueprints.itervalues():81+ loader = blueprint.jinja_loader82 if loader is not None:85- return loader.get_source(environment, name)86- except TemplateNotFound:87- pass83+ yield loader, template84 89- # at the very last, load templates from the environment90- return self.app.jinja_loader.get_source(environment, template)91-85 def list_templates(self):93- result = set(self.app.jinja_loader.list_templates())86+ result = set()87+ loader = self.app.jinja_loader88+ if loader is not None:89+ result.update(loader.list_templates())90 95- for name, module in self.app.modules.iteritems():96- if module.jinja_loader is not None:97- for template in module.jinja_loader.list_templates():98- result.add('%s/%s' % (name, template))99-91 for name, blueprint in self.app.blueprints.iteritems():101- if blueprint.jinja_loader is not None:102- for template in blueprint.jinja_loader.list_templates():103- result.add('%s:%s' % (name, template))92+ loader = blueprint.jinja_loader93+ if loader is not None:94+ for template in loader.list_templates():95+ prefix = ''96+ if not blueprint_is_module(blueprint):97+ prefix = name + '/'98+ result.add(prefix + template)99 100 return list(result)101 102 36 unmodified linesmodified:tests/flask_tests.py+1 added-1 removed
990 unmodified lines991 assert rv.data == 'dcba'992 993 def test_custom_template_loader(self):994 class MyFlask(flask.Flask):995- def create_jinja_loader(self):995+ def create_global_jinja_loader(self):996 from jinja2 import DictLoader997 return DictLoader({'index.html': 'Hello Custom World!'})998 app = MyFlask(__name__)999 @app.route('/')710 unmodified lines