blob: 8ee3e1cb775b26f004568061157d627a1ba4e55a [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001from plstackapi.core.models import Site
Tony Mackf1c12242013-04-09 16:08:43 -04002from plstackapi.core.models import *
Tony Mack7130ac32013-03-22 21:58:00 -04003from django.contrib import admin
Siobhan Tully4bc09f22013-04-10 21:15:21 -04004from django import forms
Tony Mackd90cdbf2013-04-16 22:48:40 -04005from django.utils.safestring import mark_safe
Tony Mack7130ac32013-03-22 21:58:00 -04006from django.contrib.auth.admin import UserAdmin
Siobhan Tully4bc09f22013-04-10 21:15:21 -04007from django.contrib.admin.widgets import FilteredSelectMultiple
Tony Mack7130ac32013-03-22 21:58:00 -04008
Siobhan Tully4bc09f22013-04-10 21:15:21 -04009
10class ReadonlyTabularInline(admin.TabularInline):
11 can_delete = False
12 extra = 0
13 editable_fields = []
14
15 def get_readonly_fields(self, request, obj=None):
16 fields = []
17 for field in self.model._meta.get_all_field_names():
18 if (not field == 'id'):
19 if (field not in self.editable_fields):
20 fields.append(field)
21 return fields
22
23 def has_add_permission(self, request):
24 return False
25
26class SliverInline(admin.TabularInline):
27 model = Sliver
28 extra = 0
29
30class SiteInline(admin.TabularInline):
31 model = Site
32 extra = 0
33
34class NodeInline(admin.TabularInline):
35 model = Node
36 extra = 0
37
38class PlanetStackBaseAdmin(admin.ModelAdmin):
39 save_on_top = False
40
41class DeploymentNetworkAdminForm(forms.ModelForm):
42 sites = forms.ModelMultipleChoiceField(
43 queryset=Site.objects.all(),
44 required=False,
45 widget=FilteredSelectMultiple(
46 verbose_name=('Sites'), is_stacked=False
47 )
48 )
49 class Meta:
50 model = DeploymentNetwork
51
52 def __init__(self, *args, **kwargs):
53 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
54
55 if self.instance and self.instance.pk:
56 self.fields['sites'].initial = self.instance.sites.all()
57
58 def save(self, commit=True):
59 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
60
61 if commit:
62 deploymentNetwork.save()
63
64 if deploymentNetwork.pk:
65 deploymentNetwork.sites = self.cleaned_data['sites']
66 self.save_m2m()
67
68 return deploymentNetwork
69
70class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
71 form = DeploymentNetworkAdminForm
72 inlines = [NodeInline,]
73
74class SiteAdmin(admin.ModelAdmin):
75 fieldsets = [
76 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
77 ('Location', {'fields': ['latitude', 'longitude']}),
78 ('Deployment Networks', {'fields': ['deployments']})
79 ]
80 list_display = ('name', 'login_base','site_url', 'enabled')
81 filter_horizontal = ('deployments',)
82 inlines = [NodeInline,]
83 search_fields = ['name']
84
Tony Mack8484bdb2013-04-14 20:26:03 -040085class UserForm(forms.ModelForm):
86 class Meta:
87 password = forms.CharField(widget=forms.PasswordInput)
88 model = User
89 widgets = {
90 'password': forms.PasswordInput(),
91 }
Tony Mack759b57a2013-04-14 21:03:31 -040092
Tony Mack8484bdb2013-04-14 20:26:03 -040093class UserAdmin(admin.ModelAdmin):
94 form = UserForm
95 fieldsets = [
96 ('User', {'fields': ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site']})
97 ]
98 list_display = ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site']
Tony Mack759b57a2013-04-14 21:03:31 -040099 search_fields = ['email']
100
101class KeyAdmin(admin.ModelAdmin):
102 fieldsets = [
103 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
104 ]
105 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400106
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400107class SliceAdmin(PlanetStackBaseAdmin):
108 fields = ['name', 'site', 'instantiation', 'description', 'slice_url']
109 list_display = ('name', 'site','slice_url', 'instantiation')
110 inlines = [SliverInline]
111
112class SubnetAdmin(admin.ModelAdmin):
113 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
114
115class ImageAdmin(admin.ModelAdmin):
116 fields = ['image_id', 'name', 'disk_format', 'container_format']
117
118class NodeAdmin(admin.ModelAdmin):
119 list_display = ('name', 'site', 'deploymentNetwork')
120 list_filter = ('deploymentNetwork',)
121
Tony Mackfd24d0d2013-04-14 00:59:17 -0400122class RoleAdmin(admin.ModelAdmin):
Tony Mackbc9cecf2013-04-14 13:56:38 -0400123 fieldsets = [
124 ('Role', {'fields': ['role_type']})
125 ]
Tony Mackfd24d0d2013-04-14 00:59:17 -0400126 list_display = ('role_type',)
127
Tony Mackd90cdbf2013-04-16 22:48:40 -0400128class PlainTextWidget(forms.Widget):
129 def render(self, _name, value, attrs):
130 return mark_safe(value) if value is not None else ''
131
132class SliverForm(forms.ModelForm):
133 class Meta:
134 ip = forms.CharField(widget=PlainTextWidget)
135 model = Sliver
136 widgets = {
137 'ip': PlainTextWidget(),
138 }
139
Tony Mackcdec0902013-04-15 00:38:49 -0400140class SliverAdmin(admin.ModelAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400141 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400142 fieldsets = [
Tony Mackd90cdbf2013-04-16 22:48:40 -0400143 ('Sliver', {'fields': ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400144 ]
Tony Mackd90cdbf2013-04-16 22:48:40 -0400145 list_display = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mackcdec0902013-04-15 00:38:49 -0400146
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400147admin.site.register(Site, SiteAdmin)
148admin.site.register(SitePrivilege)
149admin.site.register(Slice, SliceAdmin)
150admin.site.register(SliceMembership)
151admin.site.register(Subnet, SubnetAdmin)
152admin.site.register(Image, ImageAdmin)
153admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400154admin.site.register(Sliver, SliverAdmin)
Tony Mack5cc16ba2013-04-09 10:30:45 -0400155admin.site.register(Flavor)
Tony Mack759b57a2013-04-14 21:03:31 -0400156admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400157admin.site.register(Role, RoleAdmin)
Tony Mack8484bdb2013-04-14 20:26:03 -0400158admin.site.register(User, UserAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400159admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400160