From fb9d8f06520f495d0c36236f7534dbe660c7e164 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 28 May 2014 19:03:26 +0200 Subject: [PATCH] Fixed #22717 -- Auto-corrected missing ending slash in FileSystemStorage Thanks David Fischer for the report and Moayad Mardini for the review. --- django/core/files/storage.py | 2 ++ tests/file_storage/tests.py | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 98864f7f22..a36440ab4c 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -159,6 +159,8 @@ class FileSystemStorage(Storage): self.location = abspathu(self.base_location) if base_url is None: base_url = settings.MEDIA_URL + elif not base_url.endswith('/'): + base_url += '/' self.base_url = base_url self.file_permissions_mode = ( file_permissions_mode if file_permissions_mode is not None diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 392cd871c3..da9410d70f 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -96,7 +96,7 @@ class FileStorageTests(unittest.TestCase): shutil.rmtree(self.temp_dir) shutil.rmtree(self.temp_dir2) - def test_emtpy_location(self): + def test_empty_location(self): """ Makes sure an exception is raised if the location is empty """ @@ -239,6 +239,14 @@ class FileStorageTests(unittest.TestCase): self.storage.base_url = None self.assertRaises(ValueError, self.storage.url, 'test.file') + # #22717: missing ending slash in base_url should be auto-corrected + storage = self.storage_class(location=self.temp_dir, + base_url='/no_ending_slash') + self.assertEqual( + storage.url('test.file'), + '%s%s' % (storage.base_url, 'test.file') + ) + def test_listdir(self): """ File storage returns a tuple containing directories and files.