From d86ec082f1e6f3be36d7e9a6132c5d02c43abbed Mon Sep 17 00:00:00 2001 From: Venelin Stoykov Date: Sun, 17 Jul 2016 05:08:01 +0300 Subject: [PATCH] Fixed #350: Error when trying to access width/height after url If the file is closed and something is calling `open` now the file will be opened correctly event if it was already closed --- imagekit/files.py | 13 ++++++++++++- tests/test_generateimage_tag.py | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/imagekit/files.py b/imagekit/files.py index e717888..84fa544 100644 --- a/imagekit/files.py +++ b/imagekit/files.py @@ -56,7 +56,18 @@ class BaseIKFile(File): def open(self, mode='rb'): self._require_file() - self.file.open(mode) + try: + self.file.open(mode) + except ValueError: + # if the underlaying file can't be reopened + # then we will use the storage to try to open it again + if self.file.closed: + # clear cached file instance + del self.file + # Because file is a property we can acces it after + # we deleted it + return self.file.open(mode) + raise def _get_closed(self): file = getattr(self, '_file', None) diff --git a/tests/test_generateimage_tag.py b/tests/test_generateimage_tag.py index 433c86f..db3f577 100644 --- a/tests/test_generateimage_tag.py +++ b/tests/test_generateimage_tag.py @@ -50,7 +50,7 @@ def test_single_dimension_attr(): def test_assignment_tag(): - ttag = r"""{% generateimage 'testspec' source=img as th %}{{ th.url }}""" + ttag = r"""{% generateimage 'testspec' source=img as th %}{{ th.url }}{{ th.height }}{{ th.width }}""" clear_imagekit_cache() html = render_tag(ttag) assert_not_equal(html.strip(), '')