Ticket #15641: 15641-2.diff
File 15641-2.diff, 3.7 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/file_storage/tests.py
19 19 from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured 20 20 from django.core.files.base import ContentFile, File 21 21 from django.core.files.images import get_image_dimensions 22 from django.core.files.storage import FileSystemStorage, get_storage_class 22 from django.core.files.storage import FileSystemStorage, get_storage_class, UNUSABLE_BASE_URL 23 23 from django.core.files.uploadedfile import UploadedFile 24 24 from django.utils import unittest 25 25 … … 88 88 89 89 class FileStorageTests(unittest.TestCase): 90 90 storage_class = FileSystemStorage 91 init_base_url = '/test_media_url/' 92 test_base_url = None 91 93 92 94 def setUp(self): 93 95 self.temp_dir = tempfile.mktemp() 94 96 os.makedirs(self.temp_dir) 95 97 self.storage = self.storage_class(location=self.temp_dir, 96 base_url='/test_media_url/') 98 base_url=self.init_base_url) 99 if self.test_base_url is None: 100 self.test_base_url = self.init_base_url 97 101 98 102 def tearDown(self): 99 103 shutil.rmtree(self.temp_dir) … … 207 211 # should encode special chars except ~!*()' 208 212 # like encodeURIComponent() JavaScript function do 209 213 self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""), 210 """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")214 self.test_base_url + """~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""") 211 215 212 216 # should stanslate os path separator(s) to the url path separator 213 217 self.assertEqual(self.storage.url("""a/b\\c.file"""), 214 """/test_media_url/a/b/c.file""")218 self.test_base_url + """a/b/c.file""") 215 219 216 220 self.storage.base_url = None 217 221 self.assertRaises(ValueError, self.storage.url, 'test.file') … … 265 269 self.assertRaises(SuspiciousOperation, self.storage.exists, '..') 266 270 self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd') 267 271 272 class NoUrlFileStorageTests(FileStorageTests): 273 init_base_url = None 274 275 def test_file_url(self): 276 """ 277 File storage raises ValueError if initialized with base_url=None 278 """ 279 self.assertRaises(ValueError, self.storage.url, 'test.file') 280 281 class DefaultUrlFileStorageTests(FileStorageTests): 282 init_base_url = UNUSABLE_BASE_URL 283 test_base_url = '/test_media_url/' 284 285 def setUp(self): 286 self.old_media_url = settings.MEDIA_URL 287 settings.MEDIA_URL = self.test_base_url 288 super(DefaultUrlFileStorageTests, self).setUp() 289 290 def tearDown(self): 291 super(DefaultUrlFileStorageTests, self).tearDown() 292 settings.MEDIA_URL = self.old_media_url 293 268 294 class CustomStorage(FileSystemStorage): 269 295 def get_available_name(self, name): 270 296 """ -
django/core/files/storage.py
142 142 """ 143 143 raise NotImplementedError() 144 144 145 UNUSABLE_BASE_URL = object() 146 145 147 class FileSystemStorage(Storage): 146 148 """ 147 149 Standard filesystem storage 148 150 """ 149 151 150 def __init__(self, location=None, base_url= None):152 def __init__(self, location=None, base_url=UNUSABLE_BASE_URL): 151 153 if location is None: 152 154 location = settings.MEDIA_ROOT 153 if base_url is None:155 if base_url is UNUSABLE_BASE_URL: 154 156 base_url = settings.MEDIA_URL 155 157 self.location = os.path.abspath(location) 156 158 self.base_url = base_url