From 34579b6d433d639bc9660dfcf8ea739f369a3ae8 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 26 Oct 2014 15:27:39 +0000 Subject: [PATCH] Serving images to external sites uses renditions Previously, an image served to an external site was rendered on the fly. This may have performance issues with large websites. This commit changes the image serve view to use renditions so images are only rendered when the url is first accessed. When the user hits the url, they are now given a permanent redirect to the renditions image. --- wagtail/tests/settings.py | 1 + wagtail/wagtailimages/tests/tests.py | 8 +------- wagtail/wagtailimages/views/frontend.py | 7 +++---- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/wagtail/tests/settings.py b/wagtail/tests/settings.py index 4e7eaca6cb..1b4cb8d772 100644 --- a/wagtail/tests/settings.py +++ b/wagtail/tests/settings.py @@ -7,6 +7,7 @@ from django.conf import global_settings WAGTAIL_ROOT = os.path.dirname(__file__) STATIC_ROOT = os.path.join(WAGTAIL_ROOT, 'test-static') MEDIA_ROOT = os.path.join(WAGTAIL_ROOT, 'test-media') +MEDIA_URL = '/media/' DATABASES = { diff --git a/wagtail/wagtailimages/tests/tests.py b/wagtail/wagtailimages/tests/tests.py index 8dca205970..652f2c703e 100644 --- a/wagtail/wagtailimages/tests/tests.py +++ b/wagtail/wagtailimages/tests/tests.py @@ -140,13 +140,7 @@ class TestFrontendServeView(TestCase): response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600'))) # Check response - self.assertEqual(response.status_code, 200) - self.assertEqual(response['Content-Type'], 'image/jpeg') - - # Make sure the cache headers are set to expire after at least one month - self.assertIn('Cache-Control', response) - self.assertEqual(response['Cache-Control'].split('=')[0], 'max-age') - self.assertTrue(int(response['Cache-Control'].split('=')[1]) > datetime.timedelta(days=30).seconds) + self.assertRedirects(response, self.image.renditions.first().url, status_code=301, target_status_code=404) def test_get_invalid_signature(self): """ diff --git a/wagtail/wagtailimages/views/frontend.py b/wagtail/wagtailimages/views/frontend.py index 0ad2534d36..7c792a1373 100644 --- a/wagtail/wagtailimages/views/frontend.py +++ b/wagtail/wagtailimages/views/frontend.py @@ -1,13 +1,11 @@ -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect from django.http import HttpResponse from django.core.exceptions import PermissionDenied -from django.views.decorators.cache import cache_control from wagtail.wagtailimages.models import get_image_model, Filter from wagtail.wagtailimages.utils import verify_signature -@cache_control(max_age=60*60*24*60) # Cache for 60 days def serve(request, signature, image_id, filter_spec): image = get_object_or_404(get_image_model(), id=image_id) @@ -15,6 +13,7 @@ def serve(request, signature, image_id, filter_spec): raise PermissionDenied try: - return Filter(spec=filter_spec).process_image(image.file.file, HttpResponse(content_type='image/jpeg'), focal_point=image.get_focal_point()) + rendition = image.get_rendition(filter_spec) + return redirect(rendition.url, permanent=True) except Filter.InvalidFilterSpecError: return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400)