Source

HackersEdge / he_one / admin.py

Full commit
from django.contrib import admin
from he_one.models import Host, NameSystem, UserPermission, Log, PCFile, Message,\
    Mailbox, FilePermission, Objective, Mission, MissionObjectives, UserProfile,\
    Task

class NameSystemInline(admin.StackedInline):
    model = NameSystem
    extra = 0

class HostInline(admin.TabularInline):
    model = Host
    extra = 0
    exclude = ('mailboxes', 'logs',)
    raw_id_fields = ('owner',)

class UserPermissionInline(admin.TabularInline):
    model = UserPermission
    extra = 1
    raw_id_fields = ('user',)

class FilePermissionInline(admin.TabularInline):
    model = FilePermission
    extra = 1
    raw_id_fields = ('creator',)

class MissionObjectivesInline(admin.TabularInline):
    model = MissionObjectives
    extra = 1

class NameSystemAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('hostname', 'domain',)}
    list_display = ('__unicode__', 'slug', 'host',)

class HostAdmin(admin.ModelAdmin):
    inlines = [FilePermissionInline, UserPermissionInline, NameSystemInline, ]
    list_display = ('ip', 'owner', 'is_router', 'is_booted', 'supports_mail', 'supports_web',)
    list_filter = ('owner', 'is_router', 'is_booted', 'supports_mail', 'supports_web',)
    search_fields = ['ip']
    list_select_related = True
    filter_horizontal = ['mailboxes', 'logs', 'mission_list']
    raw_id_fields = ('owner',)
    save_on_top = True
    fieldsets = (
        (None, {'fields': ('ip', 'gateway', 'owner',)}),
        ('Options', {'fields': ('is_router', 'is_booted', 'supports_mail',)}),
        ('HTTPSERV.PRG', {'classes': ('collapse',), 'fields': ('supports_web', 'supports_store', 'supports_download', 'supports_search',)}),
        ('Mission Provider', {'classes': ('collapse',), 'fields': ('provides_missions', 'mission_list',)}),
        ('Resources', {'classes': ('collapse',), 'fields': ('mailboxes', 'logs',)}),
    )
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'gateway':
            kwargs['queryset'] = Host.objects.filter(is_router=True)
        return super(HostAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

class MissionAdmin(admin.ModelAdmin):
    inlines = [MissionObjectivesInline, ]
    list_display = ('title', 'level', 'cash', 'owner',)

class LogAdmin(admin.ModelAdmin):
    list_display = ('message', 'by_who', 'logged_at', 'deleted', )
    list_filter = ('deleted', )

class MailboxAdmin(admin.ModelAdmin):
    filter_horizontal = ('messages', )

class MessageAdmin(admin.ModelAdmin):
    list_display = ('subject', 'from_user', 'to_user', )
    filter_horizontal = ('attachments', )

class UserPermissionAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'is_staff', 'is_superuser', )

class ObjectiveAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'exp', )

class PCFileAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'get_filesize', )

admin.site.register(NameSystem, NameSystemAdmin)
admin.site.register(Host, HostAdmin)
admin.site.register(UserPermission, UserPermissionAdmin)
admin.site.register(Log, LogAdmin)
admin.site.register(PCFile, PCFileAdmin)
admin.site.register(Message, MessageAdmin)
admin.site.register(Mailbox, MailboxAdmin)
admin.site.register(FilePermission)
admin.site.register(Objective, ObjectiveAdmin)
admin.site.register(Mission, MissionAdmin)
admin.site.register(MissionObjectives)
admin.site.register(UserProfile)
admin.site.register(Task)