admin.py 3.6 KB

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