Moved make_origin into the Engine class.

This commit is contained in:
Aymeric Augustin 2014-11-22 21:30:05 +01:00
parent 240ea67ce0
commit 246cfdeae3
4 changed files with 15 additions and 15 deletions

View File

@ -105,12 +105,11 @@ class Engine(object):
"Invalid value in template loaders configuration: %r" % loader) "Invalid value in template loaders configuration: %r" % loader)
def find_template(self, name, dirs=None): def find_template(self, name, dirs=None):
# Inner import to avoid circular dependency
from .loader import make_origin
for loader in self.template_loaders: for loader in self.template_loaders:
try: try:
source, display_name = loader(name, dirs) source, display_name = loader(name, dirs)
return (source, make_origin(display_name, loader, name, dirs)) origin = self.make_origin(display_name, loader, name, dirs)
return source, origin
except TemplateDoesNotExist: except TemplateDoesNotExist:
pass pass
raise TemplateDoesNotExist(name) raise TemplateDoesNotExist(name)
@ -214,3 +213,11 @@ class Engine(object):
tokens = lexer.tokenize() tokens = lexer.tokenize()
parser = parser_class(tokens) parser = parser_class(tokens)
return parser.parse() return parser.parse()
def make_origin(self, display_name, loader, name, dirs):
if settings.TEMPLATE_DEBUG and display_name:
# Inner import to avoid circular dependency
from .loader import LoaderOrigin
return LoaderOrigin(display_name, loader, name, dirs)
else:
return None

View File

@ -1,6 +1,5 @@
import warnings import warnings
from django.conf import settings
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
from .base import Origin from .base import Origin
@ -16,13 +15,6 @@ class LoaderOrigin(Origin):
return self.loader(self.loadname, self.dirs)[0] return self.loader(self.loadname, self.dirs)[0]
def make_origin(display_name, loader, name, dirs):
if settings.TEMPLATE_DEBUG and display_name:
return LoaderOrigin(display_name, loader, name, dirs)
else:
return None
def find_template(*args, **kwargs): def find_template(*args, **kwargs):
return Engine.get_default().find_template(*args, **kwargs) return Engine.get_default().find_template(*args, **kwargs)

View File

@ -1,5 +1,5 @@
from django.template.base import TemplateDoesNotExist from django.template.base import TemplateDoesNotExist
from django.template.loader import get_template_from_string, make_origin from django.template.loader import get_template_from_string
class Loader(object): class Loader(object):
@ -16,7 +16,7 @@ class Loader(object):
def load_template(self, template_name, template_dirs=None): def load_template(self, template_name, template_dirs=None):
source, display_name = self.load_template_source( source, display_name = self.load_template_source(
template_name, template_dirs) template_name, template_dirs)
origin = make_origin( origin = self.engine.make_origin(
display_name, self.load_template_source, display_name, self.load_template_source,
template_name, template_dirs) template_name, template_dirs)

View File

@ -5,7 +5,7 @@ to load templates from them in order, caching the result.
import hashlib import hashlib
from django.template.base import TemplateDoesNotExist from django.template.base import TemplateDoesNotExist
from django.template.loader import get_template_from_string, make_origin from django.template.loader import get_template_from_string
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from .base import Loader as BaseLoader from .base import Loader as BaseLoader
@ -42,7 +42,8 @@ class Loader(BaseLoader):
except TemplateDoesNotExist: except TemplateDoesNotExist:
pass pass
else: else:
result = (template, make_origin(display_name, loader, name, dirs)) origin = self.engine.make_origin(display_name, loader, name, dirs)
result = template, origin
break break
self.find_template_cache[key] = result self.find_template_cache[key] = result
if result: if result: