blob: 82cf6138667c9e9a95d307adbcfa9c2eb44c674c [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 Macke59a7c82013-04-27 11:08:10 -04003from plstackapi.openstack.driver import OpenStackDriver
4from plstackapi.openstack.client import OpenStackClient
5
Tony Mack7130ac32013-03-22 21:58:00 -04006from django.contrib import admin
Siobhan Tully53437282013-04-26 19:30:27 -04007from django.contrib.auth.models import Group
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008from django import forms
Tony Mackd90cdbf2013-04-16 22:48:40 -04009from django.utils.safestring import mark_safe
Tony Mack7130ac32013-03-22 21:58:00 -040010from django.contrib.auth.admin import UserAdmin
Siobhan Tully4bc09f22013-04-10 21:15:21 -040011from django.contrib.admin.widgets import FilteredSelectMultiple
Siobhan Tully53437282013-04-26 19:30:27 -040012from django.contrib.auth.forms import ReadOnlyPasswordHashField
Tony Mack31c2b8f2013-04-26 20:01:42 -040013from django.contrib.auth.signals import user_logged_in
Tony Mack7130ac32013-03-22 21:58:00 -040014
Siobhan Tully4bc09f22013-04-10 21:15:21 -040015
16class ReadonlyTabularInline(admin.TabularInline):
17 can_delete = False
18 extra = 0
19 editable_fields = []
20
21 def get_readonly_fields(self, request, obj=None):
22 fields = []
23 for field in self.model._meta.get_all_field_names():
24 if (not field == 'id'):
25 if (field not in self.editable_fields):
26 fields.append(field)
27 return fields
28
29 def has_add_permission(self, request):
30 return False
31
32class SliverInline(admin.TabularInline):
33 model = Sliver
Siobhan Tullyc47c39d2013-04-17 06:47:26 -040034 fields = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']
Siobhan Tully4bc09f22013-04-10 21:15:21 -040035 extra = 0
36
37class SiteInline(admin.TabularInline):
38 model = Site
39 extra = 0
40
41class NodeInline(admin.TabularInline):
42 model = Node
43 extra = 0
44
45class PlanetStackBaseAdmin(admin.ModelAdmin):
46 save_on_top = False
47
48class DeploymentNetworkAdminForm(forms.ModelForm):
49 sites = forms.ModelMultipleChoiceField(
50 queryset=Site.objects.all(),
51 required=False,
52 widget=FilteredSelectMultiple(
53 verbose_name=('Sites'), is_stacked=False
54 )
55 )
56 class Meta:
57 model = DeploymentNetwork
58
59 def __init__(self, *args, **kwargs):
60 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
61
62 if self.instance and self.instance.pk:
63 self.fields['sites'].initial = self.instance.sites.all()
64
65 def save(self, commit=True):
66 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040067 if commit:
68 deploymentNetwork.save()
69
70 if deploymentNetwork.pk:
71 deploymentNetwork.sites = self.cleaned_data['sites']
72 self.save_m2m()
73
74 return deploymentNetwork
75
76class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
77 form = DeploymentNetworkAdminForm
78 inlines = [NodeInline,]
79
80class SiteAdmin(admin.ModelAdmin):
81 fieldsets = [
82 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
83 ('Location', {'fields': ['latitude', 'longitude']}),
84 ('Deployment Networks', {'fields': ['deployments']})
85 ]
86 list_display = ('name', 'login_base','site_url', 'enabled')
87 filter_horizontal = ('deployments',)
88 inlines = [NodeInline,]
89 search_fields = ['name']
90
Tony Mack759b57a2013-04-14 21:03:31 -040091class KeyAdmin(admin.ModelAdmin):
92 fieldsets = [
93 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
94 ]
95 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -040096
Tony Macke59a7c82013-04-27 11:08:10 -040097 def save_model(self, request, obj, form, change):
98 # attach the caller's openstack clien connection to the object
99 client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
100 obj.driver = OpenStackDriver(client=client)
101 obj.save()
102
103 def delete_model(self, request, obj):
104 client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
105 obj.driver = OpenStackDriver(client=client)
106 obj.delete()
107
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400108class SliceAdmin(PlanetStackBaseAdmin):
109 fields = ['name', 'site', 'instantiation', 'description', 'slice_url']
110 list_display = ('name', 'site','slice_url', 'instantiation')
111 inlines = [SliverInline]
112
113class SubnetAdmin(admin.ModelAdmin):
114 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
Siobhan Tully53437282013-04-26 19:30:27 -0400115 list_display = ('slice','cidr', 'start', 'end', 'ip_version' )
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400116
117class ImageAdmin(admin.ModelAdmin):
118 fields = ['image_id', 'name', 'disk_format', 'container_format']
119
120class NodeAdmin(admin.ModelAdmin):
121 list_display = ('name', 'site', 'deploymentNetwork')
122 list_filter = ('deploymentNetwork',)
123
Tony Mackfd24d0d2013-04-14 00:59:17 -0400124class RoleAdmin(admin.ModelAdmin):
Tony Mackbc9cecf2013-04-14 13:56:38 -0400125 fieldsets = [
126 ('Role', {'fields': ['role_type']})
127 ]
Tony Mackfd24d0d2013-04-14 00:59:17 -0400128 list_display = ('role_type',)
129
Tony Mackd90cdbf2013-04-16 22:48:40 -0400130class PlainTextWidget(forms.Widget):
131 def render(self, _name, value, attrs):
132 return mark_safe(value) if value is not None else ''
133
134class SliverForm(forms.ModelForm):
135 class Meta:
136 ip = forms.CharField(widget=PlainTextWidget)
137 model = Sliver
138 widgets = {
139 'ip': PlainTextWidget(),
Siobhan Tully53437282013-04-26 19:30:27 -0400140 }
Tony Mackd90cdbf2013-04-16 22:48:40 -0400141
Tony Mackcdec0902013-04-15 00:38:49 -0400142class SliverAdmin(admin.ModelAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400143 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400144 fieldsets = [
Tony Mackd90cdbf2013-04-16 22:48:40 -0400145 ('Sliver', {'fields': ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400146 ]
Tony Mackd90cdbf2013-04-16 22:48:40 -0400147 list_display = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mackcdec0902013-04-15 00:38:49 -0400148
Siobhan Tully53437282013-04-26 19:30:27 -0400149class UserCreationForm(forms.ModelForm):
150 """A form for creating new users. Includes all the required
151 fields, plus a repeated password."""
152 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
153 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
154
155 class Meta:
156 model = PLUser
157 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
158
159 def clean_password2(self):
160 # Check that the two password entries match
161 password1 = self.cleaned_data.get("password1")
162 password2 = self.cleaned_data.get("password2")
163 if password1 and password2 and password1 != password2:
164 raise forms.ValidationError("Passwords don't match")
165 return password2
166
167 def save(self, commit=True):
168 # Save the provided password in hashed format
169 user = super(UserCreationForm, self).save(commit=False)
170 user.set_password(self.cleaned_data["password1"])
171 if commit:
172 user.save()
173 return user
174
175
176class UserChangeForm(forms.ModelForm):
177 """A form for updating users. Includes all the fields on
178 the user, but replaces the password field with admin's
179 password hash display field.
180 """
181 password = ReadOnlyPasswordHashField()
182
183 class Meta:
184 model = PLUser
185
186 def clean_password(self):
187 # Regardless of what the user provides, return the initial value.
188 # This is done here, rather than on the field, because the
189 # field does not have access to the initial value
190 return self.initial["password"]
191
192
193class PLUserAdmin(UserAdmin):
194 class Meta:
195 app_label = "core"
196
197 # The forms to add and change user instances
198 form = UserChangeForm
199 add_form = UserCreationForm
200
201 # The fields to be used in displaying the User model.
202 # These override the definitions on the base UserAdmin
203 # that reference specific fields on auth.User.
204 list_display = ('email', 'site', 'firstname', 'lastname', 'last_login')
205 list_filter = ('site',)
206 fieldsets = (
207 (None, {'fields': ('email', 'password')}),
208 ('Personal info', {'fields': ('firstname','lastname','phone','site')}),
209 #('Important dates', {'fields': ('last_login',)}),
210 )
211 add_fieldsets = (
212 (None, {
213 'classes': ('wide',),
214 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')}
215 ),
216 )
217 search_fields = ('email',)
218 ordering = ('email',)
219 filter_horizontal = ()
220
Tony Mack31c2b8f2013-04-26 20:01:42 -0400221# register a signal that caches the user's credentials when they log in
222def cache_credentials(sender, user, request, **kwds):
223 auth = {'username': request.POST['username'],
224 'password': request.POST['password']}
225 request.session['auth'] = auth
226user_logged_in.connect(cache_credentials)
227
Siobhan Tully53437282013-04-26 19:30:27 -0400228# Now register the new UserAdmin...
229admin.site.register(PLUser, PLUserAdmin)
230# ... and, since we're not using Django's builtin permissions,
231# unregister the Group model from admin.
232admin.site.unregister(Group)
233
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400234admin.site.register(Site, SiteAdmin)
235admin.site.register(SitePrivilege)
236admin.site.register(Slice, SliceAdmin)
237admin.site.register(SliceMembership)
238admin.site.register(Subnet, SubnetAdmin)
239admin.site.register(Image, ImageAdmin)
240admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400241admin.site.register(Sliver, SliverAdmin)
Tony Mack5cc16ba2013-04-09 10:30:45 -0400242admin.site.register(Flavor)
Tony Mack759b57a2013-04-14 21:03:31 -0400243admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400244admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400245admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400246