From fbb061e2a03cff002e1721a1ab573cfa550961cd Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 26 Oct 2014 15:52:16 +0000 Subject: [PATCH] Added preview view to admin As the frontend serve view now uses renditions, previews are now generated with their own view to prevent loads of unneeded renditions being created. --- wagtail/wagtailimages/admin_urls.py | 1 + .../wagtailimages/js/image-url-generator.js | 2 +- .../wagtailimages/tests/test_admin_views.py | 47 ++++++++++++++++--- wagtail/wagtailimages/views/images.py | 15 +++++- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/wagtail/wagtailimages/admin_urls.py b/wagtail/wagtailimages/admin_urls.py index 44bc61224c..a095f9431f 100644 --- a/wagtail/wagtailimages/admin_urls.py +++ b/wagtail/wagtailimages/admin_urls.py @@ -9,6 +9,7 @@ urlpatterns = [ url(r'^(\d+)/delete/$', images.delete, name='wagtailimages_delete_image'), url(r'^(\d+)/generate_url/$', images.url_generator, name='wagtailimages_url_generator'), url(r'^(\d+)/generate_url/(.*)/$', images.generate_url, name='wagtailimages_generate_url'), + url(r'^(\d+)/preview/(.*)/$', images.preview, name='wagtailimages_preview'), url(r'^add/$', images.add, name='wagtailimages_add_image'), url(r'^usage/(\d+)/$', images.usage, name='wagtailimages_image_usage'), diff --git a/wagtail/wagtailimages/static/wagtailimages/js/image-url-generator.js b/wagtail/wagtailimages/static/wagtailimages/js/image-url-generator.js index 86bf41fcdc..bc78fd5604 100644 --- a/wagtail/wagtailimages/static/wagtailimages/js/image-url-generator.js +++ b/wagtail/wagtailimages/static/wagtailimages/js/image-url-generator.js @@ -58,7 +58,7 @@ $(function() { $.getJSON(generatorUrl.replace('__filterspec__', filterSpec)) .done(function(data) { $result.val(data['url']); - $preview.attr('src', data['local_url']); + $preview.attr('src', data['preview_url']); $loadingMask.removeClass('loading'); }) .fail(function(data) { diff --git a/wagtail/wagtailimages/tests/test_admin_views.py b/wagtail/wagtailimages/tests/test_admin_views.py index 8225027031..1e26a286fd 100644 --- a/wagtail/wagtailimages/tests/test_admin_views.py +++ b/wagtail/wagtailimages/tests/test_admin_views.py @@ -517,7 +517,7 @@ class TestGenerateURLView(TestCase, WagtailTestUtils): # Check JSON content_json = json.loads(response.content.decode()) - self.assertEqual(set(content_json.keys()), set(['url', 'local_url'])) + self.assertEqual(set(content_json.keys()), set(['url', 'preview_url'])) expected_url = 'http://localhost/images/%(signature)s/%(image_id)d/fill-800x600/' % { 'signature': urlquote(generate_signature(self.image.id, 'fill-800x600').decode()), @@ -525,11 +525,8 @@ class TestGenerateURLView(TestCase, WagtailTestUtils): } self.assertEqual(content_json['url'], expected_url) - expected_local_url = '/images/%(signature)s/%(image_id)d/fill-800x600/' % { - 'signature': urlquote(generate_signature(self.image.id, 'fill-800x600').decode()), - 'image_id': self.image.id, - } - self.assertEqual(content_json['local_url'], expected_local_url) + expected_preview_url = reverse('wagtailimages_preview', args=(self.image.id, 'fill-800x600')) + self.assertEqual(content_json['preview_url'], expected_preview_url) def test_get_bad_permissions(self): """ @@ -585,3 +582,41 @@ class TestGenerateURLView(TestCase, WagtailTestUtils): self.assertJSONEqual(response.content.decode(), json.dumps({ 'error': 'Invalid filter spec.', })) + + +class TestPreviewView(TestCase, WagtailTestUtils): + def setUp(self): + # Create an image for running tests on + self.image = Image.objects.create( + title="Test image", + file=get_test_image_file(), + ) + + # Login + self.user = self.login() + + def test_get(self): + """ + Test a valid GET request to the view + """ + # Get the image + response = self.client.get(reverse('wagtailimages_preview', args=(self.image.id, 'fill-800x600'))) + + # Check response + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'image/jpeg') + + def test_get_invalid_filter_spec(self): + """ + Test that an invalid filter spec returns a 400 response + + This is very unlikely to happen in reality. A user would have + to create signature for the invalid filter spec which can't be + done with Wagtails built in URL generator. We should test it + anyway though. + """ + # Get the image + response = self.client.get(reverse('wagtailimages_preview', args=(self.image.id, 'bad-filter-spec'))) + + # Check response + self.assertEqual(response.status_code, 400) diff --git a/wagtail/wagtailimages/views/images.py b/wagtail/wagtailimages/views/images.py index 4ef3ecf537..8b658db96d 100644 --- a/wagtail/wagtailimages/views/images.py +++ b/wagtail/wagtailimages/views/images.py @@ -180,7 +180,20 @@ def generate_url(request, image_id, filter_spec): except Site.DoesNotExist: site_root_url = Site.objects.first().root_url - return json_response({'url': site_root_url + url, 'local_url': url}, status=200) + # Generate preview url + preview_url = reverse('wagtailimages_preview', args=(image_id, filter_spec)) + + return json_response({'url': site_root_url + url, 'preview_url': preview_url}, status=200) + + +@permission_required('wagtailadmin.access_admin') +def preview(request, image_id, filter_spec): + image = get_object_or_404(get_image_model(), id=image_id) + + try: + return Filter(spec=filter_spec).process_image(image.file.file, HttpResponse(content_type='image/jpeg'), focal_point=image.get_focal_point()) + except Filter.InvalidFilterSpecError: + return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400) @permission_required('wagtailadmin.access_admin') # more specific permission tests are applied within the view