2007-07-23 12:45:01 +08:00
|
|
|
"""
|
2009-04-02 09:55:49 +08:00
|
|
|
Wrapper for loading templates from "templates" directories in INSTALLED_APPS
|
2007-07-23 12:45:01 +08:00
|
|
|
packages.
|
|
|
|
"""
|
|
|
|
|
2014-11-12 05:36:41 +08:00
|
|
|
import io
|
2005-10-17 10:37:50 +08:00
|
|
|
|
2014-11-12 01:59:49 +08:00
|
|
|
from django.core.exceptions import SuspiciousFileOperation
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.template.base import TemplateDoesNotExist
|
2014-11-11 04:04:04 +08:00
|
|
|
from django.template.utils import get_app_template_dirs
|
2007-07-23 12:45:01 +08:00
|
|
|
from django.utils._os import safe_join
|
2005-10-17 10:37:50 +08:00
|
|
|
|
2014-11-16 01:35:02 +08:00
|
|
|
from .base import Loader as BaseLoader
|
|
|
|
|
2013-12-27 18:17:25 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
class Loader(BaseLoader):
|
|
|
|
|
|
|
|
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:
|
2014-11-11 04:04:04 +08:00
|
|
|
template_dirs = get_app_template_dirs('templates')
|
2009-12-14 20:08:23 +08:00
|
|
|
for template_dir in template_dirs:
|
|
|
|
try:
|
|
|
|
yield safe_join(template_dir, template_name)
|
2014-11-12 01:59:49 +08:00
|
|
|
except SuspiciousFileOperation:
|
|
|
|
# The joined path was located outside of this template_dir
|
|
|
|
# (it might be inside another one, so this isn't fatal).
|
2009-12-14 20:08:23 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def load_template_source(self, template_name, template_dirs=None):
|
|
|
|
for filepath in self.get_template_sources(template_name, template_dirs):
|
|
|
|
try:
|
2014-11-21 04:27:56 +08:00
|
|
|
with io.open(filepath, encoding=self.engine.file_charset) as fp:
|
2014-11-12 05:36:41 +08:00
|
|
|
return fp.read(), filepath
|
2009-12-14 20:08:23 +08:00
|
|
|
except IOError:
|
|
|
|
pass
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TemplateDoesNotExist(template_name)
|