Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #775 from HiddenData/ticket-18899

Fixed #18899 -- FileSystemStorage.save should support any file-like objects
  • Loading branch information...
commit 129d2e8f85b1e80f10ec967b72ba0af9b239019f 2 parents 8c1cc4b + 664855b
Honza Král authored February 23, 2013
8  django/core/files/storage.py
@@ -37,13 +37,17 @@ def open(self, name, mode='rb'):
37 37
 
38 38
     def save(self, name, content):
39 39
         """
40  
-        Saves new content to the file specified by name. The content should be a
41  
-        proper File object, ready to be read from the beginning.
  40
+        Saves new content to the file specified by name. The content should be
  41
+        a proper File object or any python file-like object, ready to be read
  42
+        from the beginning.
42 43
         """
43 44
         # Get the proper name for the file, as it will actually be saved.
44 45
         if name is None:
45 46
             name = content.name
46 47
 
  48
+        if not hasattr(content, 'chunks'):
  49
+            content = File(content)
  50
+
47 51
         name = self.get_available_name(name)
48 52
         name = self._save(name, content)
49 53
 
30  tests/modeltests/files/tests.py
@@ -2,6 +2,7 @@
2 2
 
3 3
 import gzip
4 4
 import shutil
  5
+import StringIO
5 6
 import tempfile
6 7
 
7 8
 from django.core.cache import cache
@@ -102,6 +103,35 @@ def test_files(self):
102 103
         obj4.random.save("random_file", ContentFile("random content"))
103 104
         self.assertTrue(obj4.random.name.endswith("/random_file"))
104 105
 
  106
+    def test_file_object(self):
  107
+        # Create sample file
  108
+        temp_storage.save('tests/example.txt', ContentFile('some content'))
  109
+
  110
+        # Load it as python file object
  111
+        file_obj = open(temp_storage.path('tests/example.txt'))
  112
+
  113
+        # Save it using storage and read its content
  114
+        temp_storage.save('tests/file_obj', file_obj)
  115
+        self.assertTrue(temp_storage.exists('tests/file_obj'))
  116
+        self.assertEqual(
  117
+            temp_storage.open('tests/file_obj').read(),
  118
+            'some content')
  119
+
  120
+
  121
+    def test_stringio(self):
  122
+        # Test passing StringIO instance as content argument to save
  123
+        output = StringIO.StringIO()
  124
+        output.write('content')
  125
+        output.seek(0)
  126
+
  127
+        # Save it and read written file
  128
+        temp_storage.save('tests/stringio', output)
  129
+        self.assertTrue(temp_storage.exists('tests/stringio'))
  130
+        self.assertEqual(
  131
+            temp_storage.open('tests/stringio').read(),
  132
+            'content')
  133
+
  134
+
105 135
 
106 136
 class FileTests(unittest.TestCase):
107 137
     def test_context_manager(self):

0 notes on commit 129d2e8

Please sign in to comment.
Something went wrong with that request. Please try again.