django-imagekit/imagekit/fields.py
Matthew Tretter 544d5b874a Added AdminThumbnailView field.
You're no longer restricted to just one, special-case admin thumbnail. Make as
many as you want by adding AdminThumbnailView properties to your model and
including them in your admin class's `list_display` tuple. You can also provide
a custom template. Note that (because this change introduces templates to
imagekit), imagekit is now required in INSTALLED_APPS.

Ideally we could get this stuff out of the model, but we'll have to look into
whether that's possible without making things really complicated.
2011-09-20 19:37:04 -04:00

44 lines
1.5 KiB
Python
Executable file

from django.utils.translation import ugettext_lazy as _
from django.template.loader import render_to_string
class BoundAdminThumbnailView(object):
short_description = _('Thumbnail')
allow_tags = True
def __init__(self, model_instance, image_field, template=None):
self.model_instance = model_instance
self.image_field = image_field
self.template = template
def __unicode__(self):
thumbnail = getattr(self.model_instance, self.image_field, None)
if not thumbnail:
raise Exception('The property {0} is not defined on {1}.'.format(
self.model_instance, self.image_field))
original_image = getattr(thumbnail, '_imgfield', None) or thumbnail
template = self.template or 'imagekit/admin/thumbnail.html'
return render_to_string(template, {
'model': self.model_instance,
'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)