summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchillaranand <anand21nanda@gmail.com>2017-02-22 20:14:25 +0530
committerTim Graham <timograham@gmail.com>2017-02-24 16:02:33 -0500
commite4025563ea87b0e3acb1d617ebfcc0b8789f75e7 (patch)
treef7608d1ff7677cf1deb0083675bba4bfa1d7797e
parent339d526d55baffea3bd3cecd0e07ddf644028e8c (diff)
Fixed #27836 -- Allowed FileSystemStorage.delete() to remove directories.
-rw-r--r--django/core/files/storage.py11
-rw-r--r--tests/file_storage/tests.py5
2 files changed, 12 insertions, 4 deletions
diff --git a/django/core/files/storage.py b/django/core/files/storage.py
index 1ee9533eb2..b3bb2dc589 100644
--- a/django/core/files/storage.py
+++ b/django/core/files/storage.py
@@ -293,12 +293,15 @@ class FileSystemStorage(Storage):
def delete(self, name):
assert name, "The name argument is not allowed to be empty."
name = self.path(name)
- # If the file exists, delete it from the filesystem.
+ # If the file or directory exists, delete it from the filesystem.
try:
- os.remove(name)
+ if os.path.isdir(name):
+ os.rmdir(name)
+ else:
+ os.remove(name)
except FileNotFoundError:
- # If os.remove() fails with FileNotFoundError, the file may have
- # been removed concurrently.
+ # If removal fails, the file or directory may have been removed
+ # concurrently.
pass
def exists(self, name):
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index 494832badd..b6a6f8c2f1 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -495,6 +495,11 @@ class FileStorageTests(SimpleTestCase):
with self.assertRaises(AssertionError):
self.storage.delete('')
+ def test_delete_deletes_directories(self):
+ tmp_dir = tempfile.TemporaryDirectory(dir=self.storage.location)
+ self.storage.delete(tmp_dir.name)
+ self.assertFalse(os.path.exists(tmp_dir.name))
+
@override_settings(
MEDIA_ROOT='media_root',
MEDIA_URL='media_url/',