コード例 #1
0
def generate_image_url(image, filter_spec='original'):
    """Return generated URL for image."""
    signature = generate_signature(image.id, filter_spec)
    url = reverse('wagtailimages_serve',
                  urlconf='wagtail.images.urls',
                  args=(signature, image.id, filter_spec))
    return url
コード例 #2
0
    def test_get_with_serve_action(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_serve_action_serve', args=(signature, self.image.id, 'fill-800x600')))

        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.streaming)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #3
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_with_serve_action(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_serve_action_serve', args=(signature, self.image.id, 'fill-800x600')))

        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.streaming)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #4
0
    def test_get(self):
        """
        This tests that the view responds correctly for a user with edit permissions on this image
        """
        # Get
        response = self.client.get(
            reverse('wagtailimages:generate_url',
                    args=(self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/json')

        # Check JSON
        content_json = json.loads(response.content.decode())

        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),
            'image_id':
            self.image.id,
        }
        self.assertEqual(content_json['url'], expected_url)

        expected_preview_url = reverse('wagtailimages:preview',
                                       args=(self.image.id, 'fill-800x600'))
        self.assertEqual(content_json['preview_url'], expected_preview_url)
コード例 #5
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_sendfile_dummy_backend(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_sendfile_dummy',
                                           args=(signature, self.image.id,
                                                 'fill-800x600')))

        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.content, 'Dummy backend response')
コード例 #6
0
ファイル: tests.py プロジェクト: melody40/monorepo
    def test_sendfile_nobackend(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(
            reverse('wagtailimages_sendfile',
                    args=(signature, self.image.id, 'fill-800x600')))

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #7
0
ファイル: tests.py プロジェクト: melody40/monorepo
    def test_sendfile_dummy_backend(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(
            reverse('wagtailimages_sendfile_dummy',
                    args=(signature, self.image.id, 'fill-800x600')))

        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.content, 'Dummy backend response')
コード例 #8
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_sendfile_nobackend(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_sendfile',
                                           args=(signature, self.image.id,
                                                 'fill-800x600')))

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #9
0
    def test_get_with_redirect_action(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_serve_action_redirect', args=(signature, self.image.id, 'fill-800x600')))

        expected_redirect_url = '/media/images/{filename[0]}.2e16d0ba.fill-800x600{filename[1]}'.format(
            filename=os.path.splitext(os.path.basename(self.image.file.path))
        )

        self.assertRedirects(response, expected_redirect_url, status_code=301, fetch_redirect_response=False)
コード例 #10
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_with_redirect_action(self):
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_serve_action_redirect', args=(signature, self.image.id, 'fill-800x600')))

        expected_redirect_url = '/media/images/{filename[0]}.2e16d0ba.fill-800x600{filename[1]}'.format(
            filename=os.path.splitext(os.path.basename(self.image.file.path))
        )

        self.assertRedirects(response, expected_redirect_url, status_code=301, fetch_redirect_response=False)
コード例 #11
0
ファイル: serializers.py プロジェクト: brudil/falmer
def generate_image_url(image, filter_spec):
    from wagtail.images.views.serve import generate_signature
    signature = generate_signature(image.id, filter_spec)
    url = reverse('wagtailimages_serve',
                  args=(signature, image.id, filter_spec))

    # Append image's original filename to the URL (optional)
    # url += image.file.name[len('original_images/'):]

    return settings.PUBLIC_HOST + url
コード例 #12
0
    def test_get_invalid_signature(self):
        """
        Test that an invalid signature returns a 403 response
        """
        # Generate a signature for the incorrect image id
        signature = generate_signature(self.image.id + 1, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 403)
コード例 #13
0
    def test_get_with_custom_key(self):
        """
        Test that that the key can be changed on the view
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600', key='custom')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve_custom_key', args=(signature, self.image.id, 'fill-800x600')) + 'test.png')

        # Check response
        self.assertEqual(response.status_code, 200)
コード例 #14
0
    def test_get_with_too_many_extra_components(self):
        """
        A filename can be appended to the end of the URL, but it must not contain a '/'
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')) + 'test/test.png')

        # URL pattern should not match
        self.assertEqual(response.status_code, 404)
コード例 #15
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_with_custom_key(self):
        """
        Test that that the key can be changed on the view
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600', key='custom')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve_custom_key', args=(signature, self.image.id, 'fill-800x600')) + 'test.png')

        # Check response
        self.assertEqual(response.status_code, 200)
コード例 #16
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_with_too_many_extra_components(self):
        """
        A filename can be appended to the end of the URL, but it must not contain a '/'
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')) + 'test/test.png')

        # URL pattern should not match
        self.assertEqual(response.status_code, 404)
コード例 #17
0
def generate_image_url(image, filter_spec):
    cache_key = image_url_cache_key(image.id, filter_spec)
    url = cache.get(cache_key)
    if url:
        return url
    from wagtail.images.views.serve import generate_signature
    signature = generate_signature(image.id, filter_spec)
    url = reverse('wagtailimages_serve',
                  args=(signature, image.id, filter_spec))
    url += image.file.name[len('original_images/'):]
    cache.set(cache_key, url)
    return url
コード例 #18
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_invalid_signature(self):
        """
        Test that an invalid signature returns a 403 response
        """
        # Generate a signature for the incorrect image id
        signature = generate_signature(self.image.id + 1, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 403)
コード例 #19
0
    def test_get(self):
        """
        Test a valid GET request to the view
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.streaming)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #20
0
    def test_get_with_extra_component(self):
        """
        Test that a filename can be optionally added to the end of the URL.
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')) + 'test.png')

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.streaming)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #21
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_with_extra_component(self):
        """
        Test that a filename can be optionally added to the end of the URL.
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')) + 'test.png')

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.streaming)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #22
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get(self):
        """
        Test a valid GET request to the view
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.streaming)
        self.assertEqual(response['Content-Type'], 'image/png')
