Changeset 5750
- Timestamp:
- 07/22/07 23:45:01 (1 year ago)
- Files:
-
- django/trunk/django/template/loaders/app_directories.py (modified) (2 diffs)
- django/trunk/django/template/loaders/filesystem.py (modified) (2 diffs)
- django/trunk/django/utils/_os.py (added)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/template/loaders/app_directories.py
r5609 r5750 1 # Wrapper for loading templates from "template" directories in installed app packages. 1 """ 2 Wrapper for loading templates from "template" directories in INSTALLED_APPS 3 packages. 4 """ 5 6 import os 2 7 3 8 from django.conf import settings 4 9 from django.core.exceptions import ImproperlyConfigured 5 10 from django.template import TemplateDoesNotExist 6 import os 11 from django.utils._os import safe_join 7 12 8 13 # At compile time, cache the directories to search. … … 29 34 30 35 def get_template_sources(template_name, template_dirs=None): 31 for template_dir in app_template_dirs: 32 yield os.path.join(template_dir, template_name) 36 if not template_dirs: 37 template_dirs = app_template_dirs 38 for template_dir in template_dirs: 39 try: 40 yield safe_join(template_dir, template_name) 41 except ValueError: 42 # The joined path was located outside of template_dir. 43 pass 33 44 34 45 def load_template_source(template_name, template_dirs=None): django/trunk/django/template/loaders/filesystem.py
r5609 r5750 1 # Wrapper for loading templates from the filesystem. 1 """ 2 Wrapper for loading templates from the filesystem. 3 """ 2 4 3 5 from django.conf import settings 4 6 from django.template import TemplateDoesNotExist 5 import os 7 from django.utils._os import safe_join 6 8 7 9 def get_template_sources(template_name, template_dirs=None): … … 9 11 template_dirs = settings.TEMPLATE_DIRS 10 12 for template_dir in template_dirs: 11 yield os.path.join(template_dir, template_name) 13 try: 14 yield safe_join(template_dir, template_name) 15 except ValueError: 16 # The joined path was located outside of template_dir. 17 pass 12 18 13 19 def load_template_source(template_name, template_dirs=None): django/trunk/tests/regressiontests/templates/tests.py
r5728 r5750 7 7 settings.configure() 8 8 9 import os 10 import unittest 11 from datetime import datetime, timedelta 12 9 13 from django import template 10 14 from django.template import loader 15 from django.template.loaders import app_directories, filesystem 11 16 from django.utils.translation import activate, deactivate, install, ugettext as _ 12 17 from django.utils.tzinfo import LocalTimezone 13 from datetime import datetime, timedelta 18 14 19 from unicode import unicode_tests 15 import unittest16 20 17 21 # Some other tests we would like to run … … 76 80 77 81 class Templates(unittest.TestCase): 82 def test_loaders_security(self): 83 def test_template_sources(path, template_dirs, expected_sources): 84 # Fix expected sources so they are normcased and abspathed 85 expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources] 86 # Test app_directories loader 87 sources = app_directories.get_template_sources(path, template_dirs) 88 self.assertEqual(list(sources), expected_sources) 89 # Test filesystem loader 90 sources = filesystem.get_template_sources(path, template_dirs) 91 self.assertEqual(list(sources), expected_sources) 92 93 template_dirs = ['/dir1', '/dir2'] 94 test_template_sources('index.html', template_dirs, 95 ['/dir1/index.html', '/dir2/index.html']) 96 test_template_sources('/etc/passwd', template_dirs, 97 []) 98 test_template_sources('etc/passwd', template_dirs, 99 ['/dir1/etc/passwd', '/dir2/etc/passwd']) 100 test_template_sources('../etc/passwd', template_dirs, 101 []) 102 test_template_sources('../../../etc/passwd', template_dirs, 103 []) 104 test_template_sources('/dir1/index.html', template_dirs, 105 ['/dir1/index.html']) 106 test_template_sources('../dir2/index.html', template_dirs, 107 ['/dir2/index.html']) 108 test_template_sources('/dir1blah', template_dirs, 109 []) 110 test_template_sources('../dir1blah', template_dirs, 111 []) 112 113 # Case insensitive tests (for win32). Not run unless we're on 114 # a case insensitive operating system. 115 if os.path.normcase('/TEST') == os.path.normpath('/test'): 116 template_dirs = ['/dir1', '/DIR2'] 117 test_template_sources('index.html', template_dirs, 118 ['/dir1/index.html', '/dir2/index.html']) 119 test_template_sources('/DIR1/index.HTML', template_dirs, 120 ['/dir1/index.html']) 121 78 122 def test_templates(self): 79 123 # NOW and NOW_tz are used by timesince tag tests.
