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 |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 12 | from django.contrib.auth.signals import user_logged_in |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 13 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 14 | |
| 15 | class ReadonlyTabularInline(admin.TabularInline): |
| 16 | can_delete = False |
| 17 | extra = 0 |
| 18 | editable_fields = [] |
| 19 | |
| 20 | def get_readonly_fields(self, request, obj=None): |
| 21 | fields = [] |
| 22 | for field in self.model._meta.get_all_field_names(): |
| 23 | if (not field == 'id'): |
| 24 | if (field not in self.editable_fields): |
| 25 | fields.append(field) |
| 26 | return fields |
| 27 | |
| 28 | def has_add_permission(self, request): |
| 29 | return False |
| 30 | |
| 31 | class SliverInline(admin.TabularInline): |
| 32 | model = Sliver |
Tony Mack | 3777b01 | 2013-05-07 21:38:06 -0400 | [diff] [blame] | 33 | fields = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 34 | extra = 0 |
Tony Mack | 3777b01 | 2013-05-07 21:38:06 -0400 | [diff] [blame] | 35 | #readonly_fields = ['ip', 'instance_name', 'image'] |
| 36 | readonly_fields = ['ip', 'instance_name'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 37 | |
| 38 | class SiteInline(admin.TabularInline): |
| 39 | model = Site |
| 40 | extra = 0 |
| 41 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 42 | class UserInline(admin.TabularInline): |
| 43 | model = User |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 44 | fields = ['email', 'firstname', 'lastname'] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 45 | extra = 0 |
| 46 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 47 | class SliceInline(admin.TabularInline): |
| 48 | model = Slice |
| 49 | extra = 0 |
| 50 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 51 | class RoleInline(admin.TabularInline): |
| 52 | model = Role |
| 53 | extra = 0 |
| 54 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 55 | class NodeInline(admin.TabularInline): |
| 56 | model = Node |
| 57 | extra = 0 |
| 58 | |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 59 | class SitePrivilegeInline(admin.TabularInline): |
| 60 | model = SitePrivilege |
| 61 | extra = 0 |
| 62 | |
| 63 | class SliceMembershipInline(admin.TabularInline): |
| 64 | model = SliceMembership |
| 65 | extra = 0 |
| 66 | |
Tony Mack | 5e71a66 | 2013-05-03 23:30:41 -0400 | [diff] [blame] | 67 | class PlainTextWidget(forms.HiddenInput): |
| 68 | input_type = 'hidden' |
| 69 | |
| 70 | def render(self, name, value, attrs=None): |
| 71 | if value is None: |
| 72 | value = '' |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 73 | return mark_safe(str(value) + super(PlainTextWidget, self).render(name, value, attrs)) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 74 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 75 | class PlanetStackBaseAdmin(admin.ModelAdmin): |
| 76 | save_on_top = False |
| 77 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 78 | class OSModelAdmin(PlanetStackBaseAdmin): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 79 | """Attach client connection to openstack on delete() and save()""" |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 80 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 81 | def save_model(self, request, obj, form, change): |
Tony Mack | 38e247c | 2013-05-05 11:48:14 -0400 | [diff] [blame] | 82 | if request.user.site: |
| 83 | auth = request.session.get('auth', {}) |
| 84 | auth['tenant'] = request.user.site.login_base |
| 85 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 86 | obj.save() |
| 87 | |
| 88 | def delete_model(self, request, obj): |
Tony Mack | 38e247c | 2013-05-05 11:48:14 -0400 | [diff] [blame] | 89 | if request.user.site: |
| 90 | auth = request.session.get('auth', {}) |
| 91 | auth['tenant'] = request.user.site.login_base |
| 92 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 93 | obj.delete() |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 94 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 95 | class RoleAdmin(OSModelAdmin): |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 96 | fieldsets = [ |
| 97 | ('Role', {'fields': ['role_type']}) |
| 98 | ] |
| 99 | list_display = ('role_type',) |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 100 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 101 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 102 | class DeploymentNetworkAdminForm(forms.ModelForm): |
| 103 | sites = forms.ModelMultipleChoiceField( |
| 104 | queryset=Site.objects.all(), |
| 105 | required=False, |
| 106 | widget=FilteredSelectMultiple( |
| 107 | verbose_name=('Sites'), is_stacked=False |
| 108 | ) |
| 109 | ) |
| 110 | class Meta: |
| 111 | model = DeploymentNetwork |
| 112 | |
| 113 | def __init__(self, *args, **kwargs): |
| 114 | super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs) |
| 115 | |
| 116 | if self.instance and self.instance.pk: |
| 117 | self.fields['sites'].initial = self.instance.sites.all() |
| 118 | |
| 119 | def save(self, commit=True): |
| 120 | deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 121 | if commit: |
| 122 | deploymentNetwork.save() |
| 123 | |
| 124 | if deploymentNetwork.pk: |
| 125 | deploymentNetwork.sites = self.cleaned_data['sites'] |
| 126 | self.save_m2m() |
| 127 | |
| 128 | return deploymentNetwork |
| 129 | |
| 130 | class DeploymentNetworkAdmin(PlanetStackBaseAdmin): |
| 131 | form = DeploymentNetworkAdminForm |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 132 | inlines = [NodeInline,SliverInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 133 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 134 | def get_formsets(self, request, obj=None): |
| 135 | for inline in self.get_inline_instances(request, obj): |
| 136 | # hide MyInline in the add view |
| 137 | if obj is None: |
| 138 | continue |
| 139 | # give inline object access to driver and caller |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 140 | auth = request.session.get('auth', {}) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 141 | if request.user.site: |
| 142 | auth['tenant'] = request.user.site.login_base |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 143 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 144 | yield inline.get_formset(request, obj) |
| 145 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 146 | class SiteAdmin(OSModelAdmin): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 147 | fieldsets = [ |
| 148 | (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}), |
| 149 | ('Location', {'fields': ['latitude', 'longitude']}), |
| 150 | ('Deployment Networks', {'fields': ['deployments']}) |
| 151 | ] |
| 152 | list_display = ('name', 'login_base','site_url', 'enabled') |
| 153 | filter_horizontal = ('deployments',) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 154 | inlines = [NodeInline, UserInline, SitePrivilegeInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 155 | search_fields = ['name'] |
| 156 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 157 | def queryset(self, request): |
| 158 | # admins can see all keys. Users can only see sites they belong to. |
| 159 | qs = super(SiteAdmin, self).queryset(request) |
| 160 | if not request.user.is_admin: |
| 161 | valid_sites = [request.user.site.login_base] |
| 162 | roles = request.user.get_roles() |
| 163 | for tenant_list in roles.values(): |
| 164 | valid_sites.extend(tenant_list) |
| 165 | qs = qs.filter(login_base__in=valid_sites) |
| 166 | return qs |
| 167 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 168 | def get_formsets(self, request, obj=None): |
| 169 | for inline in self.get_inline_instances(request, obj): |
| 170 | # hide MyInline in the add view |
| 171 | if obj is None: |
| 172 | continue |
| 173 | # give inline object access to driver and caller |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 174 | auth = request.session.get('auth', {}) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 175 | #auth['tenant'] = request.user.site.login_base |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 176 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 177 | yield inline.get_formset(request, obj) |
| 178 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 179 | class SitePrivilegeAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 180 | fieldsets = [ |
| 181 | (None, {'fields': ['user', 'site', 'role']}) |
| 182 | ] |
| 183 | list_display = ('user', 'site', 'role') |
| 184 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 185 | def queryset(self, request): |
| 186 | # admins can see all privileges. Users can only see privileges at sites |
| 187 | # where they have the admin role. |
| 188 | qs = super(SitePrivilegeAdmin, self).queryset(request) |
| 189 | if not request.user.is_admin: |
| 190 | roles = request.user.get_roles() |
| 191 | tenants = [] |
| 192 | for (role, tenant_list) in roles: |
| 193 | if role == 'admin': |
| 194 | tenants.extend(tenant_list) |
| 195 | valid_sites = Sites.objects.filter(login_base__in=tenants) |
| 196 | qs = qs.filter(site__in=valid_sites) |
| 197 | return qs |
| 198 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 199 | def save_model(self, request, obj, form, change): |
| 200 | # update openstack connection to use this site/tenant |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 201 | auth = request.session.get('auth', {}) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 202 | #auth['tenant'] = obj.site.login_base |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 203 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 204 | obj.save() |
| 205 | |
| 206 | def delete_model(self, request, obj): |
| 207 | # update openstack connection to use this site/tenant |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 208 | auth = request.session.get('auth', {}) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 209 | #auth['tenant'] = obj.site.login_base |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 210 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 211 | obj.delete() |
| 212 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 213 | class KeyAdmin(OSModelAdmin): |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 214 | fieldsets = [ |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 215 | ('Key', {'fields': ['key', 'type', 'blacklisted']}) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 216 | ] |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 217 | list_display = ['key', 'type', 'blacklisted'] |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 218 | |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 219 | #def queryset(self, request): |
Tony Mack | c14de8f | 2013-05-09 21:44:17 -0400 | [diff] [blame] | 220 | # admins can see all keys. Users can only see their own key. |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 221 | #if request.user.is_admin: |
| 222 | # qs = super(KeyAdmin, self).queryset(request) |
| 223 | #else: |
| 224 | # qs = Key.objects.filter(user=request.user) |
| 225 | #return qs |
Tony Mack | 956104d | 2013-04-27 12:36:19 -0400 | [diff] [blame] | 226 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 227 | class SliceAdmin(OSModelAdmin): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 228 | fields = ['name', 'site', 'serviceClass', 'description', 'slice_url'] |
| 229 | list_display = ('name', 'site','serviceClass', 'slice_url') |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 230 | inlines = [SliverInline, SliceMembershipInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 231 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 232 | def queryset(self, request): |
| 233 | # admins can see all keys. Users can only see slices they belong to. |
| 234 | qs = super(SliceAdmin, self).queryset(request) |
| 235 | if not request.user.is_admin: |
| 236 | valid_slices = [] |
| 237 | roles = request.user.get_roles() |
| 238 | for tenant_list in roles.values(): |
| 239 | valid_slices.extend(tenant_list) |
| 240 | qs = qs.filter(name__in=valid_slices) |
| 241 | return qs |
| 242 | |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 243 | def get_formsets(self, request, obj=None): |
| 244 | for inline in self.get_inline_instances(request, obj): |
| 245 | # hide MyInline in the add view |
| 246 | if obj is None: |
| 247 | continue |
| 248 | # give inline object access to driver and caller |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 249 | auth = request.session.get('auth', {}) |
| 250 | auth['tenant'] = obj.name # meed to connect using slice's tenant |
| 251 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 252 | yield inline.get_formset(request, obj) |
| 253 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 254 | def get_queryset(self, request): |
| 255 | qs = super(SliceAdmin, self).get_queryset(request) |
| 256 | if request.user.is_superuser: |
| 257 | return qs |
| 258 | # users can only see slices at their site |
| 259 | return qs.filter(site=request.user.site) |
| 260 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 261 | class SliceMembershipAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 262 | fieldsets = [ |
| 263 | (None, {'fields': ['user', 'slice', 'role']}) |
| 264 | ] |
| 265 | list_display = ('user', 'slice', 'role') |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 266 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 267 | def queryset(self, request): |
| 268 | # admins can see all memberships. Users can only see memberships of |
| 269 | # slices where they have the admin role. |
| 270 | qs = super(SliceMembershipAdmin, self).queryset(request) |
| 271 | if not request.user.is_admin: |
| 272 | roles = request.user.get_roles() |
| 273 | tenants = [] |
| 274 | for (role, tenant_list) in roles: |
| 275 | if role == 'admin': |
| 276 | tenants.extend(tenant_list) |
| 277 | valid_slices = Slice.objects.filter(name__in=tenants) |
| 278 | qs = qs.filter(slice__in=valid_slices) |
| 279 | return qs |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 280 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 281 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 282 | # update openstack connection to use this site/tenant |
| 283 | auth = request.session.get('auth', {}) |
| 284 | auth['tenant'] = obj.slice.name |
| 285 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 286 | obj.save() |
| 287 | |
| 288 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 289 | # update openstack connection to use this site/tenant |
| 290 | auth = request.session.get('auth', {}) |
| 291 | auth['tenant'] = obj.slice.name |
| 292 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 293 | obj.delete() |
| 294 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 295 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 296 | class ImageAdmin(admin.ModelAdmin): |
| 297 | fields = ['image_id', 'name', 'disk_format', 'container_format'] |
| 298 | |
| 299 | class NodeAdmin(admin.ModelAdmin): |
| 300 | list_display = ('name', 'site', 'deploymentNetwork') |
| 301 | list_filter = ('deploymentNetwork',) |
| 302 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 303 | |
| 304 | class SliverForm(forms.ModelForm): |
| 305 | class Meta: |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 306 | model = Sliver |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 307 | ip = forms.CharField(widget=PlainTextWidget) |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 308 | instance_name = forms.CharField(widget=PlainTextWidget) |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 309 | widgets = { |
| 310 | 'ip': PlainTextWidget(), |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 311 | 'instance_name': PlainTextWidget(), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 312 | } |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 313 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 314 | class SliverAdmin(PlanetStackBaseAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 315 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 316 | fieldsets = [ |
Tony Mack | 1008202 | 2013-05-06 17:15:00 -0400 | [diff] [blame] | 317 | ('Sliver', {'fields': ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']}) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 318 | ] |
Tony Mack | 1008202 | 2013-05-06 17:15:00 -0400 | [diff] [blame] | 319 | list_display = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork'] |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 320 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 321 | def queryset(self, request): |
| 322 | # admins can see all slivers. Users can only see slivers of |
| 323 | # the slices they belong to. |
| 324 | qs = super(SliverAdmin, self).queryset(request) |
| 325 | if not request.user.is_admin: |
| 326 | tenants = [] |
| 327 | roles = request.user.get_roles() |
| 328 | for tenant_list in roles.values(): |
| 329 | tenants.extend(tenant_list) |
| 330 | valid_slices = Slice.objects.filter(name__in=tenants) |
| 331 | qs = qs.filter(slice__in=valid_slices) |
| 332 | return qs |
| 333 | |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 334 | def get_formsets(self, request, obj=None): |
| 335 | # make some fields read only if we are updating an existing record |
| 336 | if obj == None: |
| 337 | #self.readonly_fields = ('ip', 'instance_name') |
| 338 | self.readonly_fields = () |
| 339 | else: |
Tony Mack | 1e88946 | 2013-05-10 21:34:54 -0400 | [diff] [blame] | 340 | self.readonly_fields = () |
| 341 | #self.readonly_fields = ('ip', 'instance_name', 'slice', 'image', 'key') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 342 | |
| 343 | for inline in self.get_inline_instances(request, obj): |
| 344 | # hide MyInline in the add view |
| 345 | if obj is None: |
| 346 | continue |
| 347 | # give inline object access to driver and caller |
| 348 | auth = request.session.get('auth', {}) |
| 349 | auth['tenant'] = obj.name # meed to connect using slice's tenant |
| 350 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 351 | yield inline.get_formset(request, obj) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 352 | |
| 353 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 354 | # update openstack connection to use this site/tenant |
| 355 | auth = request.session.get('auth', {}) |
| 356 | auth['tenant'] = obj.slice.name |
| 357 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 358 | obj.save() |
| 359 | |
| 360 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 361 | # update openstack connection to use this site/tenant |
| 362 | auth = request.session.get('auth', {}) |
| 363 | auth['tenant'] = obj.slice.name |
| 364 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 365 | obj.delete() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 366 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 367 | class UserCreationForm(forms.ModelForm): |
| 368 | """A form for creating new users. Includes all the required |
| 369 | fields, plus a repeated password.""" |
| 370 | password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| 371 | password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| 372 | |
| 373 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 374 | model = User |
| 375 | fields = ('email', 'firstname', 'lastname', 'phone', 'key', 'site') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 376 | |
| 377 | def clean_password2(self): |
| 378 | # Check that the two password entries match |
| 379 | password1 = self.cleaned_data.get("password1") |
| 380 | password2 = self.cleaned_data.get("password2") |
| 381 | if password1 and password2 and password1 != password2: |
| 382 | raise forms.ValidationError("Passwords don't match") |
| 383 | return password2 |
| 384 | |
| 385 | def save(self, commit=True): |
| 386 | # Save the provided password in hashed format |
| 387 | user = super(UserCreationForm, self).save(commit=False) |
Tony Mack | f9f4afb | 2013-05-01 21:02:12 -0400 | [diff] [blame] | 388 | user.password = self.cleaned_data["password1"] |
| 389 | #user.set_password(self.cleaned_data["password1"]) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 390 | if commit: |
| 391 | user.save() |
| 392 | return user |
| 393 | |
| 394 | |
| 395 | class UserChangeForm(forms.ModelForm): |
| 396 | """A form for updating users. Includes all the fields on |
| 397 | the user, but replaces the password field with admin's |
| 398 | password hash display field. |
| 399 | """ |
| 400 | password = ReadOnlyPasswordHashField() |
| 401 | |
| 402 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 403 | model = User |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 404 | |
| 405 | def clean_password(self): |
| 406 | # Regardless of what the user provides, return the initial value. |
| 407 | # This is done here, rather than on the field, because the |
| 408 | # field does not have access to the initial value |
| 409 | return self.initial["password"] |
| 410 | |
| 411 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 412 | class UserAdmin(UserAdmin, OSModelAdmin): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 413 | class Meta: |
| 414 | app_label = "core" |
| 415 | |
| 416 | # The forms to add and change user instances |
| 417 | form = UserChangeForm |
| 418 | add_form = UserCreationForm |
| 419 | |
| 420 | # The fields to be used in displaying the User model. |
| 421 | # These override the definitions on the base UserAdmin |
| 422 | # that reference specific fields on auth.User. |
Tony Mack | 416c0f2 | 2013-05-09 16:59:09 -0400 | [diff] [blame] | 423 | list_display = ('email', 'site', 'firstname', 'lastname', 'is_admin', 'last_login') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 424 | list_filter = ('site',) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 425 | inlines = [SitePrivilegeInline, SliceMembershipInline] |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 426 | fieldsets = ( |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 427 | (None, {'fields': ('email', 'password', 'site', 'is_admin')}), |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 428 | ('Personal info', {'fields': ('firstname','lastname','phone', 'key')}), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 429 | #('Important dates', {'fields': ('last_login',)}), |
| 430 | ) |
| 431 | add_fieldsets = ( |
| 432 | (None, { |
| 433 | 'classes': ('wide',), |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 434 | 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'key','password1', 'password2', 'is_admin')} |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 435 | ), |
| 436 | ) |
| 437 | search_fields = ('email',) |
| 438 | ordering = ('email',) |
| 439 | filter_horizontal = () |
| 440 | |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 441 | class ServiceResourceInline(admin.TabularInline): |
| 442 | model = ServiceResource |
| 443 | extra = 0 |
| 444 | |
| 445 | class ServiceClassAdmin(admin.ModelAdmin): |
| 446 | list_display = ('name', 'commitment', 'membershipFee') |
| 447 | inlines = [ServiceResourceInline] |
| 448 | |
| 449 | class ServiceResourceAdmin(admin.ModelAdmin): |
| 450 | list_display = ('serviceClass', 'name', 'cost', 'calendarReservable', 'maxUnitsDeployment', 'maxUnitsNode') |
| 451 | |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 452 | # register a signal that caches the user's credentials when they log in |
| 453 | def cache_credentials(sender, user, request, **kwds): |
| 454 | auth = {'username': request.POST['username'], |
| 455 | 'password': request.POST['password']} |
| 456 | request.session['auth'] = auth |
| 457 | user_logged_in.connect(cache_credentials) |
| 458 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 459 | # Now register the new UserAdmin... |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 460 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 461 | # ... and, since we're not using Django's builtin permissions, |
| 462 | # unregister the Group model from admin. |
| 463 | admin.site.unregister(Group) |
| 464 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 465 | admin.site.register(Site, SiteAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 466 | admin.site.register(SitePrivilege, SitePrivilegeAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 467 | admin.site.register(Slice, SliceAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 468 | admin.site.register(SliceMembership, SliceMembershipAdmin) |
| 469 | #admin.site.register(Subnet) |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 470 | admin.site.register(Image, ImageAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 471 | admin.site.register(Node, NodeAdmin) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 472 | admin.site.register(Sliver, SliverAdmin) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 473 | admin.site.register(Key, KeyAdmin) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 474 | admin.site.register(Role, RoleAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 475 | admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin) |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 476 | admin.site.register(ServiceClass, ServiceClassAdmin) |
| 477 | admin.site.register(ServiceResource, ServiceResourceAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 478 | |