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.
75 lines
2.3 KiB
75 lines
2.3 KiB
from django import forms
|
|
from django.contrib.auth.admin import UserAdmin
|
|
from django.contrib.auth.forms import UserChangeForm
|
|
from django.contrib.auth.forms import UsernameField
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from .models import BlogUser
|
|
|
|
|
|
class BlogUserCreationForm(forms.ModelForm):
|
|
password1 = forms.CharField(label=_('password'), widget=forms.PasswordInput)
|
|
password2 = forms.CharField(label=_('Enter password again'), widget=forms.PasswordInput)
|
|
|
|
class Meta:
|
|
model = BlogUser
|
|
fields = ('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(_("passwords do not match"))
|
|
return password2
|
|
|
|
def save(self, commit=True):
|
|
user = super().save(commit=False)
|
|
user.set_password(self.cleaned_data["password1"])
|
|
if commit:
|
|
user.source = 'adminsite'
|
|
user.save()
|
|
return user
|
|
|
|
|
|
class BlogUserChangeForm(UserChangeForm):
|
|
class Meta:
|
|
model = BlogUser
|
|
fields = '__all__'
|
|
field_classes = {'username': UsernameField}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
class BlogUserAdmin(UserAdmin):
|
|
form = BlogUserChangeForm
|
|
add_form = BlogUserCreationForm
|
|
|
|
# 添加这些关键定义
|
|
fieldsets = (
|
|
(None, {'fields': ('username', 'password')}),
|
|
(_('Personal info'), {'fields': ('nickname', 'email', 'first_name', 'last_name')}),
|
|
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
|
|
'groups', 'user_permissions')}),
|
|
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
|
|
(_('Source'), {'fields': ('source',)}),
|
|
)
|
|
|
|
add_fieldsets = (
|
|
(None, {
|
|
'classes': ('wide',),
|
|
'fields': ('username', 'email', 'password1', 'password2'),
|
|
}),
|
|
)
|
|
|
|
list_display = (
|
|
'id',
|
|
'nickname',
|
|
'username',
|
|
'email',
|
|
'last_login',
|
|
'date_joined',
|
|
'source')
|
|
list_display_links = ('id', 'username')
|
|
ordering = ('-id',)
|
|
search_fields = ('username', 'nickname', 'email') |