Guest User

Untitled

a guest
Feb 10th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. from django import forms
  2. from django.contrib import admin
  3. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  4. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  5.  
  6.  
  7. from .models import ApiUser
  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 = ApiUser
  18. fields = (
  19. "email",
  20. )
  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. raise forms.ValidationError("Passwords don't match")
  28. return password2
  29.  
  30. def save(self, commit=True):
  31. # Save the provided password in hashed format
  32. user = super(UserCreationForm, self).save(commit=False)
  33. user.set_password(self.cleaned_data["password1"])
  34. if commit:
  35. user.save()
  36. return user
  37.  
  38.  
  39. class UserChangeForm(forms.ModelForm):
  40. """A form for updating users. Includes all the fields on
  41. the user, but replaces the password field with admin's
  42. password hash display field.
  43. """
  44. password = ReadOnlyPasswordHashField()
  45.  
  46. class Meta:
  47. model = ApiUser
  48. fields = ("email", "password", "is_active", "is_admin")
  49.  
  50. def clean_password(self):
  51. # Regardless of what the user provides, return the initial value.
  52. # This is done here, rather than on the field, because the
  53. # field does not have access to the initial value
  54. return self.initial["password"]
  55.  
  56.  
  57. class UserAdmin(BaseUserAdmin):
  58. # Forms for creating and editing users
  59. form = UserChangeForm
  60. add_form = UserCreationForm
  61.  
  62. list_display = ("email", "is_active", "is_admin")
  63. list_filter = ("is_active",)
  64.  
  65. # Specify how to display users in the admin console
  66. fieldsets = (
  67. (None, {"fields": ("email", "password")}),
  68. ("Permissions", {"fields": ("is_admin",)}),
  69. )
  70.  
  71. # add_fieldsets is not a standard ModelAdmin attribute. Django"s UserAdmin
  72. # overrides get_fieldsets to use this attribute when creating a user.
  73. add_fieldsets = (
  74. (
  75. None,
  76. {
  77. "classes": ("wide",),
  78. "fields": ("email", "password1", "password2")
  79. }
  80. ),
  81. )
  82.  
  83. search_fields = ("email",)
  84. ordering = ("email",)
  85. filter_horizontal = ()
  86.  
  87.  
  88. admin.site.register(ApiUser, UserAdmin)
Add Comment
Please, Sign In to add comment