Guest User

Untitled

a guest
Oct 17th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. from django.contrib import admin
  2. from .models import User
  3. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  4. from .forms import UserAdminCreationForm, UserAdminChangeForm
  5.  
  6. class MyUserAdmin(BaseUserAdmin):
  7. form = UserAdminChangeForm
  8. add_form = UserAdminCreationForm
  9.  
  10. list_display = ('email', 'first_name', 'last_name', 'is_staff', 'is_active', 'is_email_verified')
  11.  
  12. def get_list_filter(self, request):
  13. if request.user.is_superuser:
  14. return ['groups']
  15. else:
  16. return ['is_staff', 'is_active', 'is_email_verified']
  17.  
  18. readonly_fields = ('last_login', 'date_joined',)
  19. fieldsets = (
  20. (None, {'fields': ('email', 'password')}),
  21. ('Personal info', {'fields': ('first_name','last_name')}),
  22. ('Permissions', {'fields': ('is_active','is_staff', 'groups')}),
  23. ('Important dates', {'fields': ('last_login', 'date_joined')}),
  24. )
  25.  
  26. add_fieldsets = (
  27. (None, {
  28. 'classes': ('wide',),
  29. 'fields': ('first_name','last_name','email', 'password1', 'password2', 'groups')}
  30. ),
  31. )
  32. search_fields = ('first_name', 'last_name', 'email',)
  33. ordering = ('email',)
  34. filter_horizontal = ()
  35.  
  36. def get_queryset(self, request):
  37. qs = super().get_queryset(request)
  38. if request.user.is_superuser:
  39. return qs
  40. qs = qs.filter(groups__id__in=request.user.groups.all())
  41. return qs
  42.  
  43. admin.site.register(User, MyUserAdmin)
  44.  
  45. class UserAdminCreationForm(forms.ModelForm):
  46. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  47. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  48.  
  49. class Meta:
  50. User = get_user_model()
  51. model = User
  52. fields = ('email',)
  53.  
  54. def clean_password2(self):
  55. # Check that the two password entries match
  56. password1 = self.cleaned_data.get("password1")
  57. password2 = self.cleaned_data.get("password2")
  58. if password1 and password2 and password1 != password2:
  59. raise forms.ValidationError("Passwords don't match")
  60. return password2
  61.  
  62. def save(self, commit=True):
  63. # Save the provided password in hashed format
  64. user = super(UserAdminCreationForm, self).save(commit=False)
  65. user.set_password(self.cleaned_data["password1"])
  66. user.is_staff = True
  67. if commit:
  68. user.save()
  69. return user
  70.  
  71. class UserAdminChangeForm(forms.ModelForm):
  72.  
  73. password = ReadOnlyPasswordHashField()
  74.  
  75. class Meta:
  76. User = get_user_model()
  77. model = User
  78. fields = ('email', 'password', 'is_active','is_staff', 'groups')
  79.  
  80.  
  81. def clean_password(self):
  82. return self.initial["password"]
Add Comment
Please, Sign In to add comment