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.
72 lines
2.3 KiB
72 lines
2.3 KiB
from django.contrib import admin
|
|
from django import forms
|
|
from django.contrib import admin
|
|
from django.contrib.auth.models import Group
|
|
from django.contrib.auth.admin import UserAdmin
|
|
from django.contrib.auth.forms import ReadOnlyPasswordHashField
|
|
|
|
from .models import Profile
|
|
|
|
|
|
class ProfileCreationForm(forms.ModelForm):
|
|
password1 = forms.CharField(label='Пароль', widget=forms.PasswordInput)
|
|
password2 = forms.CharField(label='Подтверждение', widget=forms.PasswordInput)
|
|
|
|
class Meta:
|
|
model = Profile
|
|
fields = ('phone', 'email')
|
|
|
|
def clean_password2(self):
|
|
password1 = self.cleaned_data.get("password1")
|
|
password2 = self.cleaned_data.get("password2")
|
|
if password1 and password2 and password1 != password2:
|
|
raise forms.ValidationError("Пароли не совпадают")
|
|
return password2
|
|
|
|
def save(self, commit=True):
|
|
user = super(ProfileCreationForm, self).save(commit=False)
|
|
user.set_password(self.cleaned_data["password1"])
|
|
if commit:
|
|
user.save()
|
|
return user
|
|
|
|
|
|
class ProfileChangeForm(forms.ModelForm):
|
|
password = ReadOnlyPasswordHashField(
|
|
label= ("Password"),
|
|
help_text= ("<a href=\"../password/\">ИЗМЕНИТЬ ПАРОЛЬ</a>"))
|
|
|
|
class Meta:
|
|
model = Profile
|
|
fields = ('phone', 'email', 'password', 'is_active', 'is_superuser')
|
|
|
|
def clean_password(self):
|
|
return self.initial["password"]
|
|
|
|
|
|
class ProfileAdmin(UserAdmin):
|
|
form = ProfileChangeForm
|
|
add_form = ProfileCreationForm
|
|
|
|
list_display = ('phone', 'first_name', 'last_name', 'email','sale','date_joined')
|
|
list_filter = ('is_superuser',)
|
|
fieldsets = (
|
|
(None, {'fields': (
|
|
'phone', 'email', 'password', 'first_name', 'last_name', 'sale'
|
|
)}),
|
|
('Инфо', {'fields': ('date_joined',)}),
|
|
('Доступ', {'fields': ('is_superuser', 'is_active')}),
|
|
)
|
|
add_fieldsets = (
|
|
(None, {
|
|
'classes': ('wide',),
|
|
'fields': ('phone', 'email', 'first_name', 'last_name', 'password1', 'password2', 'temp_password')}
|
|
),
|
|
)
|
|
search_fields = ('phone', 'email',)
|
|
ordering = ('phone', 'email',)
|
|
filter_horizontal = ()
|
|
|
|
|
|
admin.site.register(Profile, ProfileAdmin)
|
|
admin.site.unregister(Group)
|
|
|