0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Backwards compatibility for 3-argument run methods

This commit is contained in:
Karl Hobley 2016-04-12 20:57:19 +01:00 committed by Matt Westcott
parent ee24881e71
commit 79348d427f
2 changed files with 59 additions and 3 deletions

View File

@ -1,7 +1,9 @@
from __future__ import absolute_import, unicode_literals
import hashlib
import inspect
import os.path
import warnings
from collections import OrderedDict
from contextlib import contextmanager
@ -25,6 +27,7 @@ from taggit.managers import TaggableManager
from unidecode import unidecode
from willow.image import Image as WillowImage
from wagtail.utils.deprecation import RemovedInWagtail19Warning
from wagtail.wagtailadmin.utils import get_object_usage
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import CollectionMember
@ -412,7 +415,25 @@ class Filter(models.Model):
env = {}
for operation in self.operations:
willow = operation.run(willow, image, env) or willow
# Check that the operation can take the new "env" argument
# (added in Wagtail 1.5)
try:
inspect.getcallargs(operation.run, willow, image, env)
accepts_env = True
except TypeError:
# Check that the paramters fit the old style, so we don't
# raise a warning if there is a coding error
inspect.getcallargs(operation.run, willow, image)
accepts_env = False
warnings.warn("ImageOperation run methods should take 4 "
"arguments. %d.run only takes 3.",
RemovedInWagtail19Warning)
# Call operation
if accepts_env:
willow = operation.run(willow, image, env) or willow
else:
willow = operation.run(willow, image) or willow
if original_format == 'jpeg':
# Allow changing of JPEG compression quality

View File

@ -1,9 +1,12 @@
from __future__ import absolute_import, unicode_literals
import warnings
from django.test import TestCase
from django.utils.six import BytesIO
from mock import Mock
from wagtail.utils.deprecation import RemovedInWagtail19Warning
from wagtail.wagtailcore import hooks
from wagtail.wagtailimages import image_operations
from wagtail.wagtailimages.exceptions import InvalidFilterSpecError
@ -394,7 +397,12 @@ class TestFilter(TestCase):
operation_instance = Mock()
def test_runs_operations(self):
self.operation_instance.run = Mock()
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(
@ -403,7 +411,34 @@ class TestFilter(TestCase):
)
fil.run(image, BytesIO())
self.assertEqual(self.operation_instance.run.call_count, 2)
self.assertEqual(run_mock.call_count, 2)
def test_runs_operations_without_env_argument(self):
# The "env" argument was added in Wagtial 1.5. This tests that
# image operations written for 1.4 will still work
run_mock = Mock()
def run(willow, image):
run_mock(willow, image)
self.operation_instance.run = run
fil = Filter(spec='operation1|operation2')
image = Image.objects.create(
title="Test image",
file=get_test_image_file(),
)
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter('always')
fil.run(image, BytesIO())
self.assertEqual(len(ws), 2)
self.assertIs(ws[0].category, RemovedInWagtail19Warning)
self.assertEqual(run_mock.call_count, 2)
@hooks.register('register_image_operations')