Removed dependency of template loaders on Django settings.

This commit is contained in:
Aymeric Augustin 2014-11-20 21:27:56 +01:00
parent 29a977ab14
commit a2dd08666c
5 changed files with 13 additions and 9 deletions

View File

@ -18,7 +18,8 @@ class Engine(object):
def __init__(self, dirs=None, app_dirs=False,
allowed_include_roots=None, context_processors=None,
loaders=None, string_if_invalid=''):
loaders=None, string_if_invalid='',
file_charset=None):
if dirs is None:
dirs = []
if allowed_include_roots is None:
@ -33,6 +34,8 @@ class Engine(object):
if app_dirs:
raise ImproperlyConfigured(
"APP_DIRS must not be set when LOADERS is defined.")
if file_charset is None:
file_charset = 'utf-8'
self.dirs = dirs
self.app_dirs = app_dirs
@ -40,6 +43,7 @@ class Engine(object):
self.context_processors = context_processors
self.loaders = loaders
self.string_if_invalid = string_if_invalid
self.file_charset = file_charset
@classmethod
@lru_cache.lru_cache()
@ -51,6 +55,7 @@ class Engine(object):
context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS,
loaders=settings.TEMPLATE_LOADERS,
string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID,
file_charset=settings.FILE_CHARSET,
)
@cached_property

View File

@ -5,7 +5,6 @@ packages.
import io
from django.conf import settings
from django.core.exceptions import SuspiciousFileOperation
from django.template.base import TemplateDoesNotExist
from django.template.utils import get_app_template_dirs
@ -36,7 +35,7 @@ class Loader(BaseLoader):
def load_template_source(self, template_name, template_dirs=None):
for filepath in self.get_template_sources(template_name, template_dirs):
try:
with io.open(filepath, encoding=settings.FILE_CHARSET) as fp:
with io.open(filepath, encoding=self.engine.file_charset) as fp:
return fp.read(), filepath
except IOError:
pass

View File

@ -7,7 +7,6 @@ except ImportError:
resource_string = None
from django.apps import apps
from django.conf import settings
from django.template.base import TemplateDoesNotExist
from django.utils import six
@ -31,6 +30,6 @@ class Loader(BaseLoader):
except Exception:
continue
if six.PY2:
resource = resource.decode(settings.FILE_CHARSET)
resource = resource.decode(self.engine.file_charset)
return (resource, 'egg:%s:%s' % (app_config.name, pkg_name))
raise TemplateDoesNotExist(template_name)

View File

@ -4,7 +4,6 @@ Wrapper for loading templates from the filesystem.
import io
from django.conf import settings
from django.core.exceptions import SuspiciousFileOperation
from django.template.base import TemplateDoesNotExist
from django.utils._os import safe_join
@ -22,7 +21,7 @@ class Loader(BaseLoader):
template dirs are excluded from the result set, for security reasons.
"""
if not template_dirs:
template_dirs = settings.TEMPLATE_DIRS
template_dirs = self.engine.dirs
for template_dir in template_dirs:
try:
yield safe_join(template_dir, template_name)
@ -35,13 +34,14 @@ class Loader(BaseLoader):
tried = []
for filepath in self.get_template_sources(template_name, template_dirs):
try:
with io.open(filepath, encoding=settings.FILE_CHARSET) as fp:
with io.open(filepath, encoding=self.engine.file_charset) as fp:
return fp.read(), filepath
except IOError:
tried.append(filepath)
if tried:
error_msg = "Tried %s" % tried
else:
error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
error_msg = ("Your template directories configuration is empty. "
"Change it to point to at least one template directory.")
raise TemplateDoesNotExist(error_msg)
load_template_source.is_usable = True

View File

@ -85,6 +85,7 @@ def reset_default_template_engine(**kwargs):
'TEMPLATE_CONTEXT_PROCESSORS',
'TEMPLATE_LOADERS',
'TEMPLATE_STRING_IF_INVALID',
'FILE_CHARSET',
}:
from django.template.engine import Engine
Engine.get_default.cache_clear()