admin.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.contrib import admin
  2. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  3. from django.contrib.auth.models import Group
  4. from django import forms
  5. from .models import User
  6. class UserCreationForm(forms.ModelForm):
  7. """A form for creating new users. Includes all the required
  8. fields, plus a repeated password."""
  9. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  10. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  11. class Meta:
  12. model = User
  13. fields = ('email', 'age', 'name')
  14. def clean_password2(self):
  15. # Check that the two password entries match
  16. password1 = self.cleaned_data.get("password1")
  17. password2 = self.cleaned_data.get("password2")
  18. if password1 and password2 and password1 != password2:
  19. raise forms.ValidationError("Passwords don't match")
  20. return password2
  21. def save(self, commit=True):
  22. # Save the provided password in hashed format
  23. user = super(UserCreationForm, self).save(commit=False)
  24. user.set_password(self.cleaned_data["password1"])
  25. if commit:
  26. user.save()
  27. return user
  28. class UserChangeForm(forms.ModelForm):
  29. """A form for updating users. Includes all the fields on
  30. the user, but replaces the password field with admin's
  31. password hash display field.
  32. """
  33. password = ReadOnlyPasswordHashField(label=("Password"),
  34. help_text=("Raw passwords are not stored, so there is no way to see "
  35. "this user's password, but you can change the password "
  36. "using <a href=\"password/\">this form</a>."))
  37. class Meta:
  38. model = User
  39. fields = ('email', 'age', 'name','password')
  40. def clean_password(self):
  41. # Regardless of what the user provides, return the initial value.
  42. # This is done here, rather than on the field, because the
  43. # field does not have access to the initial value
  44. return self.initial["password"]
  45. class CustomUserAdmin(admin.ModelAdmin):
  46. # The forms to add and change user instances
  47. form = UserChangeForm
  48. add_form = UserCreationForm
  49. model = User
  50. # The fields to be used in displaying the User model.
  51. # These override the definitions on the base UserAdmin
  52. # that reference specific fields on auth.User.
  53. list_display = ('email', 'name', 'age', 'is_superuser')
  54. list_filter = ('is_superuser',)
  55. fieldsets = (
  56. (None, {'fields': ('email', 'password')}),
  57. ('Personal info', {'fields': ('name','age')}),
  58. )
  59. add_fieldsets = (
  60. (None, {
  61. 'classes': ('wide',),
  62. 'fields': ('email', 'name','age', 'password1', 'password2')}
  63. ),
  64. )
  65. search_fields = ('email',)
  66. ordering = ('email',)
  67. filter_horizontal = ()
  68. def get_fieldsets(self, request, obj=None):
  69. if not obj:
  70. return self.add_fieldsets
  71. return self.fieldsets
  72. def get_form(self, request, obj=None, change=False, **kwargs):
  73. if not obj:
  74. return self.add_form
  75. return self.form
  76. admin.site.register(User, CustomUserAdmin)
  77. admin.site.unregister(Group)