Skip to content

Commit

Permalink
Fixing OnDiscPlaceholderImage empty file bug #13
Browse files Browse the repository at this point in the history
Includes:
* Updating tests
* Swapping out placeholder.gif for placeholder.png (since PIL doesn’t
know how to invert ‘P’ images:
https://github.com/python-pillow/Pillow/blob/master/PIL/ImageOps.py#L50)
  • Loading branch information
respondcreate committed May 15, 2015
1 parent e0e37f0 commit 52f610e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
Binary file removed tests/media/on-storage-placeholder/placeholder.gif
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class VersatileImageTestModel(models.Model):
placeholder_image=OnDiscPlaceholderImage(
path=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'placeholder.gif'
'placeholder.png'
)
)
)
optional_image_2 = VersatileImageField(
upload_to='./',
blank=True,
placeholder_image=OnStoragePlaceholderImage(
path='on-storage-placeholder/placeholder.gif'
path='on-storage-placeholder/placeholder.png'
)
)
optional_image_3 = VersatileImageField(
Expand Down
Binary file removed tests/placeholder.gif
Binary file not shown.
Binary file added tests/placeholder.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 19 additions & 11 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def setUp(self):
self.user = user
self.client = client

def tearDown(self):
@classmethod
def tearDownClass(cls):
"""
Deletes files made by VersatileImageFields during tests
"""
Expand Down Expand Up @@ -171,35 +172,42 @@ def test_invert_plus_thumbnail_sizer_filtered_path(self):

def test_placeholder_image(self):
"""Ensures placehold.it integration"""
self.jpg.optional_image.create_on_demand = True
self.assertEqual(
self.jpg.optional_image.crop['100x100'].url,
'/media/__sized__/__placeholder__/'
'placeholder-crop-c0-5__0-5-100x100.gif'
'placeholder-crop-c0-5__0-5-100x100.png'
)
self.assertEqual(
self.jpg.optional_image.thumbnail['100x100'].url,
'/media/__sized__/__placeholder__/'
'placeholder-thumbnail-100x100.gif'
'placeholder-thumbnail-100x100.png'
)
self.assertEqual(
self.jpg.optional_image.filters.invert.url,
'/media/__placeholder__/__filtered__/placeholder__invert__.gif'
'/media/__placeholder__/__filtered__/placeholder__invert__.png'
)
self.assertEqual(
self.jpg.optional_image_2.crop['100x100'].url,
'/media/__sized__/__placeholder__/on-storage-placeholder/'
'placeholder-crop-c0-5__0-5-100x100.gif'
'placeholder-crop-c0-5__0-5-100x100.png'
)
self.assertEqual(
self.jpg.optional_image_2.thumbnail['100x100'].url,
'/media/__sized__/__placeholder__/on-storage-placeholder/'
'placeholder-thumbnail-100x100.gif'
'placeholder-thumbnail-100x100.png'
)
self.assertEqual(
self.jpg.optional_image_2.filters.invert.url,
'/media/__placeholder__/on-storage-placeholder/__filtered__/'
'placeholder__invert__.gif'
'placeholder__invert__.png'
)
self.assertFalse(
self.jpg.optional_image.field.storage.size(
self.jpg.optional_image.name
) is 0
)
self.jpg.optional_image.create_on_demand = False

def test_setting_ppoi_values(self):
"""Ensure PPOI values are set correctly"""
Expand Down Expand Up @@ -571,11 +579,11 @@ def test_horizontal_and_vertical_crop(self):

def test_DummyFilter(self):
"""Tests placeholder image functionality for filters"""
test_jpg = VersatileImageTestModel.objects.get(
test_png = VersatileImageTestModel.objects.get(
img_type='png'
)
test_jpg.optional_image.create_on_demand = True
test_jpg.optional_image.filters.invert.url
test_png.optional_image.create_on_demand = True
test_png.optional_image.filters.invert.url

def test_crop_and_thumbnail_key_assignment(self):
"""Tests placeholder image functionality for filters"""
Expand Down Expand Up @@ -772,7 +780,7 @@ def test_template_rendering(self):
<img id="image-thumbnail" src="/media/__sized__/python-logo-thumbnail-400x400.jpg" />
<img id="image-invert" src="/media/__filtered__/python-logo__invert__.jpg" />
<img id="image-invert-crop" src="/media/__sized__/__filtered__/python-logo__invert__-crop-c0-25__0-25-400x400.jpg" />
<img src="/media/__sized__/__placeholder__/placeholder-crop-c0-5__0-5-400x400.gif" id="optional-image-crop"/>
<img src="/media/__sized__/__placeholder__/placeholder-crop-c0-5__0-5-400x400.png" id="optional-image-crop"/>
</body>
</html>
"""
Expand Down
10 changes: 7 additions & 3 deletions versatileimagefield/placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def __init__(self, file, name):
`file` - A python file instance.
`name` - The desired filename of `file`.
"""
self.image_data = ContentFile(file.read(), name=name)
if isinstance(file, ContentFile):
image_data = file
else:
image_data = ContentFile(file.read(), name=name)
self.image_data = image_data
file.close()


Expand All @@ -33,8 +37,8 @@ def __init__(self, path):
"""
folder, name = os.path.split(path)
file = open(path, 'rb')
self.image_data = ContentFile(file.read(), name=name)
super(OnDiscPlaceholderImage, self).__init__(file, name)
content_file = ContentFile(file.read(), name=name)
super(OnDiscPlaceholderImage, self).__init__(content_file, name)


class OnStoragePlaceholderImage(PlaceholderImage):
Expand Down

0 comments on commit 52f610e

Please sign in to comment.