From 5e00de5204ff2f0a18db91f710bf1888a6e45057 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 8 Sep 2011 16:50:06 -0400 Subject: [PATCH] Admin thumbnails. --- imagekit/defaults.py | 3 +-- imagekit/models.py | 32 +++++++++++++++++--------------- imagekit/options.py | 5 ++++- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/imagekit/defaults.py b/imagekit/defaults.py index 4423357..bc197b3 100644 --- a/imagekit/defaults.py +++ b/imagekit/defaults.py @@ -20,5 +20,4 @@ class PNGFormat(processors.Format): extension = 'png' class DjangoAdminThumbnail(ImageSpec): - access_as = 'admin_thumbnail' - processors = [ResizeThumbnail, EnhanceSmall, SampleReflection, PNGFormat] + processors = [ResizeThumbnail(), EnhanceSmall(), SampleReflection(), PNGFormat()] diff --git a/imagekit/models.py b/imagekit/models.py index 4308540..382636b 100644 --- a/imagekit/models.py +++ b/imagekit/models.py @@ -12,6 +12,7 @@ from imagekit.specs import ImageSpec, Descriptor from imagekit.lib import * from imagekit.options import Options from imagekit.utils import img_to_fobj +from imagekit import defaults # Modify image file buffer size. ImageFile.MAXBLOCK = getattr(settings, 'PIL_IMAGEFILE_MAXBLOCK', 256 * 2 ** 10) @@ -39,18 +40,17 @@ class ImageModelBase(ModelBase): """ def __init__(self, name, bases, attrs): - if [b for b in bases if isinstance(b, ImageModelBase)]: - user_opts = getattr(self, 'IKOptions', None) - - specs = [] - for k, v in attrs.items(): - if isinstance(v, ImageSpec): - setattr(self, k, Descriptor(v, k)) - specs.append(v) - - user_opts.specs = specs - opts = Options(user_opts) - setattr(self, '_ik', opts) + user_opts = getattr(self, 'IKOptions', None) + + specs = [] + for k, v in attrs.items(): + if isinstance(v, ImageSpec): + setattr(self, k, Descriptor(v, k)) + specs.append(v) + + user_opts.specs = specs + opts = Options(user_opts) + setattr(self, '_ik', opts) ModelBase.__init__(self, name, bases, attrs) @@ -64,16 +64,18 @@ class ImageModel(models.Model): """ __metaclass__ = ImageModelBase + admin_thumbnail = defaults.DjangoAdminThumbnail() + class Meta: abstract = True def admin_thumbnail_view(self): if not self._imgfield: return None - prop = getattr(self, self._ik.admin_thumbnail_spec, None) + prop = getattr(self, self._ik.admin_thumbnail_property, None) if prop is None: - return 'An "%s" image spec has not been defined.' % \ - self._ik.admin_thumbnail_spec + return 'The property "%s" has not been defined.' % \ + self._ik.admin_thumbnail_property else: if hasattr(self, 'get_absolute_url'): return u'' % \ diff --git a/imagekit/options.py b/imagekit/options.py index 455eb41..d0deaea 100644 --- a/imagekit/options.py +++ b/imagekit/options.py @@ -7,6 +7,10 @@ class Options(object): """ Class handling per-model imagekit options """ + + admin_thumbnail_property = 'admin_thumbnail' + """The name of the spec to be used by the admin_thumbnail_view""" + image_field = 'image' crop_horz_field = 'crop_horz' crop_vert_field = 'crop_vert' @@ -15,7 +19,6 @@ class Options(object): save_count_as = None cache_filename_fields = ['pk', ] cache_filename_format = "%(filename)s_%(specname)s.%(extension)s" - admin_thumbnail_spec = 'admin_thumbnail' specs = None #storage = defaults to image_field.storage