diff --git a/imagekit/templatetags/imagekit.py b/imagekit/templatetags/imagekit.py index f9e5806e..b2bbfe72 100644 --- a/imagekit/templatetags/imagekit.py +++ b/imagekit/templatetags/imagekit.py @@ -92,6 +92,11 @@ def render(self, context): kwargs = {k: v.resolve(context) for k, v in self._generator_kwargs.items()} kwargs['source'] = self._source.resolve(context) kwargs.update(parse_dimensions(self._dimensions.resolve(context))) + if kwargs.get('anchor'): + # ImageKit uses pickle at protocol 0, which throws infinite + # recursion errors when anchor is set to a SafeString instance. + # This converts the SafeString into a str instance. + kwargs['anchor'] = kwargs['anchor'][:] generator = generator_registry.get(generator_id, **kwargs) context[variable_name] = ImageCacheFile(generator) @@ -114,6 +119,11 @@ def render(self, context): kwargs = {k: v.resolve(context) for k, v in self._generator_kwargs.items()} kwargs['source'] = self._source.resolve(context) kwargs.update(dimensions) + if kwargs.get('anchor'): + # ImageKit uses pickle at protocol 0, which throws infinite + # recursion errors when anchor is set to a SafeString instance. + # This converts the SafeString into a str instance. + kwargs['anchor'] = kwargs['anchor'][:] generator = generator_registry.get(generator_id, **kwargs) file = ImageCacheFile(generator) diff --git a/tests/test_thumbnail_tag.py b/tests/test_thumbnail_tag.py index 9c80fe13..f3bdfda6 100644 --- a/tests/test_thumbnail_tag.py +++ b/tests/test_thumbnail_tag.py @@ -15,6 +15,16 @@ def test_img_tag(): assert attrs[k].strip() != '' +def test_img_tag_anchor(): + ttag = r"""{% thumbnail '100x100' img anchor='c' %}""" + clear_imagekit_cache() + attrs = get_html_attrs(ttag) + expected_attrs = {'src', 'width', 'height'} + assert set(attrs.keys()) == expected_attrs + for k in expected_attrs: + assert attrs[k].strip() != '' + + def test_img_tag_attrs(): ttag = r"""{% thumbnail '100x100' img -- alt="Hello" %}""" clear_imagekit_cache() @@ -58,6 +68,13 @@ def test_assignment_tag(): assert html != '' +def test_assignment_tag_anchor(): + ttag = r"""{% thumbnail '100x100' img anchor='c' as th %}{{ th.url }}""" + clear_imagekit_cache() + html = render_tag(ttag) + assert html != '' + + def test_single_dimension(): ttag = r"""{% thumbnail '100x' img as th %}{{ th.width }}""" clear_imagekit_cache()