You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
2.0 KiB

  1. from api.models import PasswordInfo, Entry, LessPassUser
  2. from django import forms
  3. from django.contrib import admin
  4. from django.contrib.auth.models import Group
  5. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  6. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  7. class UserCreationForm(forms.ModelForm):
  8. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  9. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  10. class Meta:
  11. model = LessPassUser
  12. fields = ('email',)
  13. def clean_password2(self):
  14. password1 = self.cleaned_data.get("password1")
  15. password2 = self.cleaned_data.get("password2")
  16. if password1 and password2 and password1 != password2:
  17. raise forms.ValidationError("Passwords don't match")
  18. return password2
  19. def save(self, commit=True):
  20. user = super(UserCreationForm, self).save(commit=False)
  21. user.set_password(self.cleaned_data["password1"])
  22. if commit:
  23. user.save()
  24. return user
  25. class UserChangeForm(forms.ModelForm):
  26. password = ReadOnlyPasswordHashField()
  27. class Meta:
  28. model = LessPassUser
  29. fields = ('email', 'password', 'is_active', 'is_admin')
  30. def clean_password(self):
  31. return self.initial["password"]
  32. class LessPassUserAdmin(BaseUserAdmin):
  33. form = UserChangeForm
  34. add_form = UserCreationForm
  35. list_display = ('email', 'is_admin',)
  36. list_filter = ('is_admin',)
  37. fieldsets = (
  38. (None, {'fields': ('email', 'password')}),
  39. ('Permissions', {'fields': ('is_admin',)}),
  40. )
  41. add_fieldsets = (
  42. (None, {
  43. 'classes': ('wide',),
  44. 'fields': ('email', 'password1', 'password2')}
  45. ),
  46. )
  47. search_fields = ('email',)
  48. ordering = ('email',)
  49. filter_horizontal = ()
  50. admin.site.register(Entry)
  51. admin.site.register(PasswordInfo)
  52. admin.site.register(LessPassUser, LessPassUserAdmin)
  53. admin.site.unregister(Group)