admin.py 4.13 KB
Newer Older
Dudás Ádám committed
1 2
from django import contrib
from django.utils.translation import ugettext_lazy as _
3

Dudás Ádám committed
4
from one import models
5 6
import school.models

Dudás Ádám committed
7

8 9 10 11 12
def owner_person(obj):
    p = obj.owner
    return "%s %s (%s)" % (p.last_name, p.first_name, p.username)
owner_person.short_description = _('owner')

13

Dudás Ádám committed
14
class PersonInline(contrib.admin.StackedInline):
15
    model = school.models.Person
Dudás Ádám committed
16 17
    max_num = 1
    can_delete = False
18 19


Dudás Ádám committed
20 21 22
class SshKeyInline(contrib.admin.TabularInline):
    model = models.SshKey
    extra = 2
23 24


Dudás Ádám committed
25 26 27 28 29
class DetailsInline(contrib.admin.StackedInline):
    model = models.UserCloudDetails
    max_num = 1
    can_delete = False

30

31 32 33 34 35 36 37 38 39 40 41 42 43 44
class MyUserAdmin(contrib.auth.admin.UserAdmin):
    list_display = ('username', 'full_name', 'email',
                    'date_joined', 'instance_count', 'course_groups')
    list_filter = ('is_superuser', 'is_active',
                   'groups', 'person__course_groups', )

    def __init__(self, *args, **kwargs):
        super(MyUserAdmin, self).__init__(*args, **kwargs)
        add = [PersonInline, SshKeyInline, DetailsInline, ]
        try:
            inlines = self.inlines
        except NameError:
            inlines = list()
        self.inlines = inlines + add
45

cloud committed
46
    def instance_count(self, obj):
47 48 49
        return _("%(sum)d (%(active)d active)") % {
            'sum': obj.instance_set.count(),
            'active': obj.instance_set.filter(state='ACTIVE').count(), }
50 51 52 53 54 55 56

    def course_groups(self, obj):
        try:
            return ", ".join(obj.person_set.all()[0].course_groups.all())
        except:
            return None

cloud committed
57 58 59
    def full_name(self, obj):
        return u"%s %s" % (obj.last_name, obj.first_name)
    full_name.admin_order_field = 'last_name'
Dudás Ádám committed
60

61 62
    ordering = ["-date_joined"]

Dudás Ádám committed
63 64 65 66

contrib.admin.site.unregister(contrib.auth.models.User)
contrib.admin.site.register(contrib.auth.models.User, MyUserAdmin)

67

Dudás Ádám committed
68 69 70 71 72
def update_state(modeladmin, request, queryset):
    for i in queryset.all():
        i.update_state()
update_state.short_description = _('Update status')

73

Dudás Ádám committed
74 75 76
def submit_vm(modeladmin, request, queryset):
    for i in queryset.all():
        i.submit(request.user)
77
        i.update_state()
Dudás Ádám committed
78 79
submit_vm.short_description = _('Submit VM')

80

81 82 83 84 85
def delete_vm(modeladmin, request, queryset):
    for i in queryset.exclude(state='DONE').all():
        i.one_delete()
delete_vm.short_description = _('Delete VM')

86

87 88 89 90 91
def suspend_vm(modeladmin, request, queryset):
    for i in queryset.filter(state='ACTIVE').all():
        i.stop()
suspend_vm.short_description = _('Suspend VM')

92

93 94 95 96 97
def resume_vm(modeladmin, request, queryset):
    for i in queryset.filter(state__in=('STOPPED', 'SUSPENDED')).all():
        i.resume()
resume_vm.short_description = _('Resume VM')

Dudás Ádám committed
98 99

class TemplateAdmin(contrib.admin.ModelAdmin):
100
    model = models.Template
101
    list_display = ('name', 'state', owner_person, 'system', 'public')
102
    list_filter = ('owner', 'public')
Dudás Ádám committed
103

104

Dudás Ádám committed
105
class InstanceAdmin(contrib.admin.ModelAdmin):
106
    model = models.Instance
107
    actions = [update_state, submit_vm, delete_vm, suspend_vm, resume_vm]
108
    list_display = ('id', 'name', owner_person, 'state')
Bach Dániel committed
109
    readonly_fields = ('ip', 'active_since', 'pw', )
110
    list_filter = ('owner', 'template', 'state')
111

112 113
    def queryset(self, request):
        return super(InstanceAdmin, self).queryset(request)
cloud committed
114

115

cloud committed
116
class DiskAdmin(contrib.admin.ModelAdmin):
117
    model = models.Disk
118 119 120 121 122 123 124 125 126
    list_display = ('name', 'used_by')

    def used_by(self, obj):
        try:
            return ", ".join(obj.template_set.all())
        except:
            return None
    used_by.verbose_name = _('used by')

127

cloud committed
128
class NetworkAdmin(contrib.admin.ModelAdmin):
129
    model = models.Network
130 131
    list_display = ('name', 'nat', 'public', 'get_vlan')

132

Őry Máté committed
133
class ShareAdmin(contrib.admin.ModelAdmin):
134
    model = models.Network
135
    list_filter = ('group', 'template', )
136
    list_display = ('name', owner_person, 'template', 'group', )
cloud committed
137

Dudás Ádám committed
138 139
contrib.admin.site.register(models.Template, TemplateAdmin)
contrib.admin.site.register(models.Instance, InstanceAdmin)
cloud committed
140 141
contrib.admin.site.register(models.Network, NetworkAdmin)
contrib.admin.site.register(models.Disk, DiskAdmin)
Őry Máté committed
142
contrib.admin.site.register(models.Share, ShareAdmin)
143
contrib.admin.site.register(models.InstanceType)