コード例 #23
0
    def test_get_with_custom_key_using_default_key(self):
        """
        Test that that the key can be changed on the view

        This tests that the default key no longer works when the key is changed on the view
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve_custom_key', args=(signature, self.image.id, 'fill-800x600')) + 'test.png')

        # Check response
        self.assertEqual(response.status_code, 403)
コード例 #24
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_with_custom_key_using_default_key(self):
        """
        Test that that the key can be changed on the view

        This tests that the default key no longer works when the key is changed on the view
        """
        # Generate signature
        signature = generate_signature(self.image.id, 'fill-800x600')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve_custom_key', args=(signature, self.image.id, 'fill-800x600')) + 'test.png')

        # Check response
        self.assertEqual(response.status_code, 403)
コード例 #25
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    def test_get_missing_source_image_file(self):
        """
        Test that a missing image file gives a 410 response

        When the source image file is missing, it is presumed deleted so we
        return a 410 "Gone" response.
        """
        # Delete the image file
        os.remove(self.image.file.path)

        # Get the image
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 410)
コード例 #26
0
    def test_get_missing_source_image_file(self):
        """
        Test that a missing image file gives a 410 response

        When the source image file is missing, it is presumed deleted so we
        return a 410 "Gone" response.
        """
        # Delete the image file
        os.remove(self.image.file.path)

        # Get the image
        signature = generate_signature(self.image.id, 'fill-800x600')
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 410)
コード例 #27
0
ファイル: tests.py プロジェクト: sprymix/wagtail
    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.
        """
        # Generate a signature with the invalid filterspec
        signature = generate_signature(self.image.id, 'bad-filter-spec')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'bad-filter-spec')))

        # Check response
        self.assertEqual(response.status_code, 400)
