| author | ymh <ymh.work@gmail.com> |
| Thu, 07 Apr 2016 13:21:50 +0200 | |
| changeset 50 | c4a026852232 |
| parent 29 | 23de98e32b3b |
| child 60 | dee4eac69d84 |
| permissions | -rw-r--r-- |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
1 |
from django import forms |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
2 |
from django.contrib import admin |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
3 |
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
4 |
from django.contrib.auth.forms import ReadOnlyPasswordHashField |
|
50
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
5 |
from django.utils.translation import ugettext, ugettext_lazy as _ |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
6 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
7 |
from .models import User |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
8 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
9 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
10 |
class UserCreationForm(forms.ModelForm): |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
11 |
"""A form for creating new users. Includes all the required |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
12 |
fields, plus a repeated password.""" |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
13 |
password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
14 |
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
15 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
16 |
class Meta: |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
17 |
model = User |
|
29
23de98e32b3b
added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
durandn
parents:
11
diff
changeset
|
18 |
fields = ('external_id', 'username', 'uai', 'first_name', 'last_name') |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
19 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
20 |
def clean_password2(self): |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
21 |
# Check that the two password entries match |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
22 |
password1 = self.cleaned_data.get("password1") |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
23 |
password2 = self.cleaned_data.get("password2") |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
24 |
if password1 and password2 and password1 != password2: |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
25 |
raise forms.ValidationError("Passwords don't match") |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
26 |
return password2 |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
27 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
28 |
def save(self, commit=True): |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
29 |
# Save the provided password in hashed format |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
30 |
user = super(UserCreationForm, self).save(commit=False) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
31 |
user.set_password(self.cleaned_data["password1"]) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
32 |
if commit: |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
33 |
user.save() |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
34 |
return user |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
35 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
36 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
37 |
class UserChangeForm(forms.ModelForm): |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
38 |
"""A form for updating users. Includes all the fields on |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
39 |
the user, but replaces the password field with admin's |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
40 |
password hash display field. |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
41 |
""" |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
42 |
password = ReadOnlyPasswordHashField() |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
43 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
44 |
class Meta: |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
45 |
model = User |
|
29
23de98e32b3b
added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
durandn
parents:
11
diff
changeset
|
46 |
fields = ('external_id', 'username', 'uai', 'first_name', 'last_name', 'password', 'is_active', 'is_staff') |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
47 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
48 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
49 |
class UserAdmin(BaseUserAdmin): |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
50 |
# The forms to add and change user instances |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
51 |
form = UserChangeForm |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
52 |
add_form = UserCreationForm |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
53 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
54 |
# The fields to be used in displaying the User model. |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
55 |
# These override the definitions on the base UserAdmin |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
56 |
# that reference specific fields on auth.User. |
|
29
23de98e32b3b
added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
durandn
parents:
11
diff
changeset
|
57 |
list_display = ('external_id', 'username', 'uai', 'first_name', 'last_name', 'password', 'is_staff') |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
58 |
list_filter = ('is_staff',) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
59 |
fieldsets = ( |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
60 |
(None, {'fields': ('external_id', 'username')}), |
|
50
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
61 |
(_('Personal info'), {'fields': ('username', 'uai', 'first_name', 'last_name')}), |
|
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
62 |
(_('Permissions'), {'fields': ('is_staff',)}), |
|
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
63 |
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', |
|
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
64 |
'groups', 'user_permissions')}), |
|
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
65 |
(_('Important dates'), {'fields': ('last_login', 'date_joined')}), |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
66 |
) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
67 |
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
68 |
# overrides get_fieldsets to use this attribute when creating a user. |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
69 |
add_fieldsets = ( |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
70 |
(None, { |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
71 |
'classes': ('wide',), |
|
29
23de98e32b3b
added uai field to user model and corresponding migrations + edited test oauth server to serve uai info for testing
durandn
parents:
11
diff
changeset
|
72 |
'fields': ('external_id', 'username', 'uai', 'first_name', 'last_name', 'password1', 'password2')} |
|
11
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
73 |
), |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
74 |
) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
75 |
search_fields = ('username',) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
76 |
ordering = ('username',) |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
77 |
filter_horizontal = () |
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
78 |
|
|
cfc868991b82
Added custom user model to store extra data from the GED + corrected signals and api auth class so they interface correctly with the ged
durandn
parents:
diff
changeset
|
79 |
# Now register the new UserAdmin |
|
50
c4a026852232
Correct crsf authorization problems + new version
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
80 |
admin.site.register(User, UserAdmin) |