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.
pull/753/head
Karl Hobley 2014-10-26 15:52:16 +00:00 zatwierdzone przez Karl Hobley
rodzic 34579b6d43
commit fbb061e2a0
4 zmienionych plików z 57 dodań i 8 usunięć

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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