Chris Beaven avatar Chris Beaven committed 29136f1

Fix an error reported in issue 95 with uploading to a ThumbnailField via a form (either admin or ModelForm)
Done by changing to using a ContentFile rather than a SimpleUploadedFile in ThumbnailFieldFile

Comments (0)

Files changed (2)

sorl/thumbnail/fields.py

     from StringIO import StringIO
 
 from django.db.models.fields.files import ImageField, ImageFieldFile
-from django.core.files.uploadedfile import SimpleUploadedFile
+from django.core.files.base import ContentFile
 from django.utils.safestring import mark_safe
 from django.utils.functional import curry
 from django.utils.html import escape
                 continue
             thumbnail_kwargs[argk] = self.field.thumbnail[k]
         Thumbnail(source=content, dest=new_content, **thumbnail_kwargs)
-        new_content = SimpleUploadedFile(name, new_content.read(),
-                                         content.content_type)
+        new_content = ContentFile(new_content.read())
         super(ThumbnailFieldFile, self).save(name, new_content, *args,
                                              **kwargs)
 

sorl/thumbnail/tests/fields.py

 
 from django.db import models
 from django.conf import settings
-from django.core.files.uploadedfile import SimpleUploadedFile
+from django.core.files.base import ContentFile
 
 from sorl.thumbnail.fields import ImageWithThumbnailsField, ThumbnailField
 from sorl.thumbnail.tests.base import BaseTest, RELATIVE_PIC_NAME, PIC_NAME
         self.images_to_delete.add(admin_thumb)
         # Default setting is to only generate when the thumbnail is used.
         model = TestThumbnailFieldModel()
-        source = SimpleUploadedFile('_', open(PIC_NAME).read())
+        source = ContentFile(open(PIC_NAME).read())
         model.photo.save(RELATIVE_PIC_NAME, source, save=False)
         self.images_to_delete.add(model.photo.path)
         self.assertFalse(os.path.exists(main_thumb))
         os.remove(model.photo.path)
         # But it's easy to set it up the other way...
         model = TestThumbnailFieldGenerateModel()
-        source = SimpleUploadedFile('_', open(PIC_NAME).read())
+        source = ContentFile(open(PIC_NAME).read())
         model.photo.save(RELATIVE_PIC_NAME, source, save=False)
         self.assert_(os.path.exists(main_thumb))
         self.assert_(os.path.exists(admin_thumb))
 class ThumbnailFieldTest(BaseTest):
     def test_thumbnail(self):
         model = TestThumbnailFieldModel()
-        source = SimpleUploadedFile('_', open(PIC_NAME).read())
+        source = ContentFile(open(PIC_NAME).read())
         dest_name = 'sorl-thumbnail-test_dest.jpg'
         model.avatar.save(dest_name, source, save=False)
         expected_filename = os.path.join(model.avatar.path)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.