mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-17 05:40:25 +00:00
I never liked that the "AdminThumbnailView" was supposed to be defined on the model, but never looked into it. This commit puts the definition back where it belongs: in the admin. Instead of requiring you to add a field (with view logic) to your model, you now just add a property to your admin class and specify that property in the `list_display` list.
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from django.utils.translation import ugettext_lazy as _
|
|
from django.template.loader import render_to_string
|
|
|
|
|
|
class AdminThumbnail(object):
|
|
"""A convenience utility for adding thumbnails to the Django admin change
|
|
list.
|
|
|
|
"""
|
|
short_description = _('Thumbnail')
|
|
allow_tags = True
|
|
|
|
def __init__(self, image_field, template=None):
|
|
"""
|
|
:param image_field: The name of the ImageField or ImageSpec on the model
|
|
to use for the thumbnail.
|
|
:param template: The template with which to render the thumbnail
|
|
|
|
"""
|
|
self.image_field = image_field
|
|
self.template = template
|
|
|
|
def __call__(self, obj):
|
|
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))
|
|
|
|
original_image = getattr(thumbnail, 'source_file', None) or thumbnail
|
|
template = self.template or 'imagekit/admin/thumbnail.html'
|
|
|
|
return render_to_string(template, {
|
|
'model': obj,
|
|
'thumbnail': thumbnail,
|
|
'original_image': original_image,
|
|
})
|