2009-12-14 20:08:23 +08:00
|
|
|
"""
|
|
|
|
Wrapper class that takes a list of template loaders as an argument and attempts
|
|
|
|
to load templates from them in order, caching the result.
|
|
|
|
"""
|
|
|
|
|
2011-03-28 10:11:19 +08:00
|
|
|
import hashlib
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.template.base import TemplateDoesNotExist
|
2014-11-16 01:35:02 +08:00
|
|
|
from django.template.loader import get_template_from_string, find_template_loader, make_origin
|
2013-02-27 11:26:15 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
2009-12-14 20:08:23 +08:00
|
|
|
|
2014-11-16 01:35:02 +08:00
|
|
|
from .base import Loader as BaseLoader
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
class Loader(BaseLoader):
|
|
|
|
is_usable = True
|
|
|
|
|
|
|
|
def __init__(self, loaders):
|
|
|
|
self.template_cache = {}
|
2013-07-26 03:27:23 +08:00
|
|
|
self.find_template_cache = {}
|
2009-12-14 20:08:23 +08:00
|
|
|
self._loaders = loaders
|
|
|
|
self._cached_loaders = []
|
|
|
|
|
|
|
|
@property
|
|
|
|
def loaders(self):
|
|
|
|
# Resolve loaders on demand to avoid circular imports
|
|
|
|
if not self._cached_loaders:
|
2011-12-30 20:55:08 +08:00
|
|
|
# Set self._cached_loaders atomically. Otherwise, another thread
|
|
|
|
# could see an incomplete list. See #17303.
|
|
|
|
cached_loaders = []
|
2009-12-14 20:08:23 +08:00
|
|
|
for loader in self._loaders:
|
2011-12-30 20:55:08 +08:00
|
|
|
cached_loaders.append(find_template_loader(loader))
|
|
|
|
self._cached_loaders = cached_loaders
|
2009-12-14 20:08:23 +08:00
|
|
|
return self._cached_loaders
|
|
|
|
|
2013-07-26 03:27:23 +08:00
|
|
|
def cache_key(self, template_name, template_dirs):
|
2010-05-21 22:25:26 +08:00
|
|
|
if template_dirs:
|
|
|
|
# If template directories were specified, use a hash to differentiate
|
2013-07-26 03:27:23 +08:00
|
|
|
return '-'.join([template_name, hashlib.sha1(force_bytes('|'.join(template_dirs))).hexdigest()])
|
|
|
|
else:
|
|
|
|
return template_name
|
|
|
|
|
|
|
|
def find_template(self, name, dirs=None):
|
2013-08-25 08:26:34 +08:00
|
|
|
"""
|
|
|
|
Helper method. Lookup the template :param name: in all the configured loaders
|
|
|
|
"""
|
2013-07-26 03:27:23 +08:00
|
|
|
key = self.cache_key(name, dirs)
|
|
|
|
try:
|
|
|
|
result = self.find_template_cache[key]
|
|
|
|
except KeyError:
|
|
|
|
result = None
|
|
|
|
for loader in self.loaders:
|
|
|
|
try:
|
|
|
|
template, display_name = loader(name, dirs)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
result = (template, make_origin(display_name, loader, name, dirs))
|
2013-11-20 00:49:16 +08:00
|
|
|
break
|
2013-07-26 03:27:23 +08:00
|
|
|
self.find_template_cache[key] = result
|
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
else:
|
2013-08-25 08:26:34 +08:00
|
|
|
self.template_cache[key] = TemplateDoesNotExist
|
2013-07-26 03:27:23 +08:00
|
|
|
raise TemplateDoesNotExist(name)
|
2010-05-21 16:54:15 +08:00
|
|
|
|
2013-07-26 03:27:23 +08:00
|
|
|
def load_template(self, template_name, template_dirs=None):
|
|
|
|
key = self.cache_key(template_name, template_dirs)
|
2013-08-22 02:23:53 +08:00
|
|
|
template_tuple = self.template_cache.get(key)
|
2013-08-25 08:26:34 +08:00
|
|
|
# A cached previous failure:
|
2013-08-22 02:23:53 +08:00
|
|
|
if template_tuple is TemplateDoesNotExist:
|
|
|
|
raise TemplateDoesNotExist
|
|
|
|
elif template_tuple is None:
|
2009-12-14 20:08:23 +08:00
|
|
|
template, origin = self.find_template(template_name, template_dirs)
|
|
|
|
if not hasattr(template, 'render'):
|
2010-03-16 22:34:57 +08:00
|
|
|
try:
|
|
|
|
template = get_template_from_string(template, origin, template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2010-05-21 16:54:15 +08:00
|
|
|
# If compiling the template we found raises TemplateDoesNotExist,
|
|
|
|
# back off to returning the source and display name for the template
|
|
|
|
# we were asked to load. This allows for correct identification (later)
|
2010-03-16 22:34:57 +08:00
|
|
|
# of the actual template that does not exist.
|
2013-08-22 02:23:53 +08:00
|
|
|
self.template_cache[key] = (template, origin)
|
|
|
|
self.template_cache[key] = (template, None)
|
|
|
|
return self.template_cache[key]
|
2009-12-14 20:08:23 +08:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"Empty the template cache."
|
|
|
|
self.template_cache.clear()
|
2013-07-26 03:27:23 +08:00
|
|
|
self.find_template_cache.clear()
|