Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

8 лет назад
8 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from api import models
  2. from api.models import 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. admin.site.register(models.Password, PasswordAdmin)
  63. admin.site.register(LessPassUser, LessPassUserAdmin)
  64. admin.site.unregister(Group)