Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. from django import forms
  2. from django.contrib import admin
  3. from django.utils.translation import ugettext as _
  4. from django.contrib.auth.admin import UserAdmin
  5. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  6.  
  7. from .models import User
  8.  
  9.  
  10. class UserCreationForm(forms.ModelForm):
  11. password1 = forms.CharField(
  12. label='Password',
  13. widget=forms.PasswordInput)
  14. password2 = forms.CharField(
  15. label='Password confirmation',
  16. widget=forms.PasswordInput)
  17.  
  18. class Meta:
  19. model = User
  20. fields = ('email',)
  21.  
  22. def clean_password2(self):
  23. # Check that the two password entries match
  24. password1 = self.cleaned_data.get('password1')
  25. password2 = self.cleaned_data.get('password2')
  26. if password1 and password2 and password1 != password2:
  27. msg = "Passwords don't match"
  28. raise forms.ValidationError(msg)
  29. return password2
  30.  
  31. def save(self, commit=True):
  32. user = super(UserCreationForm, self).save(commit=False)
  33.  
  34. user.set_password(self.cleaned_data['password1'])
  35. if commit:
  36. user.save()
  37. return user
  38.  
  39.  
  40. class UserChangeForm(forms.ModelForm):
  41. password = ReadOnlyPasswordHashField(
  42. label=_("Password"),
  43. help_text=_("Raw passwords are not stored, so there is no way to see "
  44. "this user's password, but you can change the password "
  45. "using <a href=\"password/\">this form</a>."))
  46.  
  47. class Meta:
  48. model = User
  49. fields = ('password',)
  50.  
  51. def clean_password(self):
  52. return self.initial["password"]
  53.  
  54.  
  55. class CustomUserAdmin(UserAdmin):
  56. add_form = UserCreationForm
  57. form = UserChangeForm
  58.  
  59. list_display = ('id', 'email')
  60. list_filter = ('is_superuser', 'groups')
  61. search_fields = ('email',)
  62. ordering = ('email',)
  63. filter_horizontal = ('groups', 'user_permissions',)
  64.  
  65. fieldsets = (
  66. (None, {'fields': ('email', 'password')}),
  67. ('Info', {'fields': ('first_name', 'last_name', 'headshot',)}),
  68. ('Permissions', {'fields': ('is_active',
  69. 'is_superuser',
  70. 'is_staff',
  71. 'groups',
  72. 'user_permissions')}),
  73. )
  74.  
  75. add_fieldsets = (
  76. (None, {
  77. 'classes': ('wide',),
  78. 'fields': ('email', 'password1', 'password2')}),
  79. )
  80.  
  81. admin.site.register(User, CustomUserAdmin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement