Source

gnocchi-gallery / gnocchi / gallery / admin.py

Full commit

from django.contrib import admin
from django import forms

from gnocchi.gallery import models
from gnocchi.tools.admin import InlineAttributeAdmin

from easy_thumbnails.files import get_thumbnailer

try:
    import grappelli
    HAS_GRAPPELLI = True
except ImportError:
    HAS_GRAPPELLI = False

class ImageInline(admin.TabularInline):
    model = models.Image
    readonly_fields = ('width', 'height',)

    if HAS_GRAPPELLI:
        sortable_field_name = 'order'
        formfield_overrides = {
            models.models.PositiveIntegerField: { 'widget': forms.HiddenInput }
        }


class GalleryAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug',)
    prepopulated_fields = {
        'slug': ('name',)
    }
    inlines = (
        ImageInline,
    )
    def sync_images(self, request, queryset):
        for gallery in queryset.all():
            gallery.sync_images()
    actions = [sync_images]

admin.site.register(models.Gallery, GalleryAdmin)

class ImageAdmin(admin.ModelAdmin):
    list_display = ('thumb', 'gallery', 'image', 'name', 'order', 'width', 'height',)
    list_filter = ('gallery', 'order', 'width', 'height',)
    list_editable = ('order',)
    readonly_fields = ('width', 'height',)
    inlines = [
        InlineAttributeAdmin,
    ]

    def thumb(self, obj):
        img = get_thumbnailer(obj.image).get_thumbnail({'size': (70, 70,)})
        return '<img src="%s" alt="">' % (img.url,)
    thumb.short_description = 'Thumbnail'
    thumb.allow_tags = True

admin.site.register(models.Image, ImageAdmin)

class ThumbnailConfigAdmin(admin.ModelAdmin):
    list_display = ('name',)
    inlines = (
        InlineAttributeAdmin,
    )

admin.site.register(models.ThumbnailConfig, ThumbnailConfigAdmin)