Advertisement
Guest User

Untitled

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