Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | from plstackapi.core.models import Site |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 2 | from plstackapi.core.models import * |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 3 | from plstackapi.openstack.manager import OpenStackManager |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 4 | from plstackapi.openstack.driver import OpenStackDriver |
| 5 | from plstackapi.openstack.client import OpenStackClient |
| 6 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 7 | from django.contrib import admin |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 8 | from django.contrib.auth.models import Group |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 9 | from django import forms |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 10 | from django.utils.safestring import mark_safe |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 11 | from django.contrib.auth.admin import UserAdmin |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 12 | from django.contrib.admin.widgets import FilteredSelectMultiple |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 13 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 14 | from django.contrib.auth.signals import user_logged_in |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 15 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 16 | |
| 17 | class ReadonlyTabularInline(admin.TabularInline): |
| 18 | can_delete = False |
| 19 | extra = 0 |
| 20 | editable_fields = [] |
| 21 | |
| 22 | def get_readonly_fields(self, request, obj=None): |
| 23 | fields = [] |
| 24 | for field in self.model._meta.get_all_field_names(): |
| 25 | if (not field == 'id'): |
| 26 | if (field not in self.editable_fields): |
| 27 | fields.append(field) |
| 28 | return fields |
| 29 | |
| 30 | def has_add_permission(self, request): |
| 31 | return False |
| 32 | |
| 33 | class SliverInline(admin.TabularInline): |
| 34 | model = Sliver |
Siobhan Tully | d1cd5df | 2013-05-02 06:02:36 -0400 | [diff] [blame] | 35 | fields = ['ip', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 36 | extra = 0 |
| 37 | |
| 38 | class SiteInline(admin.TabularInline): |
| 39 | model = Site |
| 40 | extra = 0 |
| 41 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 42 | class SliceInline(admin.TabularInline): |
| 43 | model = Slice |
| 44 | extra = 0 |
| 45 | |
| 46 | class UserInline(admin.TabularInline): |
| 47 | model = PLUser |
| 48 | extra = 0 |
| 49 | |
| 50 | class RoleInline(admin.TabularInline): |
| 51 | model = Role |
| 52 | extra = 0 |
| 53 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 54 | class NodeInline(admin.TabularInline): |
| 55 | model = Node |
| 56 | extra = 0 |
| 57 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 58 | class PlainTextWidget(forms.Widget): |
| 59 | def render(self, _name, value, attrs): |
| 60 | return mark_safe(value) if value is not None else '' |
| 61 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 62 | class PlanetStackBaseAdmin(admin.ModelAdmin): |
| 63 | save_on_top = False |
| 64 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 65 | class OSModelAdmin(PlanetStackBaseAdmin): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 66 | """Attach client connection to openstack on delete() and save()""" |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 67 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 68 | def save_model(self, request, obj, form, change): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 69 | auth = request.session.get('auth', {}) |
| 70 | auth['tenant'] = request.user.site.login_base |
| 71 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 72 | obj.save() |
| 73 | |
| 74 | def delete_model(self, request, obj): |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 75 | auth = request.session.get('auth', {}) |
| 76 | auth['tenant'] = request.user.site.login_base |
| 77 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 78 | obj.delete() |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 79 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 80 | class RoleAdmin(OSModelAdmin): |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 81 | fieldsets = [ |
| 82 | ('Role', {'fields': ['role_type']}) |
| 83 | ] |
| 84 | list_display = ('role_type',) |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 85 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 86 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 87 | class DeploymentNetworkAdminForm(forms.ModelForm): |
| 88 | sites = forms.ModelMultipleChoiceField( |
| 89 | queryset=Site.objects.all(), |
| 90 | required=False, |
| 91 | widget=FilteredSelectMultiple( |
| 92 | verbose_name=('Sites'), is_stacked=False |
| 93 | ) |
| 94 | ) |
| 95 | class Meta: |
| 96 | model = DeploymentNetwork |
| 97 | |
| 98 | def __init__(self, *args, **kwargs): |
| 99 | super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs) |
| 100 | |
| 101 | if self.instance and self.instance.pk: |
| 102 | self.fields['sites'].initial = self.instance.sites.all() |
| 103 | |
| 104 | def save(self, commit=True): |
| 105 | deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 106 | if commit: |
| 107 | deploymentNetwork.save() |
| 108 | |
| 109 | if deploymentNetwork.pk: |
| 110 | deploymentNetwork.sites = self.cleaned_data['sites'] |
| 111 | self.save_m2m() |
| 112 | |
| 113 | return deploymentNetwork |
| 114 | |
| 115 | class DeploymentNetworkAdmin(PlanetStackBaseAdmin): |
| 116 | form = DeploymentNetworkAdminForm |
| 117 | inlines = [NodeInline,] |
| 118 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 119 | def get_formsets(self, request, obj=None): |
| 120 | for inline in self.get_inline_instances(request, obj): |
| 121 | # hide MyInline in the add view |
| 122 | if obj is None: |
| 123 | continue |
| 124 | # give inline object access to driver and caller |
| 125 | client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {})) |
| 126 | inline.model.driver = OpenStackDriver(client=client) |
| 127 | inline.model.caller = request.user |
| 128 | yield inline.get_formset(request, obj) |
| 129 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 130 | class SiteAdmin(OSModelAdmin): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 131 | fieldsets = [ |
| 132 | (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}), |
| 133 | ('Location', {'fields': ['latitude', 'longitude']}), |
| 134 | ('Deployment Networks', {'fields': ['deployments']}) |
| 135 | ] |
| 136 | list_display = ('name', 'login_base','site_url', 'enabled') |
| 137 | filter_horizontal = ('deployments',) |
| 138 | inlines = [NodeInline,] |
| 139 | search_fields = ['name'] |
| 140 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 141 | def get_formsets(self, request, obj=None): |
| 142 | for inline in self.get_inline_instances(request, obj): |
| 143 | # hide MyInline in the add view |
| 144 | if obj is None: |
| 145 | continue |
| 146 | # give inline object access to driver and caller |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 147 | auth = request.session.get('auth', {}) |
| 148 | auth['tenant'] = request.user.site.login_base |
| 149 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 150 | yield inline.get_formset(request, obj) |
| 151 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 152 | class SitePrivilegeAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 153 | fieldsets = [ |
| 154 | (None, {'fields': ['user', 'site', 'role']}) |
| 155 | ] |
| 156 | list_display = ('user', 'site', 'role') |
| 157 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 158 | def save_model(self, request, obj, form, change): |
| 159 | # update openstack connection to use this site/tenant |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 160 | auth = request.session.get('auth', {}) |
| 161 | auth['tenant'] = obj.site.login_base |
| 162 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 163 | obj.save() |
| 164 | |
| 165 | def delete_model(self, request, obj): |
| 166 | # update openstack connection to use this site/tenant |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 167 | auth = request.session.get('auth', {}) |
| 168 | auth['tenant'] = obj.site.login_base |
| 169 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 170 | obj.delete() |
| 171 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 172 | class KeyAdmin(OSModelAdmin): |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 173 | fieldsets = [ |
| 174 | ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']}) |
| 175 | ] |
| 176 | list_display = ['name', 'key', 'type', 'blacklisted', 'user'] |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 177 | |
Tony Mack | 956104d | 2013-04-27 12:36:19 -0400 | [diff] [blame] | 178 | def get_queryset(self, request): |
| 179 | # get keys user is allowed to see |
| 180 | qs = super(KeyAdmin, self).get_queryset(request) |
| 181 | if request.user.is_superuser: |
| 182 | return qs |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 183 | # users can only see their own keys |
Tony Mack | 956104d | 2013-04-27 12:36:19 -0400 | [diff] [blame] | 184 | return qs.filter(user=request.user) |
| 185 | |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 186 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 187 | class SliceAdmin(OSModelAdmin): |
Siobhan Tully | f791406 | 2013-05-02 05:50:30 -0400 | [diff] [blame] | 188 | fields = ['name', 'site', 'serviceClass', 'instantiation', 'description', 'slice_url'] |
| 189 | list_display = ('name', 'site','serviceClass', 'slice_url', 'instantiation') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 190 | inlines = [SliverInline] |
| 191 | |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 192 | def get_formsets(self, request, obj=None): |
| 193 | for inline in self.get_inline_instances(request, obj): |
| 194 | # hide MyInline in the add view |
| 195 | if obj is None: |
| 196 | continue |
| 197 | # give inline object access to driver and caller |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 198 | auth = request.session.get('auth', {}) |
| 199 | auth['tenant'] = obj.name # meed to connect using slice's tenant |
| 200 | inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 201 | yield inline.get_formset(request, obj) |
| 202 | |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 203 | def get_queryset(self, request): |
| 204 | qs = super(SliceAdmin, self).get_queryset(request) |
| 205 | if request.user.is_superuser: |
| 206 | return qs |
| 207 | # users can only see slices at their site |
| 208 | return qs.filter(site=request.user.site) |
| 209 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 210 | class SliceMembershipAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 211 | fieldsets = [ |
| 212 | (None, {'fields': ['user', 'slice', 'role']}) |
| 213 | ] |
| 214 | list_display = ('user', 'slice', 'role') |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 215 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 216 | def save_model(self, request, obj, form, change): |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 217 | # update openstack connection to use this site/tenant |
| 218 | auth = request.session.get('auth', {}) |
| 219 | auth['tenant'] = obj.slice.name |
| 220 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 221 | obj.save() |
| 222 | |
| 223 | def delete_model(self, request, obj): |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 224 | # update openstack connection to use this site/tenant |
| 225 | auth = request.session.get('auth', {}) |
| 226 | auth['tenant'] = obj.slice.name |
| 227 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 228 | obj.delete() |
| 229 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 230 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 231 | class SubnetAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 232 | fields = ['cidr', 'ip_version', 'start', 'end', 'slice'] |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 233 | list_display = ('slice','cidr', 'start', 'end', 'ip_version') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 234 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 235 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 236 | # update openstack connection to use this site/tenant |
| 237 | auth = request.session.get('auth', {}) |
| 238 | auth['tenant'] = obj.slice.name |
| 239 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 240 | obj.save() |
| 241 | |
| 242 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 243 | # update openstack connection to use this site/tenant |
| 244 | auth = request.session.get('auth', {}) |
| 245 | auth['tenant'] = obj.slice.name |
| 246 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 247 | obj.delete() |
| 248 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 249 | class ImageAdmin(admin.ModelAdmin): |
| 250 | fields = ['image_id', 'name', 'disk_format', 'container_format'] |
| 251 | |
| 252 | class NodeAdmin(admin.ModelAdmin): |
| 253 | list_display = ('name', 'site', 'deploymentNetwork') |
| 254 | list_filter = ('deploymentNetwork',) |
| 255 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 256 | |
| 257 | class SliverForm(forms.ModelForm): |
| 258 | class Meta: |
| 259 | ip = forms.CharField(widget=PlainTextWidget) |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 260 | instance_name = forms.CharField(widget=PlainTextWidget) |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 261 | model = Sliver |
| 262 | widgets = { |
| 263 | 'ip': PlainTextWidget(), |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 264 | 'instance_name': PlainTextWidget(), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 265 | } |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 266 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 267 | class SliverAdmin(PlanetStackBaseAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 268 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 269 | fieldsets = [ |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 270 | ('Sliver', {'fields': ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']}) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 271 | ] |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 272 | list_display = ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork'] |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 273 | |
| 274 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame^] | 275 | # update openstack connection to use this site/tenant |
| 276 | auth = request.session.get('auth', {}) |
| 277 | auth['tenant'] = obj.slice.name |
| 278 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 279 | obj.save() |
| 280 | |
| 281 | def delete_model(self, request, obj): |
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 | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 286 | obj.delete() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 287 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 288 | class UserCreationForm(forms.ModelForm): |
| 289 | """A form for creating new users. Includes all the required |
| 290 | fields, plus a repeated password.""" |
| 291 | password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| 292 | password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| 293 | |
| 294 | class Meta: |
| 295 | model = PLUser |
| 296 | fields = ('email', 'firstname', 'lastname', 'phone', 'site') |
| 297 | |
| 298 | def clean_password2(self): |
| 299 | # Check that the two password entries match |
| 300 | password1 = self.cleaned_data.get("password1") |
| 301 | password2 = self.cleaned_data.get("password2") |
| 302 | if password1 and password2 and password1 != password2: |
| 303 | raise forms.ValidationError("Passwords don't match") |
| 304 | return password2 |
| 305 | |
| 306 | def save(self, commit=True): |
| 307 | # Save the provided password in hashed format |
| 308 | user = super(UserCreationForm, self).save(commit=False) |
Tony Mack | f9f4afb | 2013-05-01 21:02:12 -0400 | [diff] [blame] | 309 | user.password = self.cleaned_data["password1"] |
| 310 | #user.set_password(self.cleaned_data["password1"]) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 311 | if commit: |
| 312 | user.save() |
| 313 | return user |
| 314 | |
| 315 | |
| 316 | class UserChangeForm(forms.ModelForm): |
| 317 | """A form for updating users. Includes all the fields on |
| 318 | the user, but replaces the password field with admin's |
| 319 | password hash display field. |
| 320 | """ |
| 321 | password = ReadOnlyPasswordHashField() |
| 322 | |
| 323 | class Meta: |
| 324 | model = PLUser |
| 325 | |
| 326 | def clean_password(self): |
| 327 | # Regardless of what the user provides, return the initial value. |
| 328 | # This is done here, rather than on the field, because the |
| 329 | # field does not have access to the initial value |
| 330 | return self.initial["password"] |
| 331 | |
| 332 | |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 333 | class PLUserAdmin(UserAdmin, OSModelAdmin): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 334 | class Meta: |
| 335 | app_label = "core" |
| 336 | |
| 337 | # The forms to add and change user instances |
| 338 | form = UserChangeForm |
| 339 | add_form = UserCreationForm |
| 340 | |
| 341 | # The fields to be used in displaying the User model. |
| 342 | # These override the definitions on the base UserAdmin |
| 343 | # that reference specific fields on auth.User. |
| 344 | list_display = ('email', 'site', 'firstname', 'lastname', 'last_login') |
| 345 | list_filter = ('site',) |
| 346 | fieldsets = ( |
| 347 | (None, {'fields': ('email', 'password')}), |
| 348 | ('Personal info', {'fields': ('firstname','lastname','phone','site')}), |
| 349 | #('Important dates', {'fields': ('last_login',)}), |
| 350 | ) |
| 351 | add_fieldsets = ( |
| 352 | (None, { |
| 353 | 'classes': ('wide',), |
| 354 | 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')} |
| 355 | ), |
| 356 | ) |
| 357 | search_fields = ('email',) |
| 358 | ordering = ('email',) |
| 359 | filter_horizontal = () |
| 360 | |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 361 | # register a signal that caches the user's credentials when they log in |
| 362 | def cache_credentials(sender, user, request, **kwds): |
| 363 | auth = {'username': request.POST['username'], |
| 364 | 'password': request.POST['password']} |
| 365 | request.session['auth'] = auth |
| 366 | user_logged_in.connect(cache_credentials) |
| 367 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 368 | # Now register the new UserAdmin... |
| 369 | admin.site.register(PLUser, PLUserAdmin) |
| 370 | # ... and, since we're not using Django's builtin permissions, |
| 371 | # unregister the Group model from admin. |
| 372 | admin.site.unregister(Group) |
| 373 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 374 | admin.site.register(Site, SiteAdmin) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 375 | admin.site.register(SitePrivilege, SitePrivilegeAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 376 | admin.site.register(Slice, SliceAdmin) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 377 | admin.site.register(SliceMembership, SliceMembershipAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 378 | admin.site.register(Subnet, SubnetAdmin) |
| 379 | admin.site.register(Image, ImageAdmin) |
| 380 | admin.site.register(Node, NodeAdmin) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 381 | admin.site.register(Sliver, SliverAdmin) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 382 | admin.site.register(Key, KeyAdmin) |
Tony Mack | fd24d0d | 2013-04-14 00:59:17 -0400 | [diff] [blame] | 383 | admin.site.register(Role, RoleAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 384 | admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 385 | |