"""
Thanks to Corey Oordt
    http://opensource.washingtontimes.com/
    blog/2009/jan/12/generic-collections-django/

"""
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from imagekit.admin import AdminThumbnail


class GenericCollectionInlineModelAdmin(admin.options.InlineModelAdmin):
    ct_field = "content_type"
    ct_fk_field = "object_id"

    def __init__(self, parent_model, admin_site):
        super(GenericCollectionInlineModelAdmin, self).__init__(parent_model,
                                                                admin_site)
        ctypes = ContentType.objects.all(
                    ).order_by('id').values_list('id', 'app_label', 'model')

        elements = ["%s: '%s/%s'" % (id, app_label, model) for id, app_label,
                    model in ctypes]
        self.content_types = "{%s}" % ",".join(elements)

    def get_formset(self, request, obj=None):
        result = super(GenericCollectionInlineModelAdmin, self
                      ).get_formset(request, obj)

        result.content_types = self.content_types
        result.ct_fk_field = self.ct_fk_field
        return result


class GenericCollectionTabularInline(GenericCollectionInlineModelAdmin):
    template = 'galleries/admin/edit_inline/gen_coll_tabular.html'


class GenericCollectionStackedInline(GenericCollectionInlineModelAdmin):
    template = 'galleries/admin/edit_inline/gen_coll_stacked.html'


class GalleryMembershipInline(GenericCollectionTabularInline):
    readonly_fields = ['thumbnail']
    list_editable = ('sort_order', )

    admin_thumbnail_getter = AdminThumbnail('thumbnail')

    def thumbnail(self, obj):
        if hasattr(obj, 'thumbnail'):
            return self.admin_thumbnail_getter(obj.item) if obj.item else ''

        return ''


def create_gallery_membership_inline(membership_class):
    return type('%sInline' % membership_class.__name__,
            (GalleryMembershipInline,), dict(model=membership_class))


class GalleryAdminBase(admin.ModelAdmin.__metaclass__):
    def __init__(cls, class_name, bases, attrs):
        if [b for b in bases if isinstance(b, GalleryAdminBase)]:
            if 'inlines' not in attrs:
                membership_inline = create_gallery_membership_inline(
                    attrs['model'].Membership)

                cls.inlines = [membership_inline]
        return admin.ModelAdmin.__metaclass__.__init__(cls, class_name, bases,
                                                       attrs)


class GalleryAdmin(admin.ModelAdmin):
    __metaclass__ = GalleryAdminBase

    class Media:
            js = [settings.STATIC_URL + 'galleries/js/genericcollection.js',
            'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
            ('https://ajax.googleapis.com/'
             'ajax/libs/jqueryui/1.8.10/jquery-ui.min.js'),
            settings.STATIC_URL + 'galleries/js/admin-list-reorder.js', ]


def register_gallery_admin(gallery_class, admin_class=None):
    """Registers an admin for a gallery. If none is provided, a GalleryAdmin
    will be created. Also registers admins for the member models, since those
    are required for the gallery admin to work.

    """
    if not admin_class:
        admin_class = type('GalleryAdmin', (GalleryAdmin,),
                dict(model=gallery_class))
    admin.site.register(gallery_class, admin_class)

    # Register the members
    for member in gallery_class._gallery_meta.member_models:
        try:
            admin.site.unregister(member)
        except admin.sites.NotRegistered:
            pass
        admin.site.register(member, type('GalleryMemberAdmin',
                (admin.ModelAdmin,), dict(model=member)))

@@ -1636,23 +1636,16 @@
 humbnail
-_getter
  = Admin
@@ -2973,66 +2973,294 @@
 %5D%0A%0A%0A
-def register_gallery_admin(gallery_class, admin_class=None
+class MemberModelAdmin(admin.ModelAdmin):%0A    admin_thumbnail = AdminThumbnail(image_field='thumbnail')%0A    list_display = ('__unicode__', 'admin_thumbnail', )%0A%0A%0Adef register_gallery_admin(gallery_class, admin_class=GalleryAdmin,%0A                           member_class=MemberModelAdmin
 ):%0A 
@@ -3265,16 +3265,21 @@
 %0A    %22%22%22
+%0A    
 Register
@@ -3466,25 +3466,24 @@
 o work.%0A
-%0A
     %22%22%22%0A
     if n
@@ -3478,36 +3478,9 @@
 %22%22%22%0A
-    if not admin_class:%0A    
+%0A
     
@@ -3519,28 +3519,23 @@
 ', (
-GalleryAdmin,),%0A    
+admin_class,),%0A
     
@@ -3569,16 +3569,17 @@
 class))%0A
+%0A
     admi
@@ -3711,16 +3711,17 @@
 models:%0A
+%0A
         
@@ -3826,16 +3826,17 @@
    pass%0A
+%0A
         
@@ -3907,32 +3907,28 @@
        (
-admin.ModelAdmin
+member_class
 ,), dict

Fix admin_thumbnail to show in new apps