Fixed a ValueError that happened when trying to save a Category that has a thumbnail

This commit is contained in:
Brent O'Connor 2017-03-29 20:43:55 -05:00
parent 5b230ef4f5
commit 53b92f3f0f
4 changed files with 25 additions and 6 deletions

View file

@ -1,3 +1,4 @@
from django.core.files.images import get_image_dimensions
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import force_text
@ -81,12 +82,7 @@ class Category(CategoryBase):
def save(self, *args, **kwargs):
if self.thumbnail:
from django.core.files.images import get_image_dimensions
import django
if django.VERSION[1] < 2:
width, height = get_image_dimensions(self.thumbnail.file)
else:
width, height = get_image_dimensions(self.thumbnail.file, close=True)
width, height = get_image_dimensions(self.thumbnail.file)
else:
width, height = None, None

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

View file

@ -0,0 +1,21 @@
import os
from django.core.files import File
from django.core.files.uploadedfile import UploadedFile
from categories.models import Category
from django.test import TestCase
class TestCategoryThumbnail(TestCase):
def test_thumbnail(self):
file_name = 'test_image.jpg'
with open(os.path.join(os.path.dirname(__file__), file_name), 'rb') as f:
test_image = UploadedFile(File(f), content_type='image/jpeg')
category = Category.objects.create(name='Test Category', slug='test-category', thumbnail=test_image)
self.assertEqual(category.pk, 1)
self.assertEqual(category.thumbnail_width, 640)
self.assertEqual(category.thumbnail_height, 480)

View file

@ -11,6 +11,8 @@ deps=
django19: Django==1.9.2
django18: Django==1.8.9
coverage==4.0.3
pillow
ipdb
-r{toxinidir}/requirements.txt
commands=