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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2008-11-14 03:03:42 +08:00
|
|
|
import sys
|
2005-10-17 10:37:50 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2005-10-21 07:16:45 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
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
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2005-10-17 10:37:50 +08:00
|
|
|
|
|
|
|
# At compile time, cache the directories to search.
|
2008-11-14 03:03:42 +08:00
|
|
|
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
|
2005-10-17 10:37:50 +08:00
|
|
|
app_template_dirs = []
|
2006-05-02 09:31:56 +08:00
|
|
|
for app in settings.INSTALLED_APPS:
|
2005-10-21 07:16:45 +08:00
|
|
|
try:
|
2009-03-19 00:55:59 +08:00
|
|
|
mod = import_module(app)
|
2005-10-21 07:16:45 +08:00
|
|
|
except ImportError, e:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
|
2005-10-17 10:37:50 +08:00
|
|
|
template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
|
|
|
|
if os.path.isdir(template_dir):
|
2008-11-14 03:03:42 +08:00
|
|
|
app_template_dirs.append(template_dir.decode(fs_encoding))
|
2005-10-17 10:37:50 +08:00
|
|
|
|
|
|
|
# It won't change, so convert it to a tuple to save memory.
|
|
|
|
app_template_dirs = tuple(app_template_dirs)
|
|
|
|
|
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)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
# The template dir name was a bytestring that 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 filepath in self.get_template_sources(template_name, template_dirs):
|
|
|
|
try:
|
|
|
|
file = open(filepath)
|
|
|
|
try:
|
|
|
|
return (file.read().decode(settings.FILE_CHARSET), filepath)
|
|
|
|
finally:
|
|
|
|
file.close()
|
|
|
|
except IOError:
|
|
|
|
pass
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TemplateDoesNotExist(template_name)
|
2009-12-14 20:08:23 +08:00
|
|
|
|
|
|
|
_loader = Loader()
|
2005-11-25 05:14:42 +08:00
|
|
|
|
|
|
|
def load_template_source(template_name, template_dirs=None):
|
2009-12-14 20:08:23 +08:00
|
|
|
# For backwards compatibility
|
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"'django.template.loaders.app_directories.load_template_source' is deprecated; use 'django.template.loaders.app_directories.Loader' instead.",
|
2010-10-11 20:20:07 +08:00
|
|
|
DeprecationWarning
|
2009-12-14 20:08:23 +08:00
|
|
|
)
|
|
|
|
return _loader.load_template_source(template_name, template_dirs)
|
2005-10-17 10:37:50 +08:00
|
|
|
load_template_source.is_usable = True
|