From d871180cdf4e6ffe4699382f9e67090c61139da8 Mon Sep 17 00:00:00 2001 From: Will Stott Date: Wed, 17 Jul 2019 23:54:18 +0100 Subject: [PATCH] Use caller's Image object when fetching or creating a thumbnail. --- core/models.py | 2 +- core/serializers.py | 2 +- core/tests/api.py | 2 +- core/tests/helpers.py | 2 +- django_images/models.py | 3 +-- django_images/tests.py | 24 ++++++++++++------------ django_images/views.py | 7 +++---- 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/core/models.py b/core/models.py index 0de747d..86eee38 100644 --- a/core/models.py +++ b/core/models.py @@ -36,7 +36,7 @@ class ImageManager(models.Manager): # try to create thumbnails one by one later image = self.create(image=obj) for size in settings.IMAGE_SIZES.keys(): - Thumbnail.objects.get_or_create_at_size(image.pk, size) + Thumbnail.objects.get_or_create_at_size(image, size) return image diff --git a/core/serializers.py b/core/serializers.py index 070b253..bb7193d 100644 --- a/core/serializers.py +++ b/core/serializers.py @@ -53,7 +53,7 @@ class ImageSerializer(serializers.ModelSerializer): def create(self, validated_data): image = super(ImageSerializer, self).create(validated_data) for size in settings.IMAGE_SIZES: - Thumbnail.objects.get_or_create_at_size(image.pk, size) + Thumbnail.objects.get_or_create_at_size(image, size) return image diff --git a/core/tests/api.py b/core/tests/api.py index 2e3e588..eda173a 100644 --- a/core/tests/api.py +++ b/core/tests/api.py @@ -14,7 +14,7 @@ from core.models import Pin, Image def filter_generator_for(size): def wrapped_func(obj): - return Thumbnail.objects.get_or_create_at_size(obj.pk, size) + return Thumbnail.objects.get_or_create_at_size(obj, size) return wrapped_func diff --git a/core/tests/helpers.py b/core/tests/helpers.py index be0bd27..13d9718 100644 --- a/core/tests/helpers.py +++ b/core/tests/helpers.py @@ -33,7 +33,7 @@ def create_tag(name): def create_image(): image = Image.objects.create(image=ImageFile(open(TEST_IMAGE_PATH, 'rb'))) for size in settings.IMAGE_SIZES.keys(): - Thumbnail.objects.get_or_create_at_size(image.pk, size) + Thumbnail.objects.get_or_create_at_size(image, size) return image diff --git a/django_images/models.py b/django_images/models.py index 6786af3..ef33923 100644 --- a/django_images/models.py +++ b/django_images/models.py @@ -67,8 +67,7 @@ class Image(models.Model): class ThumbnailManager(models.Manager): - def get_or_create_at_size(self, image_id, size): - image = Image.objects.get(id=image_id) + def get_or_create_at_size(self, image, size): if size not in IMAGE_SIZES: raise ValueError("Received unknown size: %s" % size) try: diff --git a/django_images/tests.py b/django_images/tests.py index 90d824e..6838864 100644 --- a/django_images/tests.py +++ b/django_images/tests.py @@ -20,7 +20,7 @@ class ImageModelTest(TestCase): def test_get_by_size(self): size = list(settings.IMAGE_SIZES.keys())[0] - Thumbnail.objects.get_or_create_at_size(self.image.id, size) + Thumbnail.objects.get_or_create_at_size(self.image, size) self.image.get_by_size(size) def test_get_absolute_url(self): @@ -28,7 +28,7 @@ class ImageModelTest(TestCase): self.assertEqual(url, self.image.image.url) # For thumbnail size = list(settings.IMAGE_SIZES.keys())[0] - thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) + thumb = Thumbnail.objects.get_or_create_at_size(self.image, size) url = self.image.get_absolute_url(size) self.assertEqual(url, thumb.image.url) # Fallback on creation url @@ -49,16 +49,16 @@ class ThumbnailManagerModelTest(TestCase): def test_unknown_size(self): self.assertRaises(ValueError, Thumbnail.objects.get_or_create_at_size, - self.image.id, 'foo') + self.image, 'foo') # TODO: Test the image object and data def test_create(self): - Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) + Thumbnail.objects.get_or_create_at_size(self.image, self.size) self.assertEqual(self.image.thumbnail_set.count(), 1) def test_get(self): - thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) - thumb2 = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) + thumb = Thumbnail.objects.get_or_create_at_size(self.image, self.size) + thumb2 = Thumbnail.objects.get_or_create_at_size(self.image, self.size) self.assertEqual(thumb.id, thumb2.id) @@ -70,7 +70,7 @@ class ThumbnailModelTest(TestCase): self.image = Image.objects.create(width=370, height=370, image=ImageFile(image_obj, '01.png')) size = list(settings.IMAGE_SIZES.keys())[0] - self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) + self.thumb = Thumbnail.objects.get_or_create_at_size(self.image, size) def test_get_absolute_url(self): url = self.thumb.get_absolute_url() @@ -85,11 +85,11 @@ class PostSaveSignalOriginalChangedTestCase(TestCase): self.image = Image.objects.create(width=370, height=370, image=ImageFile(image_obj, '01.png')) size = list(settings.IMAGE_SIZES.keys())[0] - self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) + self.thumb = Thumbnail.objects.get_or_create_at_size(self.image, size) def test_post_save_signal_original_changed(self): size = list(settings.IMAGE_SIZES.keys())[0] - Thumbnail.objects.get_or_create_at_size(self.image.id, size) + Thumbnail.objects.get_or_create_at_size(self.image, size) self.image.delete() self.assertFalse(Thumbnail.objects.exists()) @@ -102,7 +102,7 @@ class PostDeleteSignalDeleteImageFileTest(TestCase): self.image = Image.objects.create(width=370, height=370, image=ImageFile(image_obj, '01.png')) size = list(settings.IMAGE_SIZES.keys())[0] - self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) + self.thumb = Thumbnail.objects.get_or_create_at_size(self.image, size) @mock.patch('django_images.models.IMAGE_AUTO_DELETE', True) def test_post_delete_signal_delete_image_files_enabled(self): @@ -136,7 +136,7 @@ class AtSizeTemplateTagTest(TestCase): self.image = Image.objects.create(width=370, height=370, image=ImageFile(image_obj, '01.png')) size = list(settings.IMAGE_SIZES.keys())[0] - self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size) + self.thumb = Thumbnail.objects.get_or_create_at_size(self.image, size) def test_at_size(self): size = list(settings.IMAGE_SIZES.keys())[0] @@ -152,7 +152,7 @@ class ThumbnailViewTest(TestCase): self.image = Image.objects.create(width=370, height=370, image=ImageFile(image_obj, '01.png')) self.size = list(settings.IMAGE_SIZES.keys())[0] - self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size) + self.thumb = Thumbnail.objects.get_or_create_at_size(self.image, self.size) def test_redirect(self): url = reverse('image-thumbnail', args=[self.image.id, self.size]) diff --git a/django_images/views.py b/django_images/views.py index ff15b26..d4ac9b9 100644 --- a/django_images/views.py +++ b/django_images/views.py @@ -1,14 +1,13 @@ from django.http import HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect -from . import models +from .models import Image, Thumbnail from .settings import IMAGE_SIZES def thumbnail(request, image_id, size): - image = get_object_or_404(models.Image, id=image_id) + image = get_object_or_404(Image, id=image_id) if size not in IMAGE_SIZES: return HttpResponseNotFound() - return redirect(models.Thumbnail.objects.get_or_create_at_size(image.id, - size)) + return redirect(Thumbnail.objects.get_or_create_at_size(image, size))