Commits

Simon Meers committed edf1cde

Introducing ThumbnailAdminMixin.

  • Participants
  • Parent commits 8cc4f5f

Comments (0)

Files changed (1)

generic/admin/mixins.py

+import logging
+
 from django import forms
 from django import http
 from django.conf import settings
 
 from .widgets import ForeignKeyCookedIdWidget, ManyToManyCookedIdWidget
 
+logger = logging.getLogger(__name__)
+
 class CookedIdAdmin(admin.ModelAdmin):
     """
     Support for CookedIdWidgets (vs. RawIdWidgets) in admin.
             field = self.model._meta.get_field(field)
 
 
+class ThumbnailAdminMixin(admin.ModelAdmin):
+    """
+    Shortcut for displaying a thumbnail in a changelist.
+
+    Requires easy-thumbnails.
+
+    Specify ImageField name in `thumbnail_field`, and optionally override
+    `thumbnail_options` for customisation such as sizing, cropping, etc.
+    Plays nicely with list_display_links if you want a clickable thumbnail.
+    """
+
+    thumbnail_field = None
+    thumbnail_options = {'size': (100,100)}
+
+    def thumbnail(self, obj):
+        if not self.thumbnail_field:
+            logger.warning('ThumbnailAdminMixin.thumbnail_field unspecified')
+            return ''
+
+        try:
+            field_value = getattr(obj, self.thumbnail_field)
+        except AttributeError:
+            logger.error('ThumbnailAdminMixin.thumbnail_field getattr failed')
+            return ''
+
+        if field_value:
+            from easy_thumbnails.files import get_thumbnailer
+            thumbnailer = get_thumbnailer(field_value)
+            thumbnail = thumbnailer.get_thumbnail(self.thumbnail_options)
+            return '<img class="thumbnail" src="{0}" />'.format(thumbnail.url)
+        else:
+            return ''
+    thumbnail.allow_tags = True
+
+
 from ..models.delible import Delible
 class DelibleAdmin(admin.ModelAdmin):
     """ Admin with "undelete" functionality for Delible objects """