コード例 #28
0
    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.
        """
        # Generate a signature with the invalid filterspec
        signature = generate_signature(self.image.id, 'bad-filter-spec')

        # Get the image
        response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'bad-filter-spec')))

        # Check response
        self.assertEqual(response.status_code, 400)
コード例 #29
0
def generate_url(request, image_id, filter_spec):
    # Get the image
    Image = get_image_model()
    try:
        image = Image.objects.get(id=image_id)
    except Image.DoesNotExist:
        return JsonResponse({'error': "Cannot find image."}, status=404)

    # Check if this user has edit permission on this image
    if not permission_policy.user_has_permission_for_instance(
            request.user, 'change', image):
        return JsonResponse(
            {
                'error':
                "You do not have permission to generate a URL for this image."
            },
            status=403)

    # Parse the filter spec to make sure its valid
    try:
        Filter(spec=filter_spec).operations
    except InvalidFilterSpecError:
        return JsonResponse({'error': "Invalid filter spec."}, status=400)

    # Generate url
    signature = generate_signature(image_id, filter_spec)
    url = reverse('wagtailimages_serve',
                  args=(signature, image_id, filter_spec))

    # Get site root url
    try:
        site_root_url = Site.objects.get(is_default_site=True).root_url
    except Site.DoesNotExist:
        site_root_url = Site.objects.first().root_url

    # Generate preview url
    preview_url = reverse('wagtailimages:preview',
                          args=(image_id, filter_spec))

    return JsonResponse(
        {
            'url': site_root_url + url,
            'preview_url': preview_url
        }, status=200)
コード例 #30
0
def generate_url(request, image_id, filter_spec):
    # Get the image
    Image = get_image_model()
    try:
        image = Image.objects.get(id=image_id)
    except Image.DoesNotExist:
        return JsonResponse({
            'error': "Cannot find image."
        }, status=404)

    # Check if this user has edit permission on this image
    if not permission_policy.user_has_permission_for_instance(request.user, 'change', image):
        return JsonResponse({
            'error': "You do not have permission to generate a URL for this image."
        }, status=403)

    # Parse the filter spec to make sure its valid
    try:
        Filter(spec=filter_spec).operations
    except InvalidFilterSpecError:
        return JsonResponse({
            'error': "Invalid filter spec."
        }, status=400)

    # Generate url
    signature = generate_signature(image_id, filter_spec)
    url = reverse('wagtailimages_serve', args=(signature, image_id, filter_spec))

    # Get site root url
    try:
        site_root_url = Site.objects.get(is_default_site=True).root_url
    except Site.DoesNotExist:
        site_root_url = Site.objects.first().root_url

    # Generate preview url
    preview_url = reverse('wagtailimages:preview', args=(image_id, filter_spec))

    return JsonResponse({'url': site_root_url + url, 'preview_url': preview_url}, status=200)
コード例 #31
0
ファイル: test_admin_views.py プロジェクト: gasman/wagtail
    def test_get(self):
        """
        This tests that the view responds correctly for a user with edit permissions on this image
        """
        # Get
        response = self.client.get(reverse('wagtailimages:generate_url', args=(self.image.id, 'fill-800x600')))

        # Check response
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/json')

        # Check JSON
        content_json = json.loads(response.content.decode())

        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'), safe=urlquote_safechars),
            'image_id': self.image.id,
        }
        self.assertEqual(content_json['url'], expected_url)

        expected_preview_url = reverse('wagtailimages:preview', args=(self.image.id, 'fill-800x600'))
        self.assertEqual(content_json['preview_url'], expected_preview_url)
コード例 #32
0
ファイル: tests.py プロジェクト: melody40/monorepo
 def test_signature_generation(self):
     self.assertEqual(generate_signature(100, 'fill-800x600'),
                      'xnZOzQyUg6pkfciqcfRJRosOrGg=')
コード例 #33
0
def generate_image_url(image, filter_spec):
    signature = generate_signature(image.id, filter_spec)
    url = reverse("wagtailimages_serve",
                  args=(signature, image.id, filter_spec))
    url += image.file.name[len("original_images/"):]
    return url
コード例 #34
0
ファイル: images.py プロジェクト: toryburgett/wagtail-graphql
def generate_image_url(image: wagtailImage, filter_spec: str) -> str:
    signature = generate_signature(image.pk, filter_spec)
    url = reverse('wagtailimages_serve',
                  args=(signature, image.pk, filter_spec))
    return url
コード例 #35
0
ファイル: tests.py プロジェクト: sprymix/wagtail
 def test_signature_generation(self):
     self.assertEqual(generate_signature(100, 'fill-800x600'), 'xnZOzQyUg6pkfciqcfRJRosOrGg=')