Skip to content

Commit

Permalink
Fixed #26495 -- Added name arg to Storage.save()'s File wrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Novikov authored and timgraham committed Apr 21, 2016
1 parent 65006e0 commit 4d1c229
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/core/files/storage.py
Expand Up @@ -48,7 +48,7 @@ def save(self, name, content, max_length=None):
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)
Expand Down
23 changes: 23 additions & 0 deletions tests/file_storage/tests.py
Expand Up @@ -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)
Expand Down

0 comments on commit 4d1c229

Please sign in to comment.