Переглянути джерело

Use caller's Image object when fetching or creating a thumbnail.

pull/154/head
Will Stott 5 роки тому
джерело
коміт
d871180cdf
7 змінених файлів з 20 додано та 22 видалено
  1. +1
    -1
      core/models.py
  2. +1
    -1
      core/serializers.py
  3. +1
    -1
      core/tests/api.py
  4. +1
    -1
      core/tests/helpers.py
  5. +1
    -2
      django_images/models.py
  6. +12
    -12
      django_images/tests.py
  7. +3
    -4
      django_images/views.py

+ 1
- 1
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




+ 1
- 1
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




+ 1
- 1
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




+ 1
- 1
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




+ 1
- 2
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:


+ 12
- 12
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])


+ 3
- 4
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))

Завантаження…
Відмінити
Зберегти