0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Moved action validation to as_view method

This commit is contained in:
Karl Hobley 2016-01-26 14:26:02 +00:00
parent 231523d3f4
commit 430a3a2dae
3 changed files with 12 additions and 8 deletions

View File

@ -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):
"""

View File

@ -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'),
]

View File

@ -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()