25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.py 2.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from api import models
  2. from api.models import PasswordInfo, Entry, LessPassUser
  3. from django import forms
  4. from django.contrib import admin
  5. from django.contrib.auth.models import Group
  6. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  7. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  8. from django.db.models import Count
  9. class UserCreationForm(forms.ModelForm):
  10. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  11. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  12. class Meta:
  13. model = LessPassUser
  14. fields = ('email',)
  15. def clean_password2(self):
  16. password1 = self.cleaned_data.get("password1")
  17. password2 = self.cleaned_data.get("password2")
  18. if password1 and password2 and password1 != password2:
  19. raise forms.ValidationError("Passwords don't match")
  20. return password2
  21. def save(self, commit=True):
  22. user = super(UserCreationForm, self).save(commit=False)
  23. user.set_password(self.cleaned_data["password1"])
  24. if commit:
  25. user.save()
  26. return user
  27. class UserChangeForm(forms.ModelForm):
  28. password = ReadOnlyPasswordHashField()
  29. class Meta:
  30. model = LessPassUser
  31. fields = ('email', 'password', 'is_active', 'is_admin')
  32. def clean_password(self):
  33. return self.initial["password"]
  34. class LessPassUserAdmin(BaseUserAdmin):
  35. form = UserChangeForm
  36. add_form = UserCreationForm
  37. list_display = ('email', 'is_admin', 'column_passwords_count')
  38. list_filter = ('is_admin', 'is_active')
  39. fieldsets = (
  40. (None, {'fields': ('email', 'password')}),
  41. ('Permissions', {'fields': ('is_admin',)}),
  42. )
  43. add_fieldsets = (
  44. (None, {
  45. 'classes': ('wide',),
  46. 'fields': ('email', 'password1', 'password2')}
  47. ),
  48. )
  49. search_fields = ('email',)
  50. ordering = ('email',)
  51. filter_horizontal = ()
  52. def get_queryset(self, request):
  53. return models.LessPassUser.objects.annotate(passwords_count=Count('passwords'))
  54. def column_passwords_count(self, instance):
  55. return instance.passwords_count
  56. column_passwords_count.short_description = 'Password count'
  57. column_passwords_count.admin_order_field = 'passwords_count'
  58. class PasswordAdmin(admin.ModelAdmin):
  59. list_display = ('id', 'user',)
  60. search_fields = ('user__email',)
  61. ordering = ('user',)
  62. class EntryAdmin(admin.ModelAdmin):
  63. list_display = ('id', 'user',)
  64. search_fields = ('user__email',)
  65. ordering = ('user',)
  66. admin.site.register(models.Password, PasswordAdmin)
  67. admin.site.register(Entry, EntryAdmin)
  68. admin.site.register(PasswordInfo)
  69. admin.site.register(LessPassUser, LessPassUserAdmin)
  70. admin.site.unregister(Group)