2017-01-19 04:30:21 +08:00
|
|
|
import functools
|
2019-02-05 19:22:08 +08:00
|
|
|
from collections import Counter
|
2017-07-22 04:33:26 +08:00
|
|
|
from pathlib import Path
|
2014-11-11 04:04:04 +08:00
|
|
|
|
|
|
|
from django.apps import apps
|
2014-11-11 23:32:19 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
from django.utils.module_loading import import_string
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidTemplateEngineError(ImproperlyConfigured):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class EngineHandler:
|
2014-11-11 23:32:19 +08:00
|
|
|
def __init__(self, templates=None):
|
|
|
|
"""
|
|
|
|
templates is an optional list of template engine definitions
|
|
|
|
(structured like settings.TEMPLATES).
|
|
|
|
"""
|
|
|
|
self._templates = templates
|
|
|
|
self._engines = {}
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def templates(self):
|
|
|
|
if self._templates is None:
|
|
|
|
self._templates = settings.TEMPLATES
|
|
|
|
|
2019-02-05 19:22:08 +08:00
|
|
|
templates = {}
|
2015-05-04 03:42:56 +08:00
|
|
|
backend_names = []
|
2014-11-11 23:32:19 +08:00
|
|
|
for tpl in self._templates:
|
|
|
|
try:
|
|
|
|
# This will raise an exception if 'BACKEND' doesn't exist or
|
|
|
|
# isn't a string containing at least one dot.
|
|
|
|
default_name = tpl['BACKEND'].rsplit('.', 2)[-2]
|
|
|
|
except Exception:
|
|
|
|
invalid_backend = tpl.get('BACKEND', '<not defined>')
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Invalid BACKEND for a template engine: {}. Check "
|
|
|
|
"your TEMPLATES setting.".format(invalid_backend))
|
|
|
|
|
2018-02-27 01:23:31 +08:00
|
|
|
tpl = {
|
|
|
|
'NAME': default_name,
|
|
|
|
'DIRS': [],
|
|
|
|
'APP_DIRS': False,
|
|
|
|
'OPTIONS': {},
|
|
|
|
**tpl,
|
|
|
|
}
|
2014-11-11 23:32:19 +08:00
|
|
|
|
|
|
|
templates[tpl['NAME']] = tpl
|
2015-05-04 03:42:56 +08:00
|
|
|
backend_names.append(tpl['NAME'])
|
2014-11-11 23:32:19 +08:00
|
|
|
|
2015-05-04 03:42:56 +08:00
|
|
|
counts = Counter(backend_names)
|
2014-11-11 23:32:19 +08:00
|
|
|
duplicates = [alias for alias, count in counts.most_common() if count > 1]
|
|
|
|
if duplicates:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Template engine aliases aren't unique, duplicates: {}. "
|
|
|
|
"Set a unique NAME for each engine in settings.TEMPLATES."
|
|
|
|
.format(", ".join(duplicates)))
|
|
|
|
|
|
|
|
return templates
|
|
|
|
|
|
|
|
def __getitem__(self, alias):
|
|
|
|
try:
|
|
|
|
return self._engines[alias]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
params = self.templates[alias]
|
|
|
|
except KeyError:
|
|
|
|
raise InvalidTemplateEngineError(
|
|
|
|
"Could not find config for '{}' "
|
|
|
|
"in settings.TEMPLATES".format(alias))
|
|
|
|
|
2015-02-05 18:53:04 +08:00
|
|
|
# If importing or initializing the backend raises an exception,
|
|
|
|
# self._engines[alias] isn't set and this code may get executed
|
|
|
|
# again, so we must preserve the original params. See #24265.
|
|
|
|
params = params.copy()
|
2014-11-11 23:32:19 +08:00
|
|
|
backend = params.pop('BACKEND')
|
|
|
|
engine_cls = import_string(backend)
|
|
|
|
engine = engine_cls(params)
|
|
|
|
|
|
|
|
self._engines[alias] = engine
|
|
|
|
return engine
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.templates)
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return [self[alias] for alias in self]
|
2014-11-11 04:04:04 +08:00
|
|
|
|
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache()
|
2014-11-11 04:04:04 +08:00
|
|
|
def get_app_template_dirs(dirname):
|
|
|
|
"""
|
|
|
|
Return an iterable of paths of directories to load app templates from.
|
|
|
|
|
|
|
|
dirname is the name of the subdirectory containing templates inside
|
|
|
|
installed applications.
|
|
|
|
"""
|
2017-07-22 04:33:26 +08:00
|
|
|
template_dirs = [
|
|
|
|
str(Path(app_config.path) / dirname)
|
|
|
|
for app_config in apps.get_app_configs()
|
|
|
|
if app_config.path and (Path(app_config.path) / dirname).is_dir()
|
|
|
|
]
|
2014-11-11 04:04:04 +08:00
|
|
|
# Immutable return value because it will be cached and shared by callers.
|
|
|
|
return tuple(template_dirs)
|