Added default_image_field

This works kind of like Django's models' _default_manager. If your specs don't
specify an image_field, and your IKOptions don't specify a default_image_field,
the first ImageField your model defines will be used.
This commit is contained in:
Matthew Tretter 2011-09-10 00:25:27 -04:00
parent 4c78f2d24c
commit 57a28091c5
3 changed files with 19 additions and 6 deletions

View file

@ -41,14 +41,18 @@ class ImageModelBase(ModelBase):
"""
def __init__(self, name, bases, attrs):
user_opts = getattr(self, 'IKOptions', None)
specs = []
default_image_field = getattr(user_opts, 'default_image_field', None)
for k, v in attrs.items():
if isinstance(v, ImageSpec):
setattr(self, k, Descriptor(v, k))
specs.append(v)
elif not default_image_field and isinstance(v, models.ImageField):
default_image_field = k
user_opts.specs = specs
user_opts.default_image_field = default_image_field
opts = Options(user_opts)
setattr(self, '_ik', opts)
ModelBase.__init__(self, name, bases, attrs)

View file

@ -10,8 +10,16 @@ class Options(object):
admin_thumbnail_property = 'admin_thumbnail'
"""The name of the spec to be used by the admin_thumbnail_view"""
image_field = 'image'
default_image_field = None
"""The name of the image field property on the model.
Can be overridden on a per-spec basis by setting the image_field property on
the spec. If you don't define default_image_field on your IKOptions class,
it will be automatically populated with the name of the first ImageField the
model defines.
"""
crop_horz_field = 'crop_horz'
crop_vert_field = 'crop_vert'
preprocessor_spec = None

View file

@ -15,7 +15,7 @@ from django.core.files.base import ContentFile
class ImageSpec(object):
image_field = 'original_image' # TODO: Get rid of this. It can be specified in a SpecDefaults nested class.
image_field = None
processors = []
pre_cache = False
quality = 70
@ -27,7 +27,8 @@ class ImageSpec(object):
self.__dict__.update(kwargs)
def _get_imgfield(self, obj):
return getattr(obj, self.image_field)
field_name = getattr(self, 'image_field', None) or obj._ik.default_image_field
return getattr(obj, field_name)
def process(self, image, obj):
fmt = image.format