diff --git a/categories/models.py b/categories/models.py index 3c16ca2..2585364 100644 --- a/categories/models.py +++ b/categories/models.py @@ -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 diff --git a/categories/tests/test_image.jpg b/categories/tests/test_image.jpg new file mode 100644 index 0000000..65fb511 Binary files /dev/null and b/categories/tests/test_image.jpg differ diff --git a/categories/tests/test_models.py b/categories/tests/test_models.py new file mode 100644 index 0000000..5649bee --- /dev/null +++ b/categories/tests/test_models.py @@ -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) diff --git a/tox.ini b/tox.ini index 7444ae3..ed3e9af 100644 --- a/tox.ini +++ b/tox.ini @@ -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=