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
|
2007-07-23 12:45:01 +08:00
|
|
|
import os
|
2008-11-14 03:03:42 +08:00
|
|
|
import sys
|
2005-10-17 10:37:50 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
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
|
2009-12-14 20:08:23 +08:00
|
|
|
from django.template.loader import BaseLoader
|
2007-07-23 12:45:01 +08:00
|
|
|
from django.utils._os import safe_join
|
2012-08-08 19:07:49 +08:00
|
|
|
from django.utils import six
|
2005-10-17 10:37:50 +08:00
|
|
|
|
2013-12-27 18:17:25 +08:00
|
|
|
|
|
|
|
def calculate_app_template_dirs():
|
|
|
|
if six.PY2:
|
|
|
|
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
|
|
|
|
app_template_dirs = []
|
|
|
|
for app_config in apps.get_app_configs():
|
2013-12-27 18:42:24 +08:00
|
|
|
if not app_config.path:
|
|
|
|
continue
|
2013-12-27 18:17:25 +08:00
|
|
|
template_dir = os.path.join(app_config.path, 'templates')
|
|
|
|
if os.path.isdir(template_dir):
|
|
|
|
if six.PY2:
|
|
|
|
template_dir = template_dir.decode(fs_encoding)
|
|
|
|
app_template_dirs.append(template_dir)
|
|
|
|
return tuple(app_template_dirs)
|
|
|
|
|
|
|
|
|
2005-10-17 10:37:50 +08:00
|
|
|
# At compile time, cache the directories to search.
|
2013-12-27 18:17:25 +08:00
|
|
|
app_template_dirs = calculate_app_template_dirs()
|
2005-10-17 10:37:50 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
class Loader(BaseLoader):
|
|
|
|
is_usable = 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 = app_template_dirs
|
|
|
|
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-12 05:36:41 +08:00
|
|
|
with io.open(filepath, encoding=settings.FILE_CHARSET) as fp:
|
|
|
|
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)
|