Make generate_signature return a string, so we can pass it to reverse() again

pull/4101/merge
Matt Westcott 2017-12-03 00:10:38 +00:00 zatwierdzone przez Karl Hobley
rodzic ed3cc5587c
commit 9bf8735e0e
4 zmienionych plików z 14 dodań i 7 usunięć

Wyświetl plik

@ -126,3 +126,9 @@ Removed support for Elasticsearch 1.x
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Elasticsearch 1.x is no longer supported in this release. Please upgrade to a 2.x or 5.x release of Elasticsearch before upgrading to Wagtail 2.0.
``wagtail.images.views.serve.generate_signature`` now returns a string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``generate_signature`` function in ``wagtail.images.views.serve``, used to build URLs for the :ref:`dynamic image serve view <using_images_outside_wagtail>`, now returns a string rather than a binary string. This ensures that any existing user code that builds up the final image URL with ``reverse`` will continue to work on Django 2.0 (which no longer allows binary strings to be passed to ``reverse``). Any code that expects a binary string as the return value of ``generate_string`` - for example, calling ``decode()`` on the result - will need to be updated. (Apps that need to preserve compatibility with earlier versions of Wagtail can call ``django.utils.encoding.force_text`` instead of ``decode``.)

Wyświetl plik

@ -1065,7 +1065,7 @@ class TestGenerateURLView(TestCase, WagtailTestUtils):
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(), safe=urlquote_safechars),
'signature': urlquote(generate_signature(self.image.id, 'fill-800x600'), safe=urlquote_safechars),
'image_id': self.image.id,
}
self.assertEqual(content_json['url'], expected_url)

Wyświetl plik

@ -225,16 +225,16 @@ class TestFormat(TestCase):
class TestSignatureGeneration(TestCase):
def test_signature_generation(self):
self.assertEqual(generate_signature(100, 'fill-800x600'), b'xnZOzQyUg6pkfciqcfRJRosOrGg=')
self.assertEqual(generate_signature(100, 'fill-800x600'), 'xnZOzQyUg6pkfciqcfRJRosOrGg=')
def test_signature_verification(self):
self.assertTrue(verify_signature(b'xnZOzQyUg6pkfciqcfRJRosOrGg=', 100, 'fill-800x600'))
self.assertTrue(verify_signature('xnZOzQyUg6pkfciqcfRJRosOrGg=', 100, 'fill-800x600'))
def test_signature_changes_on_image_id(self):
self.assertFalse(verify_signature(b'xnZOzQyUg6pkfciqcfRJRosOrGg=', 200, 'fill-800x600'))
self.assertFalse(verify_signature('xnZOzQyUg6pkfciqcfRJRosOrGg=', 200, 'fill-800x600'))
def test_signature_changes_on_filter_spec(self):
self.assertFalse(verify_signature(b'xnZOzQyUg6pkfciqcfRJRosOrGg=', 100, 'fill-800x700'))
self.assertFalse(verify_signature('xnZOzQyUg6pkfciqcfRJRosOrGg=', 100, 'fill-800x700'))
class TestFrontendServeView(TestCase):

Wyświetl plik

@ -9,6 +9,7 @@ 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.encoding import force_text
from django.views.generic import View
from wagtail.utils.sendfile import sendfile
@ -28,11 +29,11 @@ def generate_signature(image_id, filter_spec, key=None):
# Based on libthumbor hmac generation
# https://github.com/thumbor/libthumbor/blob/b19dc58cf84787e08c8e397ab322e86268bb4345/libthumbor/crypto.py#L50
url = '{}/{}/'.format(image_id, filter_spec)
return base64.urlsafe_b64encode(hmac.new(key, url.encode(), hashlib.sha1).digest())
return force_text(base64.urlsafe_b64encode(hmac.new(key, url.encode(), hashlib.sha1).digest()))
def verify_signature(signature, image_id, filter_spec, key=None):
return signature == generate_signature(image_id, filter_spec, key=key)
return force_text(signature) == generate_signature(image_id, filter_spec, key=key)
class ServeView(View):