7 import sys |
7 import sys |
8 |
8 |
9 from django.conf import settings |
9 from django.conf import settings |
10 from django.core.exceptions import ImproperlyConfigured |
10 from django.core.exceptions import ImproperlyConfigured |
11 from django.template import TemplateDoesNotExist |
11 from django.template import TemplateDoesNotExist |
|
12 from django.template.loader import BaseLoader |
12 from django.utils._os import safe_join |
13 from django.utils._os import safe_join |
13 from django.utils.importlib import import_module |
14 from django.utils.importlib import import_module |
14 |
15 |
15 # At compile time, cache the directories to search. |
16 # At compile time, cache the directories to search. |
16 fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() |
17 fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() |
17 app_template_dirs = [] |
18 app_template_dirs = [] |
18 for app in settings.INSTALLED_APPS: |
19 for app in settings.INSTALLED_APPS: |
19 try: |
20 try: |
20 mod = import_module(app) |
21 mod = import_module(app) |
21 except ImportError, e: |
22 except ImportError, e: |
22 raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0]) |
23 raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0])) |
23 template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') |
24 template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') |
24 if os.path.isdir(template_dir): |
25 if os.path.isdir(template_dir): |
25 app_template_dirs.append(template_dir.decode(fs_encoding)) |
26 app_template_dirs.append(template_dir.decode(fs_encoding)) |
26 |
27 |
27 # It won't change, so convert it to a tuple to save memory. |
28 # It won't change, so convert it to a tuple to save memory. |
28 app_template_dirs = tuple(app_template_dirs) |
29 app_template_dirs = tuple(app_template_dirs) |
29 |
30 |
30 def get_template_sources(template_name, template_dirs=None): |
31 class Loader(BaseLoader): |
31 """ |
32 is_usable = True |
32 Returns the absolute paths to "template_name", when appended to each |
33 |
33 directory in "template_dirs". Any paths that don't lie inside one of the |
34 def get_template_sources(self, template_name, template_dirs=None): |
34 template dirs are excluded from the result set, for security reasons. |
35 """ |
35 """ |
36 Returns the absolute paths to "template_name", when appended to each |
36 if not template_dirs: |
37 directory in "template_dirs". Any paths that don't lie inside one of the |
37 template_dirs = app_template_dirs |
38 template dirs are excluded from the result set, for security reasons. |
38 for template_dir in template_dirs: |
39 """ |
39 try: |
40 if not template_dirs: |
40 yield safe_join(template_dir, template_name) |
41 template_dirs = app_template_dirs |
41 except UnicodeDecodeError: |
42 for template_dir in template_dirs: |
42 # The template dir name was a bytestring that wasn't valid UTF-8. |
43 try: |
43 raise |
44 yield safe_join(template_dir, template_name) |
44 except ValueError: |
45 except UnicodeDecodeError: |
45 # The joined path was located outside of template_dir. |
46 # The template dir name was a bytestring that wasn't valid UTF-8. |
46 pass |
47 raise |
|
48 except ValueError: |
|
49 # The joined path was located outside of template_dir. |
|
50 pass |
|
51 |
|
52 def load_template_source(self, template_name, template_dirs=None): |
|
53 for filepath in self.get_template_sources(template_name, template_dirs): |
|
54 try: |
|
55 file = open(filepath) |
|
56 try: |
|
57 return (file.read().decode(settings.FILE_CHARSET), filepath) |
|
58 finally: |
|
59 file.close() |
|
60 except IOError: |
|
61 pass |
|
62 raise TemplateDoesNotExist(template_name) |
|
63 |
|
64 _loader = Loader() |
47 |
65 |
48 def load_template_source(template_name, template_dirs=None): |
66 def load_template_source(template_name, template_dirs=None): |
49 for filepath in get_template_sources(template_name, template_dirs): |
67 # For backwards compatibility |
50 try: |
68 import warnings |
51 return (open(filepath).read().decode(settings.FILE_CHARSET), filepath) |
69 warnings.warn( |
52 except IOError: |
70 "'django.template.loaders.app_directories.load_template_source' is deprecated; use 'django.template.loaders.app_directories.Loader' instead.", |
53 pass |
71 PendingDeprecationWarning |
54 raise TemplateDoesNotExist, template_name |
72 ) |
|
73 return _loader.load_template_source(template_name, template_dirs) |
55 load_template_source.is_usable = True |
74 load_template_source.is_usable = True |