Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. from django import forms
  2. from django.contrib import admin
  3. from django.contrib.auth.models import Group
  4. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  5. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  6.  
  7. from customauth.models import MyUser
  8.  
  9.  
  10. class UserCreationForm(forms.ModelForm):
  11. """A form for creating new users. Includes all the required
  12. fields, plus a repeated password."""
  13. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  14. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  15.  
  16. class Meta:
  17. model = MyUser
  18. fields = ('email', 'date_of_birth')
  19.  
  20. def clean_password2(self):
  21. # Check that the two password entries match
  22. password1 = self.cleaned_data.get("password1")
  23. password2 = self.cleaned_data.get("password2")
  24. if password1 and password2 and password1 != password2:
  25. raise forms.ValidationError("Passwords don't match")
  26. return password2
  27.  
  28. def save(self, commit=True):
  29. # Save the provided password in hashed format
  30. user = super().save(commit=False)
  31. user.set_password(self.cleaned_data["password1"])
  32. if commit:
  33. user.save()
  34. return user
  35.  
  36.  
  37. class UserChangeForm(forms.ModelForm):
  38. """A form for updating users. Includes all the fields on
  39. the user, but replaces the password field with admin's
  40. password hash display field.
  41. """
  42. password = ReadOnlyPasswordHashField()
  43.  
  44. class Meta:
  45. model = MyUser
  46. fields = ('email', 'password', 'date_of_birth', 'is_active', 'is_admin')
  47.  
  48. def clean_password(self):
  49. # Regardless of what the user provides, return the initial value.
  50. # This is done here, rather than on the field, because the
  51. # field does not have access to the initial value
  52. return self.initial["password"]
  53.  
  54.  
  55. class UserAdmin(BaseUserAdmin):
  56. # The forms to add and change user instances
  57. form = UserChangeForm
  58. add_form = UserCreationForm
  59.  
  60. # The fields to be used in displaying the User model.
  61. # These override the definitions on the base UserAdmin
  62. # that reference specific fields on auth.User.
  63. list_display = ('email', 'date_of_birth', 'is_admin')
  64. list_filter = ('is_admin',)
  65. fieldsets = (
  66. (None, {'fields': ('email', 'password')}),
  67. ('Personal info', {'fields': ('date_of_birth',)}),
  68. ('Permissions', {'fields': ('is_admin',)}),
  69. )
  70. # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
  71. # overrides get_fieldsets to use this attribute when creating a user.
  72. add_fieldsets = (
  73. (None, {
  74. 'classes': ('wide',),
  75. 'fields': ('email', 'date_of_birth', 'password1', 'password2')}
  76. ),
  77. )
  78. search_fields = ('email',)
  79. ordering = ('email',)
  80. filter_horizontal = ()
  81.  
  82. # Now register the new UserAdmin...
  83. admin.site.register(MyUser, UserAdmin)
  84. # ... and, since we're not using Django's built-in permissions,
  85. # unregister the Group model from admin.
  86. admin.site.unregister(Group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement