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