horizon/horizon/loaders.py
Akihiro Motoki b017879500 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
2018-03-03 07:22:35 +09:00

50 lines
1.9 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Wrapper for loading templates from "templates" directories in panel modules.
"""
import os
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
# Set up a cache of the panel directories to search.
panel_template_dirs = {}
class TemplateLoader(filesystem_loader.Loader):
def get_template_sources(self, template_name):
bits = template_name.split('/', 2)
if len(bits) == 3:
dash_name, panel_name, remainder = bits
key = os.path.join(dash_name, panel_name)
if key in panel_template_dirs:
template_dir = panel_template_dirs[key]
try:
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 SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
pass