예제 #1
0
    def test_gif(self):
        fil = Filter(spec='width-400|format-gif')
        image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )
        out = fil.run(image, BytesIO())

        self.assertEqual(out.format_name, 'gif')
예제 #2
0
    def test_cache_key_fill_filter_with_focal_point(self):
        image = Image(
            width=1000,
            height=1000,
            focal_point_width=100,
            focal_point_height=100,
            focal_point_x=500,
            focal_point_y=500,
        )
        fil = Filter(spec='fill-100x100')
        cache_key = fil.get_cache_key(image)

        self.assertEqual(cache_key, '0bbe3b2f')
예제 #3
0
    def test_jpeg_quality_filter_overrides_setting(self):
        fil = Filter(spec='width-400|jpegquality-40')
        image = Image.objects.create(
            title="Test image",
            file=get_test_image_file_jpeg(),
        )

        f = BytesIO()
        with patch('PIL.Image.Image.save') as save:
            fil.run(image, f)

        save.assert_called_with(f,
                                'JPEG',
                                quality=40,
                                optimize=True,
                                progressive=True)
예제 #4
0
 def test_jpeg_quality_filter_too_big(self):
     fil = Filter(spec='width-400|jpegquality-101')
     image = Image.objects.create(
         title="Test image",
         file=get_test_image_file_jpeg(),
     )
     self.assertRaises(InvalidFilterSpecError, fil.run, image, BytesIO())
예제 #5
0
 def test_invalid(self):
     fil = Filter(spec='width-400|format-foo')
     image = Image.objects.create(
         title="Test image",
         file=get_test_image_file(),
     )
     self.assertRaises(InvalidFilterSpecError, fil.run, image, BytesIO())
예제 #6
0
    def test_runs_operations(self):
        run_mock = Mock()

        def run(willow, image, env):
            run_mock(willow, image, env)

        self.operation_instance.run = run

        fil = Filter(spec='operation1|operation2')
        image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )
        fil.run(image, BytesIO())

        self.assertEqual(run_mock.call_count, 2)
예제 #7
0
def preview(request, image_id, filter_spec):
    image = get_object_or_404(get_image_model(), id=image_id)

    try:
        response = HttpResponse()
        image = Filter(spec=filter_spec).run(image, response)
        response['Content-Type'] = 'image/' + image.format_name
        return response
    except InvalidFilterSpecError:
        return HttpResponse("Invalid filter spec: " + filter_spec,
                            content_type='text/plain',
                            status=400)
예제 #8
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('tuiuiuimages_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('tuiuiuimages:preview', args=(image_id, filter_spec))

    return JsonResponse(
        {
            'url': site_root_url + url,
            'preview_url': preview_url
        }, status=200)
예제 #9
0
 def filter(self):
     return Filter(spec=self.filter_spec)
예제 #10
0
    def test_cache_key_fill_filter(self):
        image = Image(width=1000, height=1000)
        fil = Filter(spec='fill-100x100')
        cache_key = fil.get_cache_key(image)

        self.assertEqual(cache_key, '2e16d0ba')
예제 #11
0
    def test_cache_key(self):
        image = Image(width=1000, height=1000)
        fil = Filter(spec='max-100x100')
        cache_key = fil.get_cache_key(image)

        self.assertEqual(cache_key, '')