diff --git a/accounts/admin.py b/accounts/admin.py index 83a4086..eebb165 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -1,8 +1,55 @@ +from django import forms from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from django.contrib.auth.forms import UserCreationForm, UserChangeForm +from django.contrib.auth.forms import ReadOnlyPasswordHashField # Register your models here. from .models import BlogUser -class BlogUserAdmin(admin.ModelAdmin): +class BlogUserCreationForm(forms.ModelForm): + password1 = forms.CharField(label='密码', widget=forms.PasswordInput) + password2 = forms.CharField(label='再次输入密码', widget=forms.PasswordInput) + + class Meta: + model = BlogUser + fields = ('email',) + + def clean_password2(self): + # Check that the two password entries match + 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): + # Save the provided password in hashed format + user = super().save(commit=False) + user.set_password(self.cleaned_data["password1"]) + if commit: + user.save() + return user + + +class BlogUserChangeForm(forms.ModelForm): + password = ReadOnlyPasswordHashField + email = forms.EmailField(label="Email", widget=forms.EmailInput) + + class Meta: + model = BlogUser + fields = ('email', 'password', 'is_active') + + def clean_password(self): + # Regardless of what the user provides, return the initial value. + # This is done here, rather than on the field, because the + # field does not have access to the initial value + return self.initial["password"] + + +class BlogUserAdmin(UserAdmin): + form = BlogUserChangeForm + add_form = BlogUserCreationForm list_display = ('id', 'nickname', 'username', 'email', 'last_login', 'date_joined') list_display_links = ('id', 'username') + ordering = ('-id',) diff --git a/accounts/models.py b/accounts/models.py index 1b40384..12b4f61 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -9,7 +9,6 @@ from django.utils.timezone import now class BlogUser(AbstractUser): nickname = models.CharField('昵称', max_length=100, blank=True) - mugshot = models.ImageField('头像', upload_to='upload/mugshots', blank=True) created_time = models.DateTimeField('创建时间', default=now) last_mod_time = models.DateTimeField('修改时间', default=now) @@ -25,3 +24,9 @@ class BlogUser(AbstractUser): site = get_current_site().domain url = "https://{site}{path}".format(site=site, path=self.get_absolute_url()) return url + + class Meta: + ordering = ['-id'] + verbose_name = "用户" + verbose_name_plural = verbose_name + get_latest_by = 'id'