Fixed #27836 -- Allowed FileSystemStorage.delete() to remove directories.

This commit is contained in:
chillaranand 2017-02-22 20:14:25 +05:30 committed by Tim Graham
parent 339d526d55
commit e4025563ea
2 changed files with 12 additions and 4 deletions

View File

@ -293,12 +293,15 @@ class FileSystemStorage(Storage):
def delete(self, name): def delete(self, name):
assert name, "The name argument is not allowed to be empty." assert name, "The name argument is not allowed to be empty."
name = self.path(name) 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: try:
if os.path.isdir(name):
os.rmdir(name)
else:
os.remove(name) os.remove(name)
except FileNotFoundError: except FileNotFoundError:
# If os.remove() fails with FileNotFoundError, the file may have # If removal fails, the file or directory may have been removed
# been removed concurrently. # concurrently.
pass pass
def exists(self, name): def exists(self, name):

View File

@ -495,6 +495,11 @@ class FileStorageTests(SimpleTestCase):
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
self.storage.delete('') 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( @override_settings(
MEDIA_ROOT='media_root', MEDIA_ROOT='media_root',
MEDIA_URL='media_url/', MEDIA_URL='media_url/',