diff --git a/wagtail/wagtailimages/tests/tests.py b/wagtail/wagtailimages/tests/tests.py index 2d01989bc9..d414a66d20 100644 --- a/wagtail/wagtailimages/tests/tests.py +++ b/wagtail/wagtailimages/tests/tests.py @@ -17,7 +17,7 @@ from wagtail.wagtailimages.formats import Format, get_image_format, register_ima from wagtail.wagtailimages.forms import get_image_form from wagtail.wagtailimages.models import Image as WagtailImage from wagtail.wagtailimages.rect import Rect, Vector -from wagtail.wagtailimages.views.serve import generate_signature, verify_signature +from wagtail.wagtailimages.views.serve import ServeView, generate_signature, verify_signature from .utils import Image, get_test_image_file @@ -273,11 +273,9 @@ class TestFrontendServeView(TestCase): self.assertRedirects(response, expected_redirect_url, status_code=301, fetch_redirect_response=False) - def test_get_with_unknown_action(self): - signature = generate_signature(self.image.id, 'fill-800x600') - + def test_init_with_unknown_action_raises_error(self): with self.assertRaises(ImproperlyConfigured): - self.client.get(reverse('wagtailimages_serve_action_unknown', args=(signature, self.image.id, 'fill-800x600'))) + ServeView.as_view(action='unknown') def test_get_with_custom_key(self): """ diff --git a/wagtail/wagtailimages/tests/urls.py b/wagtail/wagtailimages/tests/urls.py index cf781c8f1a..e8f82bbb0e 100644 --- a/wagtail/wagtailimages/tests/urls.py +++ b/wagtail/wagtailimages/tests/urls.py @@ -8,6 +8,5 @@ from wagtail.wagtailimages.views.serve import ServeView urlpatterns = [ url(r'^actions/serve/(.*)/(\d*)/(.*)/[^/]*', ServeView.as_view(action='serve'), name='wagtailimages_serve_action_serve'), url(r'^actions/redirect/(.*)/(\d*)/(.*)/[^/]*', ServeView.as_view(action='redirect'), name='wagtailimages_serve_action_redirect'), - url(r'^actions/unknown/(.*)/(\d*)/(.*)/[^/]*', ServeView.as_view(action='unknown'), name='wagtailimages_serve_action_unknown'), url(r'^custom_key/(.*)/(\d*)/(.*)/[^/]*', ServeView.as_view(key='custom'), name='wagtailimages_serve_custom_key'), ] diff --git a/wagtail/wagtailimages/views/serve.py b/wagtail/wagtailimages/views/serve.py index 82896a2932..6560c95042 100644 --- a/wagtail/wagtailimages/views/serve.py +++ b/wagtail/wagtailimages/views/serve.py @@ -10,6 +10,7 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.http import HttpResponse, HttpResponsePermanentRedirect, StreamingHttpResponse from django.shortcuts import get_object_or_404 +from django.utils.decorators import classonlymethod from django.utils.six import text_type from django.views.generic import View @@ -40,6 +41,14 @@ class ServeView(View): action = 'serve' key = None + @classonlymethod + def as_view(cls, **initkwargs): + if 'action' in initkwargs: + if initkwargs['action'] not in ['serve', 'redirect']: + raise ImproperlyConfigured("ServeView action must be either 'serve' or 'redirect'") + + return super(ServeView, cls).as_view(**initkwargs) + def get(self, request, signature, image_id, filter_spec): if not verify_signature(signature.encode(), image_id, filter_spec, key=self.key): raise PermissionDenied @@ -63,8 +72,6 @@ class ServeView(View): elif self.action == 'redirect': # Redirect to the file's public location return HttpResponsePermanentRedirect(rendition.url) - else: - raise ImproperlyConfigured("ServeView action must be either 'serve' or 'redirect'") serve = ServeView.as_view()