From 4d1c229ee5cb210e8b592a8d9c87d4a66864328e Mon Sep 17 00:00:00 2001 From: Maxim Novikov Date: Wed, 13 Apr 2016 10:38:56 +0500 Subject: [PATCH] Fixed #26495 -- Added name arg to Storage.save()'s File wrapping. --- django/core/files/storage.py | 2 +- tests/file_storage/tests.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/django/core/files/storage.py b/django/core/files/storage.py index cc91f67b07..371efb92e1 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -48,7 +48,7 @@ class Storage(object): name = content.name if not hasattr(content, 'chunks'): - content = File(content) + content = File(content, name) name = self.get_available_name(name, max_length=max_length) name = self._save(name, content) diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index d1ea0021c5..42699dddb8 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -644,6 +644,29 @@ class CustomStorageLegacyDatetimeHandlingTests(FileStorageTests): storage_class = CustomStorageLegacyDatetimeHandling +class DiscardingFalseContentStorage(FileSystemStorage): + def _save(self, name, content): + if content: + return super(DiscardingFalseContentStorage, self)._save(name, content) + return '' + + +class DiscardingFalseContentStorageTests(FileStorageTests): + storage_class = DiscardingFalseContentStorage + + def test_custom_storage_discarding_empty_content(self): + """ + When Storage.save() wraps a file-like object in File, it should include + the name argument so that bool(file) evaluates to True (#26495). + """ + output = six.StringIO('content') + self.storage.save('tests/stringio', output) + self.assertTrue(self.storage.exists('tests/stringio')) + + with self.storage.open('tests/stringio') as f: + self.assertEqual(f.read(), b'content') + + class FileFieldStorageTests(TestCase): def tearDown(self): shutil.rmtree(temp_storage_location)