from django.contrib import admin

from server.models import *


class ApiKeyAdmin(admin.ModelAdmin):
    list_display = ('name', 'public_key', 'private_key')


class MachineGroupInline(admin.TabularInline):
    model = MachineGroup


class BusinessUnitAdmin(admin.ModelAdmin):
    inlines = [MachineGroupInline,]
class MachineAdmin(admin.ModelAdmin):
    list_display = ('hostname', 'serial')


class MachineGroupAdmin(admin.ModelAdmin):
    readonly_fields = ('key',)


admin.site.register(ApiKey, ApiKeyAdmin)
admin.site.register(BusinessUnit)
admin.site.register(Condition)
admin.site.register(Fact)
admin.site.register(HistoricalFact)
admin.site.register(InstalledUpdate)
admin.site.register(Machine, MachineAdmin)
admin.site.register(MachineDetailPlugin)
admin.site.register(MachineGroup, MachineGroupAdmin)
# admin.site.register(OSQueryColumn)
# admin.site.register(OSQueryResult)
admin.site.register(PendingAppleUpdate)
admin.site.register(PendingUpdate)
admin.site.register(Plugin)
admin.site.register(PluginScriptRow)
admin.site.register(PluginScriptSubmission)
admin.site.register(Report)
admin.site.register(SalSetting)
admin.site.register(UpdateHistory)
admin.site.register(UpdateHistoryItem)
admin.site.register(UserProfile)

@@ -25,16 +25,77 @@
 rt admin
+%0Afrom django.forms import ModelForm, ModelMultipleChoiceField
 %0A%0Afrom s
@@ -310,68 +310,990 @@
 Unit
-Admin(admin.ModelAdmin):%0A    inlines = %5BMachineGroupInline,%5D
+Form(ModelForm):%0A%0A    class Meta:%0A        model = BusinessUnit%0A        fields = %5B'name', 'users'%5D%0A%0A    users = ModelMultipleChoiceField(%0A        queryset=User.objects.exclude(userprofile__level='GA'),%0A        label=('Members'),%0A        required=False,%0A        widget=admin.widgets.FilteredSelectMultiple(verbose_name=%22Users%22, is_stacked=False))%0A%0A%0A%0Adef number_of_users(obj):%0A    return obj.users.count()%0A%0A%0Adef number_of_machine_groups(obj):%0A    return obj.machinegroup_set.count()%0A%0A%0Adef number_of_machines(obj):%0A    return Machine.objects.filter(machine_group__in=obj.machinegroup_set.all()).count()%0A%0A%0Aclass BusinessUnitAdmin(admin.ModelAdmin):%0A    inlines = %5BMachineGroupInline,%5D%0A    form = BusinessUnitForm%0A    list_display = ('name', number_of_users, number_of_machine_groups, number_of_machines)%0A    fields = (('name', number_of_users, number_of_machine_groups, number_of_machines), 'users')%0A    readonly_fields = (number_of_users, number_of_machine_groups, number_of_machines)%0A%0A
 %0Acla
@@ -1520,16 +1520,35 @@
 nessUnit
+, BusinessUnitAdmin
 )%0Aadmin.

Add multi-select users widget, and counts of users, machines, m groups.