mirror of
				https://github.com/django/django.git
				synced 2025-10-30 09:06:13 +00:00 
			
		
		
		
	Removed dependency of template loaders on Django settings.
This commit is contained in:
		| @@ -18,7 +18,8 @@ class Engine(object): | |||||||
|  |  | ||||||
|     def __init__(self, dirs=None, app_dirs=False, |     def __init__(self, dirs=None, app_dirs=False, | ||||||
|                  allowed_include_roots=None, context_processors=None, |                  allowed_include_roots=None, context_processors=None, | ||||||
|                  loaders=None, string_if_invalid=''): |                  loaders=None, string_if_invalid='', | ||||||
|  |                  file_charset=None): | ||||||
|         if dirs is None: |         if dirs is None: | ||||||
|             dirs = [] |             dirs = [] | ||||||
|         if allowed_include_roots is None: |         if allowed_include_roots is None: | ||||||
| @@ -33,6 +34,8 @@ class Engine(object): | |||||||
|             if app_dirs: |             if app_dirs: | ||||||
|                 raise ImproperlyConfigured( |                 raise ImproperlyConfigured( | ||||||
|                     "APP_DIRS must not be set when LOADERS is defined.") |                     "APP_DIRS must not be set when LOADERS is defined.") | ||||||
|  |         if file_charset is None: | ||||||
|  |             file_charset = 'utf-8' | ||||||
|  |  | ||||||
|         self.dirs = dirs |         self.dirs = dirs | ||||||
|         self.app_dirs = app_dirs |         self.app_dirs = app_dirs | ||||||
| @@ -40,6 +43,7 @@ class Engine(object): | |||||||
|         self.context_processors = context_processors |         self.context_processors = context_processors | ||||||
|         self.loaders = loaders |         self.loaders = loaders | ||||||
|         self.string_if_invalid = string_if_invalid |         self.string_if_invalid = string_if_invalid | ||||||
|  |         self.file_charset = file_charset | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     @lru_cache.lru_cache() |     @lru_cache.lru_cache() | ||||||
| @@ -51,6 +55,7 @@ class Engine(object): | |||||||
|             context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS, |             context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS, | ||||||
|             loaders=settings.TEMPLATE_LOADERS, |             loaders=settings.TEMPLATE_LOADERS, | ||||||
|             string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID, |             string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID, | ||||||
|  |             file_charset=settings.FILE_CHARSET, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|     @cached_property |     @cached_property | ||||||
|   | |||||||
| @@ -5,7 +5,6 @@ packages. | |||||||
|  |  | ||||||
| import io | import io | ||||||
|  |  | ||||||
| from django.conf import settings |  | ||||||
| from django.core.exceptions import SuspiciousFileOperation | from django.core.exceptions import SuspiciousFileOperation | ||||||
| from django.template.base import TemplateDoesNotExist | from django.template.base import TemplateDoesNotExist | ||||||
| from django.template.utils import get_app_template_dirs | 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): |     def load_template_source(self, template_name, template_dirs=None): | ||||||
|         for filepath in self.get_template_sources(template_name, template_dirs): |         for filepath in self.get_template_sources(template_name, template_dirs): | ||||||
|             try: |             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 |                     return fp.read(), filepath | ||||||
|             except IOError: |             except IOError: | ||||||
|                 pass |                 pass | ||||||
|   | |||||||
| @@ -7,7 +7,6 @@ except ImportError: | |||||||
|     resource_string = None |     resource_string = None | ||||||
|  |  | ||||||
| from django.apps import apps | from django.apps import apps | ||||||
| from django.conf import settings |  | ||||||
| from django.template.base import TemplateDoesNotExist | from django.template.base import TemplateDoesNotExist | ||||||
| from django.utils import six | from django.utils import six | ||||||
|  |  | ||||||
| @@ -31,6 +30,6 @@ class Loader(BaseLoader): | |||||||
|                 except Exception: |                 except Exception: | ||||||
|                     continue |                     continue | ||||||
|                 if six.PY2: |                 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)) |                 return (resource, 'egg:%s:%s' % (app_config.name, pkg_name)) | ||||||
|         raise TemplateDoesNotExist(template_name) |         raise TemplateDoesNotExist(template_name) | ||||||
|   | |||||||
| @@ -4,7 +4,6 @@ Wrapper for loading templates from the filesystem. | |||||||
|  |  | ||||||
| import io | import io | ||||||
|  |  | ||||||
| from django.conf import settings |  | ||||||
| from django.core.exceptions import SuspiciousFileOperation | from django.core.exceptions import SuspiciousFileOperation | ||||||
| from django.template.base import TemplateDoesNotExist | from django.template.base import TemplateDoesNotExist | ||||||
| from django.utils._os import safe_join | 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. |         template dirs are excluded from the result set, for security reasons. | ||||||
|         """ |         """ | ||||||
|         if not template_dirs: |         if not template_dirs: | ||||||
|             template_dirs = settings.TEMPLATE_DIRS |             template_dirs = self.engine.dirs | ||||||
|         for template_dir in template_dirs: |         for template_dir in template_dirs: | ||||||
|             try: |             try: | ||||||
|                 yield safe_join(template_dir, template_name) |                 yield safe_join(template_dir, template_name) | ||||||
| @@ -35,13 +34,14 @@ class Loader(BaseLoader): | |||||||
|         tried = [] |         tried = [] | ||||||
|         for filepath in self.get_template_sources(template_name, template_dirs): |         for filepath in self.get_template_sources(template_name, template_dirs): | ||||||
|             try: |             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 |                     return fp.read(), filepath | ||||||
|             except IOError: |             except IOError: | ||||||
|                 tried.append(filepath) |                 tried.append(filepath) | ||||||
|         if tried: |         if tried: | ||||||
|             error_msg = "Tried %s" % tried |             error_msg = "Tried %s" % tried | ||||||
|         else: |         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) |         raise TemplateDoesNotExist(error_msg) | ||||||
|     load_template_source.is_usable = True |     load_template_source.is_usable = True | ||||||
|   | |||||||
| @@ -85,6 +85,7 @@ def reset_default_template_engine(**kwargs): | |||||||
|         'TEMPLATE_CONTEXT_PROCESSORS', |         'TEMPLATE_CONTEXT_PROCESSORS', | ||||||
|         'TEMPLATE_LOADERS', |         'TEMPLATE_LOADERS', | ||||||
|         'TEMPLATE_STRING_IF_INVALID', |         'TEMPLATE_STRING_IF_INVALID', | ||||||
|  |         'FILE_CHARSET', | ||||||
|     }: |     }: | ||||||
|         from django.template.engine import Engine |         from django.template.engine import Engine | ||||||
|         Engine.get_default.cache_clear() |         Engine.get_default.cache_clear() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user