From bbc0bffd886ab840f21e7cf235841ac84f6b940f Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 3 Nov 2011 12:26:23 -0400 Subject: [PATCH 1/3] Fixes error message Arguments were in the wrong order. Also, shows class name in string representation of instance isn't helpful. --- imagekit/admin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/imagekit/admin.py b/imagekit/admin.py index cc24d29..94d9cf5 100644 --- a/imagekit/admin.py +++ b/imagekit/admin.py @@ -24,8 +24,8 @@ class AdminThumbnail(object): thumbnail = getattr(obj, self.image_field, None) if not thumbnail: - raise Exception('The property {0} is not defined on {1}.'.format( - obj, self.image_field)) + raise Exception('The property %s is not defined on %s.' % \ + (self.image_field, obj.__class__.__name__)) original_image = getattr(thumbnail, 'source_file', None) or thumbnail template = self.template or 'imagekit/admin/thumbnail.html' From 3022eb037d7bb18b0f1b44a0bd38dc8e0dd21382 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 3 Nov 2011 12:31:04 -0400 Subject: [PATCH 2/3] Proper handling of empty images and missing fields Fixes #42 --- imagekit/admin.py | 6 +++--- imagekit/templates/imagekit/admin/thumbnail.html | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/imagekit/admin.py b/imagekit/admin.py index 94d9cf5..ebab76c 100644 --- a/imagekit/admin.py +++ b/imagekit/admin.py @@ -21,9 +21,9 @@ class AdminThumbnail(object): self.template = template def __call__(self, obj): - thumbnail = getattr(obj, self.image_field, None) - - if not thumbnail: + try: + thumbnail = getattr(obj, self.image_field) + except AttributeError: raise Exception('The property %s is not defined on %s.' % \ (self.image_field, obj.__class__.__name__)) diff --git a/imagekit/templates/imagekit/admin/thumbnail.html b/imagekit/templates/imagekit/admin/thumbnail.html index 6531391..adaa89f 100644 --- a/imagekit/templates/imagekit/admin/thumbnail.html +++ b/imagekit/templates/imagekit/admin/thumbnail.html @@ -1,3 +1,5 @@ - - - \ No newline at end of file +{% if thumbnail %} + + + +{% endif %} \ No newline at end of file From dd642fd05bae204989e8a2c74be506b28dd228b0 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 3 Nov 2011 15:00:40 -0400 Subject: [PATCH 3/3] `format_to_extension` correctly handles `None` arg --- imagekit/utils.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/imagekit/utils.py b/imagekit/utils.py index b6ca548..07cd532 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -97,9 +97,10 @@ def _extension_to_format(extension): def _format_to_extension(format): - for k, v in Image.EXTENSION.iteritems(): - if v == format.upper(): - return k + if format: + for k, v in Image.EXTENSION.iteritems(): + if v == format.upper(): + return k return None @@ -121,11 +122,13 @@ def format_to_extension(format): """Returns the first extension that matches the provided format. """ - extension = _format_to_extension(format) - if not extension and _preinit_pil(): - extension = _format_to_extension(format) - if not extension and _init_pil(): + extension = None + if format: extension = _format_to_extension(format) + if not extension and _preinit_pil(): + extension = _format_to_extension(format) + if not extension and _init_pil(): + extension = _format_to_extension(format) if not extension: raise UnknownFormatError(format) return extension