From a2dd08666c5aad377fb8eb4782882caf5565d441 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 20 Nov 2014 21:27:56 +0100 Subject: [PATCH] Removed dependency of template loaders on Django settings. --- django/template/engine.py | 7 ++++++- django/template/loaders/app_directories.py | 3 +-- django/template/loaders/eggs.py | 3 +-- django/template/loaders/filesystem.py | 8 ++++---- django/test/signals.py | 1 + 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/django/template/engine.py b/django/template/engine.py index 257042d497..9a0f4fc2f0 100644 --- a/django/template/engine.py +++ b/django/template/engine.py @@ -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 diff --git a/django/template/loaders/app_directories.py b/django/template/loaders/app_directories.py index 5e263804f6..00ca56c84a 100644 --- a/django/template/loaders/app_directories.py +++ b/django/template/loaders/app_directories.py @@ -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 diff --git a/django/template/loaders/eggs.py b/django/template/loaders/eggs.py index 8b5c92ba62..42b43bab2d 100644 --- a/django/template/loaders/eggs.py +++ b/django/template/loaders/eggs.py @@ -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) diff --git a/django/template/loaders/filesystem.py b/django/template/loaders/filesystem.py index 6646073093..44d814a951 100644 --- a/django/template/loaders/filesystem.py +++ b/django/template/loaders/filesystem.py @@ -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 diff --git a/django/test/signals.py b/django/test/signals.py index 648162b8ae..2da973fcec 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -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()