Django2: Use new custom template loader interface

The new template loader interface is described here:
https://docs.djangoproject.com/en/2.0/ref/templates/api/#loader-methods

The old interface was deprecated in Django 1.9, and the old interface
load_template_sources() was removed in Django 2.0.
https://docs.djangoproject.com/en/2.0/releases/1.9/#template-loader-apis-have-changed

According to the UT result, force_escape in _detail_overview.html
looks unnecessary. If force_escape is used, the returned string will
be double-escaped and it is not what we expect.

blueprint django2-support
Change-Id: I8a5a79cb61a6ebd25453bde3e0ec64f5b5f50e1b
This commit is contained in:
Akihiro Motoki 2017-12-16 21:55:58 +09:00
parent 3ecda5ac67
commit b017879500
3 changed files with 25 additions and 58 deletions

View File

@ -14,26 +14,19 @@
Wrapper for loading templates from "templates" directories in panel modules.
"""
import io
import os
import django
from django.conf import settings
from django.template.engine import Engine
from django.template.loaders.base import Loader as tLoaderCls
from django.core.exceptions import SuspiciousFileOperation
from django.template.loaders import filesystem as filesystem_loader
from django.template import Origin
from django.utils._os import safe_join
if django.VERSION >= (1, 9):
from django.template.exceptions import TemplateDoesNotExist
else:
from django.template.base import TemplateDoesNotExist
# Set up a cache of the panel directories to search.
panel_template_dirs = {}
class TemplateLoader(tLoaderCls):
is_usable = True
class TemplateLoader(filesystem_loader.Loader):
def get_template_sources(self, template_name):
bits = template_name.split('/', 2)
@ -43,23 +36,14 @@ class TemplateLoader(tLoaderCls):
if key in panel_template_dirs:
template_dir = panel_template_dirs[key]
try:
yield safe_join(template_dir, panel_name, remainder)
name = safe_join(template_dir, panel_name, remainder)
yield Origin(name=name,
template_name=template_name,
loader=self)
except UnicodeDecodeError:
# The template dir name wasn't valid UTF-8.
raise
except ValueError:
# The joined path was located outside of template_dir.
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
pass
def load_template_source(self, template_name, template_dirs=None):
for path in self.get_template_sources(template_name):
try:
with io.open(path, encoding=settings.FILE_CHARSET) as file:
return (file.read(), path)
except IOError:
pass
raise TemplateDoesNotExist(template_name)
e = Engine()
_loader = TemplateLoader(e)

View File

@ -17,22 +17,15 @@
Allows Dynamic Theme Loading.
"""
import io
import os
import threading
import django
from django.conf import settings
from django.core.exceptions import SuspiciousFileOperation
from django.template.engine import Engine
from django.template.loaders.base import Loader as tLoaderCls
from django.template.loaders import filesystem as filesystem_loader
from django.template import Origin
from django.utils._os import safe_join
if django.VERSION >= (1, 9):
from django.template.exceptions import TemplateDoesNotExist
else:
from django.template.base import TemplateDoesNotExist
# Local thread storage to retrieve the currently set theme
_local = threading.local()
@ -126,7 +119,7 @@ class ThemeMiddleware(object):
return response
class ThemeTemplateLoader(tLoaderCls):
class ThemeTemplateLoader(filesystem_loader.Loader):
"""Theme-aware template loader.
Themes can contain template overrides, so we need to check the
@ -162,32 +155,22 @@ class ThemeTemplateLoader(tLoaderCls):
this_theme[2],
'templates'
)
name = None
if not template_name.startswith('/'):
try:
yield safe_join(template_path, template_name)
name = safe_join(template_path, template_name)
except SuspiciousFileOperation:
yield os.path.join(
name = os.path.join(
this_theme[2], 'templates', template_name
)
elif template_path in template_name:
yield template_name
name = template_name
if name:
yield Origin(name=name,
template_name=template_name,
loader=self)
except UnicodeDecodeError:
# The template dir name wasn't valid UTF-8.
raise
except ValueError:
# The joined path was located outside of template_dir.
pass
def load_template_source(self, template_name, template_dirs=None):
for path in self.get_template_sources(template_name):
try:
with io.open(path, encoding=settings.FILE_CHARSET) as file:
return file.read(), path
except IOError:
pass
raise TemplateDoesNotExist(template_name)
e = Engine()
_loader = ThemeTemplateLoader(e)

View File

@ -122,8 +122,8 @@
{% endif %}
{% with default_item_value="<em>"|add:_("N/A")|add:"</em>" %}
{% for key, value in instance.metadata.items %}
<dt>{{ key|force_escape }}</dt>
<dd>{{ value|force_escape|default:default_item_value }}</dd>
<dt>{{ key }}</dt>
<dd>{{ value|default:default_item_value }}</dd>
{% endfor %}
{% endwith %}
</dl>