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