Skip to content

Commit

Permalink
Naming & implementation of bound fields are more consistent.
Browse files Browse the repository at this point in the history
Bound fields are now named as such (BoundBlah), extend their unbound
counterparts, and their constructors accept an unbound instance.
  • Loading branch information
matthewwithanm committed Sep 21, 2011
1 parent a71b3ca commit 2770be2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
40 changes: 22 additions & 18 deletions imagekit/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@
from django.template.loader import render_to_string


class BoundAdminThumbnailView(object):
class AdminThumbnailView(object):
short_description = _('Thumbnail')
allow_tags = True

def __init__(self, model_instance, image_field, template=None):
self.model_instance = model_instance
def __init__(self, image_field, template=None):
"""
Keyword arguments:
image_field -- the name of the ImageField or ImageSpec on the model to
use for the thumbnail.
template -- the template with which to render the thumbnail
"""
self.image_field = image_field
self.template = template

def __get__(self, obj, type=None):
return BoundAdminThumbnailView(obj, self)


class BoundAdminThumbnailView(AdminThumbnailView):
def __init__(self, model_instance, unbound_field):
super(BoundAdminThumbnailView, self).__init__(unbound_field.image_field,
unbound_field.template)
self.model_instance = model_instance

def __unicode__(self):
thumbnail = getattr(self.model_instance, self.image_field, None)

Expand All @@ -26,19 +42,7 @@ def __unicode__(self):
'thumbnail': thumbnail,
'original_image': original_image,
})


class AdminThumbnailView(object):
def __init__(self, image_field, template=None):
"""
Keyword arguments:
image_field -- the name of the ImageField or ImageSpec on the model to
use for the thumbnail.
template -- the template with which to render the thumbnail
"""
self.image_field = image_field
self.template = template


def __get__(self, obj, type=None):
return BoundAdminThumbnailView(obj, self.image_field, self.template)
"""Override AdminThumbnailView's implementation."""
return self
42 changes: 24 additions & 18 deletions imagekit/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ def process(self, image, obj):
return img, format

def contribute_to_class(self, cls, name):
setattr(cls, name, Descriptor(self, name))


class Accessor(object):
def __init__(self, obj, spec, property_name):
setattr(cls, name, _ImageSpecDescriptor(self, name))


class BoundImageSpec(ImageSpec):
def __init__(self, obj, unbound_field, property_name):
super(BoundImageSpec, self).__init__(unbound_field.processors,
image_field=unbound_field.image_field,
pre_cache=unbound_field.pre_cache,
quality=unbound_field.quality,
increment_count=unbound_field.increment_count,
storage=unbound_field.storage, format=unbound_field.format,
cache_to=unbound_field.cache_to)
self._img = None
self._fmt = None
self._obj = obj
self.spec = spec
self.property_name = property_name

@property
Expand All @@ -81,7 +87,7 @@ def _format(self):
the `name` property).
"""
format = self.spec.format
format = self.format
if not format:
# Get the real (not suggested) extension.
extension = os.path.splitext(self.name)[1].lower()
Expand All @@ -95,13 +101,13 @@ def _get_imgfile(self):
imgfile = img_to_fobj(self._img, format)
else:
imgfile = img_to_fobj(self._img, format,
quality=int(self.spec.quality),
quality=int(self.quality),
optimize=True)
return imgfile

@property
def _imgfield(self):
return self.spec._get_imgfield(self._obj)
return self._get_imgfield(self._obj)

def _create(self):
if self._imgfield:
Expand All @@ -114,7 +120,7 @@ def _create(self):
return
fp.seek(0)
fp = StringIO(fp.read())
self._img, self._fmt = self.spec.process(Image.open(fp), self._obj)
self._img, self._fmt = self.process(Image.open(fp), self._obj)
# save the new image to the cache
content = ContentFile(self._get_imgfile().read())
self._storage.save(self.name, content)
Expand All @@ -132,10 +138,10 @@ def _exists(self):

@property
def _suggested_extension(self):
if self.spec.format:
if self.format:
# Try to look up an extension by the format
extensions = [k.lstrip('.') for k, v in Image.EXTENSION.iteritems() \
if v == self.spec.format.upper()]
if v == self.format.upper()]
else:
extensions = []
original_extension = os.path.splitext(self._imgfield.name)[1].lstrip('.')
Expand All @@ -155,7 +161,7 @@ def name(self):
"""
filename = self._imgfield.name
if filename:
cache_to = getattr(self.spec, 'cache_to', None) or \
cache_to = self.cache_to or \
getattr(self._obj._ik, 'default_cache_to', None)

if not cache_to:
Expand All @@ -173,15 +179,15 @@ def name(self):

@property
def _storage(self):
return getattr(self.spec, 'storage', None) or \
return self.storage or \
getattr(self._obj._ik, 'default_storage', None) or \
self._imgfield.storage

@property
def url(self):
if not self.spec.pre_cache:
if not self.pre_cache:
self._create()
if self.spec.increment_count:
if self.increment_count:
fieldname = self._obj._ik.save_count_as
if fieldname is not None:
current_count = getattr(self._obj, fieldname)
Expand Down Expand Up @@ -211,10 +217,10 @@ def height(self):
return self.image.size[1]


class Descriptor(object):
class _ImageSpecDescriptor(object):
def __init__(self, spec, property_name):
self._property_name = property_name
self._spec = spec

def __get__(self, obj, type=None):
return Accessor(obj, self._spec, self._property_name)
return BoundImageSpec(obj, self._spec, self._property_name)

0 comments on commit 2770be2

Please sign in to comment.