Merge pull request #23472 from edx/BOM-1416
Fixed template engine error under Django 2 - BOM-1416
This commit is contained in:
@@ -23,7 +23,7 @@ class MakoLoader(object):
|
||||
"""
|
||||
|
||||
is_usable = False
|
||||
supports_recursion = False
|
||||
supports_recursion = True
|
||||
|
||||
def __init__(self, base_loader):
|
||||
# base_loader is an instance of a BaseLoader subclass
|
||||
@@ -38,18 +38,25 @@ class MakoLoader(object):
|
||||
self.module_directory = module_directory
|
||||
|
||||
def __call__(self, template_name, template_dirs=None):
|
||||
return self.load_template(template_name, template_dirs)
|
||||
return self.load_template(template_name)
|
||||
|
||||
def load_template(self, template_name, template_dirs=None):
|
||||
source, file_path = self.load_template_source(template_name, template_dirs)
|
||||
# pylint: disable=unused-argument
|
||||
def get_template(self, template_name, template_dirs=None, skip=None):
|
||||
return self.load_template(template_name)
|
||||
|
||||
def load_template(self, template_name):
|
||||
"""
|
||||
Method returns loads and returns template if it exists
|
||||
"""
|
||||
source, origin = self.load_template_source(template_name)
|
||||
|
||||
# In order to allow dynamic template overrides, we need to cache templates based on their absolute paths
|
||||
# rather than relative paths, overriding templates would have same relative paths.
|
||||
module_directory = self.module_directory.rstrip("/") + "/{dir_hash}/".format(dir_hash=hash(file_path))
|
||||
module_directory = self.module_directory.rstrip("/") + "/{dir_hash}/".format(dir_hash=hash(origin.name))
|
||||
|
||||
if source.startswith("## mako\n"):
|
||||
# This is a mako template
|
||||
template = Template(filename=file_path,
|
||||
template = Template(filename=origin.name,
|
||||
module_directory=module_directory,
|
||||
input_encoding='utf-8',
|
||||
output_encoding='utf-8',
|
||||
@@ -57,12 +64,12 @@ class MakoLoader(object):
|
||||
encoding_errors='replace',
|
||||
uri=template_name,
|
||||
engine=engines['mako'])
|
||||
return template, None
|
||||
return template
|
||||
else:
|
||||
# This is a regular template
|
||||
try:
|
||||
template = Engine.get_default().from_string(source)
|
||||
return template, None
|
||||
return template
|
||||
except ImproperlyConfigured:
|
||||
# Either no DjangoTemplates engine was configured -or- multiple engines
|
||||
# were configured, making the get_default() call above fail.
|
||||
@@ -72,12 +79,15 @@ class MakoLoader(object):
|
||||
# returning the source and display name for the requested template.
|
||||
# This allows for eventual correct identification of the actual template that does
|
||||
# not exist.
|
||||
return source, file_path
|
||||
return source, origin.name
|
||||
|
||||
def load_template_source(self, template_name, template_dirs=None):
|
||||
for origin in self.base_loader.get_template_sources(template_name, template_dirs):
|
||||
def load_template_source(self, template_name):
|
||||
"""
|
||||
Method returns the contents of the template
|
||||
"""
|
||||
for origin in self.base_loader.get_template_sources(template_name):
|
||||
try:
|
||||
return self.base_loader.get_contents(origin), origin.name
|
||||
return self.base_loader.get_contents(origin), origin
|
||||
except TemplateDoesNotExist:
|
||||
pass
|
||||
raise TemplateDoesNotExist(template_name)
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
|
||||
from django.conf import settings
|
||||
from django.template import Context, engines
|
||||
from django.template import Context, engines, Origin
|
||||
from edx_django_utils.cache import RequestCache
|
||||
from mako.template import Template as MakoTemplate
|
||||
from six import text_type
|
||||
@@ -25,6 +25,8 @@ from .shortcuts import is_any_marketing_link_set, is_marketing_link_set, marketi
|
||||
|
||||
KEY_CSRF_TOKENS = ('csrf_token', 'csrf')
|
||||
|
||||
UNKNOWN_SOURCE = '<unknown source>'
|
||||
|
||||
|
||||
class Template(object):
|
||||
"""
|
||||
@@ -36,6 +38,8 @@ class Template(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Overrides base __init__ to provide django variable overrides"""
|
||||
self.engine = kwargs.pop('engine', engines[Engines.MAKO])
|
||||
if kwargs.get('origin') is None:
|
||||
self.origin = Origin(UNKNOWN_SOURCE)
|
||||
if len(args) and isinstance(args[0], MakoTemplate):
|
||||
self.mako_template = args[0]
|
||||
else:
|
||||
|
||||
@@ -97,7 +97,7 @@ engine = Engine(dirs=dirs)
|
||||
loader = Loader(engine)
|
||||
|
||||
source = None
|
||||
for origin in loader.get_template_sources(path, dirs):
|
||||
for origin in loader.get_template_sources(path):
|
||||
try:
|
||||
source = loader.get_contents(origin)
|
||||
except TemplateDoesNotExist:
|
||||
|
||||
@@ -27,21 +27,13 @@ class ThemeFilesystemLoader(FilesystemLoader):
|
||||
is_usable = True
|
||||
_accepts_engine_in_init = True
|
||||
|
||||
def get_template_sources(self, template_name, template_dirs=None):
|
||||
"""
|
||||
Returns the absolute paths to "template_name", when appended to each
|
||||
directory in "template_dirs". Any paths that don't lie inside one of the
|
||||
template dirs are excluded from the result set, for security reasons.
|
||||
"""
|
||||
if not template_dirs:
|
||||
template_dirs = self.engine.dirs
|
||||
def __init__(self, engine, dirs=None):
|
||||
if not dirs:
|
||||
self.dirs = engine.dirs
|
||||
theme_dirs = self.get_theme_template_sources()
|
||||
|
||||
# append theme dirs to the beginning so templates are looked up inside theme dir first
|
||||
if isinstance(theme_dirs, list):
|
||||
template_dirs = theme_dirs + template_dirs
|
||||
|
||||
return list(super(ThemeFilesystemLoader, self).get_template_sources(template_name, template_dirs))
|
||||
self.dirs = theme_dirs + self.dirs
|
||||
super(ThemeFilesystemLoader, self).__init__(engine, self.dirs)
|
||||
|
||||
@staticmethod
|
||||
def get_theme_template_sources():
|
||||
|
||||
Reference in New Issue
Block a user