mirror of
https://github.com/django/django.git
synced 2025-10-29 00:26:07 +00:00
Moved template loaders management in Engine.
Passed the engine instance to loaders. This is a prerequisite for looking up configuration on the engine instance instead of global settings. This is backwards incompatible for custom template loaders that override __init__. However the documentation doesn't talk about __init__ and the way to pass arguments to custom template loaders isn't specified. I'm considering it a private API.
This commit is contained in:
@@ -2,12 +2,13 @@ import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils import lru_cache
|
||||
from django.utils import six
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils import lru_cache
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
from .base import Context, Template, TemplateDoesNotExist
|
||||
from .loaders.utils import get_template_loaders
|
||||
|
||||
|
||||
_dirs_undefined = object()
|
||||
@@ -54,7 +55,49 @@ class Engine(object):
|
||||
|
||||
@cached_property
|
||||
def template_loaders(self):
|
||||
return get_template_loaders(self.loaders)
|
||||
return self.get_template_loaders(self.loaders)
|
||||
|
||||
def get_template_loaders(self, template_loaders):
|
||||
loaders = []
|
||||
for template_loader in template_loaders:
|
||||
loader = self.find_template_loader(template_loader)
|
||||
if loader is not None:
|
||||
loaders.append(loader)
|
||||
return loaders
|
||||
|
||||
def find_template_loader(self, loader):
|
||||
if isinstance(loader, (tuple, list)):
|
||||
args = list(loader[1:])
|
||||
loader = loader[0]
|
||||
else:
|
||||
args = []
|
||||
|
||||
if isinstance(loader, six.string_types):
|
||||
loader_class = import_string(loader)
|
||||
|
||||
if getattr(loader_class, '_accepts_engine_in_init', False):
|
||||
args.insert(0, self)
|
||||
else:
|
||||
warnings.warn(
|
||||
"%s inherits from django.template.loader.BaseLoader "
|
||||
"instead of django.template.loaders.base.Loader. " %
|
||||
loader, RemovedInDjango20Warning, stacklevel=2)
|
||||
|
||||
loader_instance = loader_class(*args)
|
||||
|
||||
if not loader_instance.is_usable:
|
||||
warnings.warn(
|
||||
"Your template loaders configuration includes %r, but "
|
||||
"your Python installation doesn't support that type of "
|
||||
"template loading. Consider removing that line from "
|
||||
"your settings." % loader)
|
||||
return None
|
||||
else:
|
||||
return loader_instance
|
||||
|
||||
else:
|
||||
raise ImproperlyConfigured(
|
||||
"Invalid value in template loaders configuration: %r" % loader)
|
||||
|
||||
def find_template(self, name, dirs=None):
|
||||
# Inner import to avoid circular dependency
|
||||
|
||||
@@ -48,10 +48,11 @@ from .loaders import base
|
||||
|
||||
|
||||
class BaseLoader(base.Loader):
|
||||
_accepts_engine_in_init = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
warnings.warn(
|
||||
"django.template.loader.BaseLoader was renamed to "
|
||||
"django.template.loader.BaseLoader was superseded by "
|
||||
"django.template.loaders.base.Loader.",
|
||||
RemovedInDjango20Warning, stacklevel=2)
|
||||
super(BaseLoader, self).__init__(*args, **kwargs)
|
||||
|
||||
@@ -4,10 +4,11 @@ from django.template.loader import get_template_from_string, make_origin
|
||||
|
||||
class Loader(object):
|
||||
is_usable = False
|
||||
# Only used to raise a deprecation warning. Remove in Django 2.0.
|
||||
_accepts_engine_in_init = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# XXX dropping arguments silently may not be the best idea.
|
||||
pass
|
||||
def __init__(self, engine):
|
||||
self.engine = engine
|
||||
|
||||
def __call__(self, template_name, template_dirs=None):
|
||||
return self.load_template(template_name, template_dirs)
|
||||
|
||||
@@ -9,18 +9,16 @@ from django.template.loader import get_template_from_string, make_origin
|
||||
from django.utils.encoding import force_bytes
|
||||
|
||||
from .base import Loader as BaseLoader
|
||||
from .utils import get_template_loaders
|
||||
|
||||
|
||||
class Loader(BaseLoader):
|
||||
is_usable = True
|
||||
|
||||
def __init__(self, loaders):
|
||||
def __init__(self, engine, loaders):
|
||||
self.template_cache = {}
|
||||
self.find_template_cache = {}
|
||||
# Use the private, non-caching version of get_template_loaders
|
||||
# in case loaders isn't hashable.
|
||||
self.loaders = get_template_loaders(loaders)
|
||||
self.loaders = engine.get_template_loaders(loaders)
|
||||
super(Loader, self).__init__(engine)
|
||||
|
||||
def cache_key(self, template_name, template_dirs):
|
||||
if template_dirs:
|
||||
|
||||
@@ -10,8 +10,9 @@ from .base import Loader as BaseLoader
|
||||
class Loader(BaseLoader):
|
||||
is_usable = True
|
||||
|
||||
def __init__(self, templates_dict):
|
||||
def __init__(self, engine, templates_dict):
|
||||
self.templates_dict = templates_dict
|
||||
super(Loader, self).__init__(engine)
|
||||
|
||||
def load_template_source(self, template_name, template_dirs=None):
|
||||
try:
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
import warnings
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils import six
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
|
||||
def get_template_loaders(template_loaders):
|
||||
loaders = []
|
||||
for template_loader in template_loaders:
|
||||
loader = find_template_loader(template_loader)
|
||||
if loader is not None:
|
||||
loaders.append(loader)
|
||||
return loaders
|
||||
|
||||
|
||||
def find_template_loader(loader):
|
||||
if isinstance(loader, (tuple, list)):
|
||||
loader, args = loader[0], loader[1:]
|
||||
else:
|
||||
args = []
|
||||
|
||||
if isinstance(loader, six.string_types):
|
||||
loader_class = import_string(loader)
|
||||
loader_instance = loader_class(*args)
|
||||
|
||||
if not loader_instance.is_usable:
|
||||
warnings.warn(
|
||||
"Your TEMPLATE_LOADERS setting includes %r, but your Python "
|
||||
"installation doesn't support that type of template loading. "
|
||||
"Consider removing that line from TEMPLATE_LOADERS." % loader)
|
||||
return None
|
||||
else:
|
||||
return loader_instance
|
||||
|
||||
else:
|
||||
raise ImproperlyConfigured(
|
||||
"Invalid value in TEMPLATE_LOADERS: %r" % loader)
|
||||
Reference in New Issue
Block a user