2007-07-23 12:45:01 +08:00
|
|
|
"""
|
|
|
|
Wrapper for loading templates from "template" directories in INSTALLED_APPS
|
|
|
|
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
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.template import TemplateDoesNotExist
|
2007-07-23 12:45:01 +08:00
|
|
|
from django.utils._os import safe_join
|
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-17 10:37:50 +08:00
|
|
|
i = app.rfind('.')
|
2005-10-21 07:16:45 +08:00
|
|
|
if i == -1:
|
|
|
|
m, a = app, None
|
|
|
|
else:
|
|
|
|
m, a = app[:i], app[i+1:]
|
|
|
|
try:
|
|
|
|
if a is None:
|
2006-10-31 04:50:27 +08:00
|
|
|
mod = __import__(m, {}, {}, [])
|
2005-10-21 07:16:45 +08:00
|
|
|
else:
|
2006-10-31 04:50:27 +08:00
|
|
|
mod = getattr(__import__(m, {}, {}, [a]), a)
|
2005-10-21 07:16:45 +08:00
|
|
|
except ImportError, e:
|
|
|
|
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)
|
|
|
|
|
2005-11-25 05:14:42 +08:00
|
|
|
def get_template_sources(template_name, template_dirs=None):
|
2008-10-06 14:34:54 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2007-07-23 12:45:01 +08:00
|
|
|
if not template_dirs:
|
|
|
|
template_dirs = app_template_dirs
|
|
|
|
for template_dir in template_dirs:
|
|
|
|
try:
|
|
|
|
yield safe_join(template_dir, template_name)
|
2008-10-06 14:34:54 +08:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
# The template dir name was a bytestring that wasn't valid UTF-8.
|
|
|
|
raise
|
2007-07-23 12:45:01 +08:00
|
|
|
except ValueError:
|
|
|
|
# The joined path was located outside of template_dir.
|
|
|
|
pass
|
2005-11-25 05:14:42 +08:00
|
|
|
|
|
|
|
def load_template_source(template_name, template_dirs=None):
|
|
|
|
for filepath in get_template_sources(template_name, template_dirs):
|
2005-10-17 10:37:50 +08:00
|
|
|
try:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
|
2005-10-17 10:37:50 +08:00
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
raise TemplateDoesNotExist, template_name
|
|
|
|
load_template_source.is_usable = True
|