Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1 | from core.models import Site |
| 2 | from core.models import * |
| 3 | from openstack.manager import OpenStackManager |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 4 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 5 | from django.contrib import admin |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 6 | from django.contrib.auth.models import Group |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 7 | from django import forms |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 8 | from django.utils.safestring import mark_safe |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 9 | from django.contrib.auth.admin import UserAdmin |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 10 | from django.contrib.admin.widgets import FilteredSelectMultiple |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 11 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 12 | from django.contrib.auth.signals import user_logged_in |
| 13 | from django.utils import timezone |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 14 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 15 | |
| 16 | class 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 | |
| 32 | class SliverInline(admin.TabularInline): |
| 33 | model = Sliver |
Tony Mack | 3777b01 | 2013-05-07 21:38:06 -0400 | [diff] [blame] | 34 | fields = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 35 | extra = 0 |
Tony Mack | 3777b01 | 2013-05-07 21:38:06 -0400 | [diff] [blame] | 36 | #readonly_fields = ['ip', 'instance_name', 'image'] |
| 37 | readonly_fields = ['ip', 'instance_name'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 38 | |
| 39 | class SiteInline(admin.TabularInline): |
| 40 | model = Site |
| 41 | extra = 0 |
| 42 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 43 | class UserInline(admin.TabularInline): |
| 44 | model = User |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 45 | fields = ['email', 'firstname', 'lastname'] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 46 | extra = 0 |
| 47 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 48 | class SliceInline(admin.TabularInline): |
| 49 | model = Slice |
| 50 | extra = 0 |
| 51 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 52 | class RoleInline(admin.TabularInline): |
| 53 | model = Role |
| 54 | extra = 0 |
| 55 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 56 | class NodeInline(admin.TabularInline): |
| 57 | model = Node |
| 58 | extra = 0 |
| 59 | |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 60 | class SitePrivilegeInline(admin.TabularInline): |
| 61 | model = SitePrivilege |
| 62 | extra = 0 |
| 63 | |
| 64 | class SliceMembershipInline(admin.TabularInline): |
| 65 | model = SliceMembership |
| 66 | extra = 0 |
| 67 | |
Tony Mack | 5e71a66 | 2013-05-03 23:30:41 -0400 | [diff] [blame] | 68 | class PlainTextWidget(forms.HiddenInput): |
| 69 | input_type = 'hidden' |
| 70 | |
| 71 | def render(self, name, value, attrs=None): |
| 72 | if value is None: |
| 73 | value = '' |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 74 | return mark_safe(str(value) + super(PlainTextWidget, self).render(name, value, attrs)) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 75 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 76 | class PlanetStackBaseAdmin(admin.ModelAdmin): |
| 77 | save_on_top = False |
| 78 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 79 | class OSModelAdmin(PlanetStackBaseAdmin): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 80 | """Attach client connection to openstack on delete() and save()""" |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 81 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 82 | def save_model(self, request, obj, form, change): |
Tony Mack | 38e247c | 2013-05-05 11:48:14 -0400 | [diff] [blame] | 83 | if request.user.site: |
| 84 | auth = request.session.get('auth', {}) |
| 85 | auth['tenant'] = request.user.site.login_base |
| 86 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 87 | obj.save() |
| 88 | |
| 89 | def delete_model(self, request, obj): |
Tony Mack | 38e247c | 2013-05-05 11:48:14 -0400 | [diff] [blame] | 90 | if request.user.site: |
| 91 | auth = request.session.get('auth', {}) |
| 92 | auth['tenant'] = request.user.site.login_base |
| 93 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 94 | obj.delete() |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 95 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 96 | class RoleAdmin(OSModelAdmin): |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 97 | fieldsets = [ |
| 98 | ('Role', {'fields': ['role_type']}) |
| 99 | ] |
| 100 | list_display = ('role_type',) |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 101 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 102 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 103 | class DeploymentNetworkAdminForm(forms.ModelForm): |
| 104 | sites = forms.ModelMultipleChoiceField( |
| 105 | queryset=Site.objects.all(), |
| 106 | required=False, |
| 107 | widget=FilteredSelectMultiple( |
| 108 | verbose_name=('Sites'), is_stacked=False |
| 109 | ) |
| 110 | ) |
| 111 | class Meta: |
| 112 | model = DeploymentNetwork |
| 113 | |
| 114 | def __init__(self, *args, **kwargs): |
| 115 | super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs) |
| 116 | |
| 117 | if self.instance and self.instance.pk: |
| 118 | self.fields['sites'].initial = self.instance.sites.all() |
| 119 | |
| 120 | def save(self, commit=True): |
| 121 | deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 122 | if commit: |
| 123 | deploymentNetwork.save() |
| 124 | |
| 125 | if deploymentNetwork.pk: |
| 126 | deploymentNetwork.sites = self.cleaned_data['sites'] |
| 127 | self.save_m2m() |
| 128 | |
| 129 | return deploymentNetwork |
| 130 | |
| 131 | class DeploymentNetworkAdmin(PlanetStackBaseAdmin): |
| 132 | form = DeploymentNetworkAdminForm |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 133 | inlines = [NodeInline,SliverInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 134 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 135 | def get_formsets(self, request, obj=None): |
| 136 | for inline in self.get_inline_instances(request, obj): |
| 137 | # hide MyInline in the add view |
| 138 | if obj is None: |
| 139 | continue |
| 140 | # give inline object access to driver and caller |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 141 | auth = request.session.get('auth', {}) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 142 | if request.user.site: |
| 143 | auth['tenant'] = request.user.site.login_base |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 144 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 145 | yield inline.get_formset(request, obj) |
| 146 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 147 | class SiteAdmin(OSModelAdmin): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 148 | fieldsets = [ |
| 149 | (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}), |
| 150 | ('Location', {'fields': ['latitude', 'longitude']}), |
| 151 | ('Deployment Networks', {'fields': ['deployments']}) |
| 152 | ] |
| 153 | list_display = ('name', 'login_base','site_url', 'enabled') |
| 154 | filter_horizontal = ('deployments',) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 155 | inlines = [NodeInline, UserInline, SitePrivilegeInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 156 | search_fields = ['name'] |
| 157 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 158 | def queryset(self, request): |
| 159 | # admins can see all keys. Users can only see sites they belong to. |
| 160 | qs = super(SiteAdmin, self).queryset(request) |
| 161 | if not request.user.is_admin: |
| 162 | valid_sites = [request.user.site.login_base] |
| 163 | roles = request.user.get_roles() |
| 164 | for tenant_list in roles.values(): |
| 165 | valid_sites.extend(tenant_list) |
| 166 | qs = qs.filter(login_base__in=valid_sites) |
| 167 | return qs |
| 168 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 169 | def get_formsets(self, request, obj=None): |
| 170 | for inline in self.get_inline_instances(request, obj): |
| 171 | # hide MyInline in the add view |
| 172 | if obj is None: |
| 173 | continue |
| 174 | # give inline object access to driver and caller |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 175 | auth = request.session.get('auth', {}) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 176 | #auth['tenant'] = request.user.site.login_base |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 177 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 178 | yield inline.get_formset(request, obj) |
| 179 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 180 | class SitePrivilegeAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 181 | fieldsets = [ |
| 182 | (None, {'fields': ['user', 'site', 'role']}) |
| 183 | ] |
| 184 | list_display = ('user', 'site', 'role') |
| 185 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 186 | def queryset(self, request): |
| 187 | # admins can see all privileges. Users can only see privileges at sites |
| 188 | # where they have the admin role. |
| 189 | qs = super(SitePrivilegeAdmin, self).queryset(request) |
| 190 | if not request.user.is_admin: |
| 191 | roles = request.user.get_roles() |
| 192 | tenants = [] |
| 193 | for (role, tenant_list) in roles: |
| 194 | if role == 'admin': |
| 195 | tenants.extend(tenant_list) |
| 196 | valid_sites = Sites.objects.filter(login_base__in=tenants) |
| 197 | qs = qs.filter(site__in=valid_sites) |
| 198 | return qs |
| 199 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 200 | def save_model(self, request, obj, form, change): |
| 201 | # update openstack connection to use this site/tenant |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 202 | auth = request.session.get('auth', {}) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 203 | #auth['tenant'] = obj.site.login_base |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 204 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 205 | obj.save() |
| 206 | |
| 207 | def delete_model(self, request, obj): |
| 208 | # update openstack connection to use this site/tenant |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 209 | auth = request.session.get('auth', {}) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 210 | #auth['tenant'] = obj.site.login_base |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 211 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 212 | obj.delete() |
| 213 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 214 | class KeyAdmin(OSModelAdmin): |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 215 | fieldsets = [ |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 216 | ('Key', {'fields': ['key', 'type', 'blacklisted']}) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 217 | ] |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 218 | list_display = ['key', 'type', 'blacklisted'] |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 219 | |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 220 | #def queryset(self, request): |
Tony Mack | c14de8f | 2013-05-09 21:44:17 -0400 | [diff] [blame] | 221 | # admins can see all keys. Users can only see their own key. |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 222 | #if request.user.is_admin: |
| 223 | # qs = super(KeyAdmin, self).queryset(request) |
| 224 | #else: |
| 225 | # qs = Key.objects.filter(user=request.user) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 226 | #return qs |
| 227 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 228 | class SliceAdmin(OSModelAdmin): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 229 | fields = ['name', 'site', 'serviceClass', 'description', 'slice_url'] |
| 230 | list_display = ('name', 'site','serviceClass', 'slice_url') |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 231 | inlines = [SliverInline, SliceMembershipInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 232 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 233 | def queryset(self, request): |
| 234 | # admins can see all keys. Users can only see slices they belong to. |
| 235 | qs = super(SliceAdmin, self).queryset(request) |
| 236 | if not request.user.is_admin: |
| 237 | valid_slices = [] |
| 238 | roles = request.user.get_roles() |
| 239 | for tenant_list in roles.values(): |
| 240 | valid_slices.extend(tenant_list) |
| 241 | qs = qs.filter(name__in=valid_slices) |
| 242 | return qs |
| 243 | |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 244 | def get_formsets(self, request, obj=None): |
| 245 | for inline in self.get_inline_instances(request, obj): |
| 246 | # hide MyInline in the add view |
| 247 | if obj is None: |
| 248 | continue |
| 249 | # give inline object access to driver and caller |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 250 | auth = request.session.get('auth', {}) |
| 251 | auth['tenant'] = obj.name # meed to connect using slice's tenant |
| 252 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 253 | yield inline.get_formset(request, obj) |
| 254 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 255 | def get_queryset(self, request): |
| 256 | qs = super(SliceAdmin, self).get_queryset(request) |
| 257 | if request.user.is_superuser: |
| 258 | return qs |
| 259 | # users can only see slices at their site |
| 260 | return qs.filter(site=request.user.site) |
| 261 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 262 | class SliceMembershipAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 263 | fieldsets = [ |
| 264 | (None, {'fields': ['user', 'slice', 'role']}) |
| 265 | ] |
| 266 | list_display = ('user', 'slice', 'role') |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 267 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 268 | def queryset(self, request): |
| 269 | # admins can see all memberships. Users can only see memberships of |
| 270 | # slices where they have the admin role. |
| 271 | qs = super(SliceMembershipAdmin, self).queryset(request) |
| 272 | if not request.user.is_admin: |
| 273 | roles = request.user.get_roles() |
| 274 | tenants = [] |
| 275 | for (role, tenant_list) in roles: |
| 276 | if role == 'admin': |
| 277 | tenants.extend(tenant_list) |
| 278 | valid_slices = Slice.objects.filter(name__in=tenants) |
| 279 | qs = qs.filter(slice__in=valid_slices) |
| 280 | return qs |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 281 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 282 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 283 | # update openstack connection to use this site/tenant |
| 284 | auth = request.session.get('auth', {}) |
| 285 | auth['tenant'] = obj.slice.name |
| 286 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 287 | obj.save() |
| 288 | |
| 289 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 290 | # update openstack connection to use this site/tenant |
| 291 | auth = request.session.get('auth', {}) |
| 292 | auth['tenant'] = obj.slice.name |
| 293 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 294 | obj.delete() |
| 295 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 296 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 297 | class ImageAdmin(admin.ModelAdmin): |
| 298 | fields = ['image_id', 'name', 'disk_format', 'container_format'] |
| 299 | |
| 300 | class NodeAdmin(admin.ModelAdmin): |
| 301 | list_display = ('name', 'site', 'deploymentNetwork') |
| 302 | list_filter = ('deploymentNetwork',) |
| 303 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 304 | |
| 305 | class SliverForm(forms.ModelForm): |
| 306 | class Meta: |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 307 | model = Sliver |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 308 | ip = forms.CharField(widget=PlainTextWidget) |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 309 | instance_name = forms.CharField(widget=PlainTextWidget) |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 310 | widgets = { |
| 311 | 'ip': PlainTextWidget(), |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 312 | 'instance_name': PlainTextWidget(), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 313 | } |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 314 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 315 | class SliverAdmin(PlanetStackBaseAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 316 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 317 | fieldsets = [ |
Tony Mack | 1008202 | 2013-05-06 17:15:00 -0400 | [diff] [blame] | 318 | ('Sliver', {'fields': ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']}) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 319 | ] |
Tony Mack | 1008202 | 2013-05-06 17:15:00 -0400 | [diff] [blame] | 320 | list_display = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork'] |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 321 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 322 | def queryset(self, request): |
| 323 | # admins can see all slivers. Users can only see slivers of |
| 324 | # the slices they belong to. |
| 325 | qs = super(SliverAdmin, self).queryset(request) |
| 326 | if not request.user.is_admin: |
| 327 | tenants = [] |
| 328 | roles = request.user.get_roles() |
| 329 | for tenant_list in roles.values(): |
| 330 | tenants.extend(tenant_list) |
| 331 | valid_slices = Slice.objects.filter(name__in=tenants) |
| 332 | qs = qs.filter(slice__in=valid_slices) |
| 333 | return qs |
| 334 | |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 335 | def get_formsets(self, request, obj=None): |
| 336 | # make some fields read only if we are updating an existing record |
| 337 | if obj == None: |
| 338 | #self.readonly_fields = ('ip', 'instance_name') |
| 339 | self.readonly_fields = () |
| 340 | else: |
Tony Mack | 1e88946 | 2013-05-10 21:34:54 -0400 | [diff] [blame] | 341 | self.readonly_fields = () |
| 342 | #self.readonly_fields = ('ip', 'instance_name', 'slice', 'image', 'key') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 343 | |
| 344 | for inline in self.get_inline_instances(request, obj): |
| 345 | # hide MyInline in the add view |
| 346 | if obj is None: |
| 347 | continue |
| 348 | # give inline object access to driver and caller |
| 349 | auth = request.session.get('auth', {}) |
| 350 | auth['tenant'] = obj.name # meed to connect using slice's tenant |
| 351 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 352 | yield inline.get_formset(request, obj) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 353 | |
| 354 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 355 | # update openstack connection to use this site/tenant |
| 356 | auth = request.session.get('auth', {}) |
| 357 | auth['tenant'] = obj.slice.name |
| 358 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 359 | obj.save() |
| 360 | |
| 361 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 362 | # update openstack connection to use this site/tenant |
| 363 | auth = request.session.get('auth', {}) |
| 364 | auth['tenant'] = obj.slice.name |
| 365 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 366 | obj.delete() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 367 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 368 | class UserCreationForm(forms.ModelForm): |
| 369 | """A form for creating new users. Includes all the required |
| 370 | fields, plus a repeated password.""" |
| 371 | password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| 372 | password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| 373 | |
| 374 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 375 | model = User |
| 376 | fields = ('email', 'firstname', 'lastname', 'phone', 'key', 'site') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 377 | |
| 378 | def clean_password2(self): |
| 379 | # Check that the two password entries match |
| 380 | password1 = self.cleaned_data.get("password1") |
| 381 | password2 = self.cleaned_data.get("password2") |
| 382 | if password1 and password2 and password1 != password2: |
| 383 | raise forms.ValidationError("Passwords don't match") |
| 384 | return password2 |
| 385 | |
| 386 | def save(self, commit=True): |
| 387 | # Save the provided password in hashed format |
| 388 | user = super(UserCreationForm, self).save(commit=False) |
Tony Mack | f9f4afb | 2013-05-01 21:02:12 -0400 | [diff] [blame] | 389 | user.password = self.cleaned_data["password1"] |
| 390 | #user.set_password(self.cleaned_data["password1"]) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 391 | if commit: |
| 392 | user.save() |
| 393 | return user |
| 394 | |
| 395 | |
| 396 | class UserChangeForm(forms.ModelForm): |
| 397 | """A form for updating users. Includes all the fields on |
| 398 | the user, but replaces the password field with admin's |
| 399 | password hash display field. |
| 400 | """ |
| 401 | password = ReadOnlyPasswordHashField() |
| 402 | |
| 403 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 404 | model = User |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 405 | |
| 406 | def clean_password(self): |
| 407 | # Regardless of what the user provides, return the initial value. |
| 408 | # This is done here, rather than on the field, because the |
| 409 | # field does not have access to the initial value |
| 410 | return self.initial["password"] |
| 411 | |
| 412 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 413 | class UserAdmin(UserAdmin, OSModelAdmin): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 414 | class Meta: |
| 415 | app_label = "core" |
| 416 | |
| 417 | # The forms to add and change user instances |
| 418 | form = UserChangeForm |
| 419 | add_form = UserCreationForm |
| 420 | |
| 421 | # The fields to be used in displaying the User model. |
| 422 | # These override the definitions on the base UserAdmin |
| 423 | # that reference specific fields on auth.User. |
Tony Mack | 416c0f2 | 2013-05-09 16:59:09 -0400 | [diff] [blame] | 424 | list_display = ('email', 'site', 'firstname', 'lastname', 'is_admin', 'last_login') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 425 | list_filter = ('site',) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 426 | inlines = [SitePrivilegeInline, SliceMembershipInline] |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 427 | fieldsets = ( |
Scott Baker | 9266e6b | 2013-05-19 15:54:48 -0700 | [diff] [blame] | 428 | (None, {'fields': ('email', 'password', 'site', 'is_admin', 'timezone')}), |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 429 | ('Personal info', {'fields': ('firstname','lastname','phone', 'key')}), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 430 | #('Important dates', {'fields': ('last_login',)}), |
| 431 | ) |
| 432 | add_fieldsets = ( |
| 433 | (None, { |
| 434 | 'classes': ('wide',), |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 435 | 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'key','password1', 'password2', 'is_admin')} |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 436 | ), |
| 437 | ) |
| 438 | search_fields = ('email',) |
| 439 | ordering = ('email',) |
| 440 | filter_horizontal = () |
| 441 | |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 442 | class ServiceResourceInline(admin.TabularInline): |
| 443 | model = ServiceResource |
| 444 | extra = 0 |
| 445 | |
| 446 | class ServiceClassAdmin(admin.ModelAdmin): |
| 447 | list_display = ('name', 'commitment', 'membershipFee') |
| 448 | inlines = [ServiceResourceInline] |
| 449 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 450 | class ReservedResourceInline(admin.TabularInline): |
| 451 | model = ReservedResource |
| 452 | extra = 0 |
| 453 | |
| 454 | def formfield_for_foreignkey(self, db_field, request=None, **kwargs): |
| 455 | field = super(ReservedResourceInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 456 | |
| 457 | if db_field.name == 'resource': |
| 458 | # restrict resources to those that the slice's service class allows |
| 459 | if request._slice is not None: |
| 460 | field.queryset = field.queryset.filter(serviceClass = request._slice.serviceClass, calendarReservable=True) |
| 461 | if len(field.queryset) > 0: |
| 462 | field.initial = field.queryset.all()[0] |
| 463 | else:
|
| 464 | field.queryset = field.queryset.none()
|
| 465 | elif db_field.name == 'sliver':
|
| 466 | # restrict slivers to those that belong to the slice
|
| 467 | if request._slice is not None:
|
| 468 | field.queryset = field.queryset.filter(slice = request._slice) |
| 469 | else: |
| 470 | field.queryset = field.queryset.none()
|
| 471 |
|
| 472 | return field |
| 473 | |
| 474 | class ReservationChangeForm(forms.ModelForm): |
| 475 | class Meta: |
| 476 | model = Reservation |
| 477 | |
| 478 | class ReservationAddForm(forms.ModelForm): |
| 479 | slice = forms.ModelChoiceField(queryset=Slice.objects.all(), widget=forms.Select(attrs={"onChange":"document.getElementById('id_refresh').value=1; submit()"})) |
| 480 | refresh = forms.CharField(widget=forms.HiddenInput()) |
| 481 | |
| 482 | class Media: |
| 483 | css = {'all': ('planetstack.css',)} # .field-refresh { display: none; } |
| 484 | |
| 485 | def clean_slice(self): |
| 486 | slice = self.cleaned_data.get("slice") |
| 487 | x = ServiceResource.objects.filter(serviceClass = slice.serviceClass, calendarReservable=True) |
| 488 | if len(x) == 0: |
| 489 | raise forms.ValidationError("The slice you selected does not have a service class that allows reservations") |
| 490 | return slice |
| 491 | |
| 492 | class Meta: |
| 493 | model = Reservation |
| 494 | |
| 495 | class ReservationAddRefreshForm(ReservationAddForm): |
| 496 | """ This form is displayed when the Reservation Form receives an update |
| 497 | from the Slice dropdown onChange handler. It doesn't validate the |
| 498 | data and doesn't save the data. This will cause the form to be |
| 499 | redrawn. |
| 500 | """ |
| 501 | |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 502 | """ don't validate anything other than slice """ |
| 503 | dont_validate_fields = ("startTime", "duration") |
| 504 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 505 | def full_clean(self): |
| 506 | result = super(ReservationAddForm, self).full_clean() |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 507 | |
| 508 | for fieldname in self.dont_validate_fields: |
| 509 | if fieldname in self._errors: |
| 510 | del self._errors[fieldname] |
| 511 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 512 | return result |
| 513 | |
| 514 | """ don't save anything """ |
| 515 | def is_valid(self): |
| 516 | return False |
| 517 | |
| 518 | class ReservationAdmin(admin.ModelAdmin): |
| 519 | list_display = ('startTime', 'duration') |
| 520 | inlines = [ReservedResourceInline] |
| 521 | form = ReservationAddForm |
| 522 | |
| 523 | def add_view(self, request, form_url='', extra_context=None): |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 524 | timezone.activate(request.user.timezone) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 525 | request._refresh = False |
| 526 | request._slice = None |
| 527 | if request.method == 'POST': |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 528 | # "refresh" will be set to "1" if the form was submitted due to |
| 529 | # a change in the Slice dropdown. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 530 | if request.POST.get("refresh","1") == "1": |
| 531 | request._refresh = True |
| 532 | request.POST["refresh"] = "0" |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 533 | |
| 534 | # Keep track of the slice that was selected, so the |
| 535 | # reservedResource inline can filter items for the slice. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 536 | request._slice = request.POST.get("slice",None) |
| 537 | if (request._slice is not None): |
| 538 | request._slice = Slice.objects.get(id=request._slice) |
| 539 | |
| 540 | result = super(ReservationAdmin, self).add_view(request, form_url, extra_context) |
| 541 | return result |
| 542 | |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 543 | def changelist_view(self, request, extra_context = None): |
| 544 | timezone.activate(request.user.timezone) |
| 545 | return super(ReservationAdmin, self).changelist_view(request, extra_context) |
| 546 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 547 | def get_form(self, request, obj=None, **kwargs): |
| 548 | request._obj_ = obj
|
| 549 | if obj is not None:
|
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 550 | # For changes, set request._slice to the slice already set in the
|
| 551 | # object.
|
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 552 | request._slice = obj.slice
|
| 553 | self.form = ReservationChangeForm
|
| 554 | else:
|
| 555 | if getattr(request, "_refresh", False):
|
| 556 | self.form = ReservationAddRefreshForm
|
| 557 | else:
|
| 558 | self.form = ReservationAddForm
|
| 559 | return super(ReservationAdmin, self).get_form(request, obj, **kwargs)
|
| 560 |
|
| 561 | def get_readonly_fields(self, request, obj=None): |
| 562 | if (obj is not None):
|
| 563 | # Prevent slice from being changed after the reservation has been
|
| 564 | # created.
|
| 565 | return ['slice']
|
| 566 | else:
|
| 567 | return [] |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 568 | |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 569 | # register a signal that caches the user's credentials when they log in |
| 570 | def cache_credentials(sender, user, request, **kwds): |
| 571 | auth = {'username': request.POST['username'], |
| 572 | 'password': request.POST['password']} |
| 573 | request.session['auth'] = auth |
| 574 | user_logged_in.connect(cache_credentials) |
| 575 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 576 | # Now register the new UserAdmin... |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 577 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 578 | # ... and, since we're not using Django's builtin permissions, |
| 579 | # unregister the Group model from admin. |
| 580 | admin.site.unregister(Group) |
| 581 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 582 | admin.site.register(Site, SiteAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 583 | admin.site.register(SitePrivilege, SitePrivilegeAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 584 | admin.site.register(Slice, SliceAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 585 | admin.site.register(SliceMembership, SliceMembershipAdmin) |
| 586 | #admin.site.register(Subnet) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 587 | admin.site.register(Image, ImageAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 588 | admin.site.register(Node, NodeAdmin) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 589 | admin.site.register(Sliver, SliverAdmin) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 590 | admin.site.register(Key, KeyAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 591 | admin.site.register(Role, RoleAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 592 | admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin) |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 593 | admin.site.register(ServiceClass, ServiceClassAdmin) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 594 | admin.site.register(Reservation, ReservationAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 595 | |