|
|
|
|
@ -1,16 +1,46 @@
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
from django import forms
|
|
|
|
|
from django.contrib.auth.admin import UserAdmin # 导入Django默认的用户管理Admin类(基础模板)
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
from django.contrib.auth.forms import UserChangeForm # 导入默认的用户编辑表单(用于继承修改)
|
|
|
|
|
from django.contrib.auth.forms import UsernameField# 用户名字段的专用类(自带验证逻辑)
|
|
|
|
|
from django.utils.translation import gettext_lazy as _# 国际化支持,文本可翻译
|
|
|
|
|
=======
|
|
|
|
|
from django import forms# 导入 Django 表单基类
|
|
|
|
|
from django.contrib.auth.admin import UserAdmin# 导入 Django 默认的用户管理类
|
|
|
|
|
from django.contrib.auth.forms import UserChangeForm# 导入 Django 默认的用户修改表单
|
|
|
|
|
from django.contrib.auth.forms import UsernameField# 导入 Django 默认的用户名字段类型
|
|
|
|
|
from django.utils.translation import gettext_lazy as _# 导入国际化翻译工具
|
|
|
|
|
>>>>>>> yrh-branch
|
|
|
|
|
|
|
|
|
|
# Register your models here.
|
|
|
|
|
from .models import BlogUser # 导入自定义的用户模型(替代Django默认User)
|
|
|
|
|
=======
|
|
|
|
|
from django.contrib.auth.forms import UserChangeForm # 导入默认的用户编辑表单(用于继承修改)
|
|
|
|
|
from django.contrib.auth.forms import UsernameField # 用户名字段的专用类(自带验证逻辑)
|
|
|
|
|
from django.utils.translation import gettext_lazy as _ # 国际化支持,文本可翻译
|
|
|
|
|
=======
|
|
|
|
|
from django import forms# 从 django 核心模块导入 forms 用于表单处理
|
|
|
|
|
from django.contrib.auth.admin import UserAdmin# 从 django 内置的 auth 管理模块导入 UserAdmin,用于用户管理界面的定制
|
|
|
|
|
from django.contrib.auth.forms import UserChangeForm# 从 django 内置的 auth 表单模块导入 UserChangeForm,用于用户信息修改表单的定制
|
|
|
|
|
from django.contrib.auth.forms import UsernameField# 从 django 内置的 auth 表单模块导入 UsernameField,用于用户名字段的处理
|
|
|
|
|
from django.utils.translation import gettext_lazy as _# 从 django 翻译工具模块导入 gettext_lazy 并别名成 _,用于实现国际化翻译(延迟翻译)
|
|
|
|
|
|
|
|
|
|
# Register your models here.
|
|
|
|
|
from .models import BlogUser# 从当前目录的 models 模块导入自定义的 BlogUser 模型
|
|
|
|
|
>>>>>>> xyr-branch
|
|
|
|
|
|
|
|
|
|
# Register your models here.
|
|
|
|
|
from .models import BlogUser # 导入自定义的用户模型(替代Django默认User)
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
>>>>>>> xh_branch
|
|
|
|
|
|
|
|
|
|
# 自定义用户创建表单(用于在后台添加新用户)
|
|
|
|
|
class BlogUserCreationForm(forms.ModelForm):
|
|
|
|
|
password1 = forms.CharField(label=_('password'), widget=forms.PasswordInput)
|
|
|
|
|
password2 = forms.CharField(label=_('Enter password again'), widget=forms.PasswordInput)
|
|
|
|
|
password1 = forms.CharField(label=_('password'), widget=forms.PasswordInput)# 密码字段(密码输入框)
|
|
|
|
|
password2 = forms.CharField(label=_('Enter password again'), widget=forms.PasswordInput)# 确认密码字段(再次输入密码)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = BlogUser # 绑定自定义的BlogUser模型
|
|
|
|
|
@ -21,19 +51,47 @@ class BlogUserCreationForm(forms.ModelForm):
|
|
|
|
|
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
|
|
|
|
|
=======
|
|
|
|
|
class BlogUserCreationForm(forms.ModelForm):# 定义 BlogUserCreationForm 类,继承自 forms.ModelForm,用于创建 BlogUser 的表单
|
|
|
|
|
password1 = forms.CharField(label=_('password'), widget=forms.PasswordInput) # 定义 password1 字段,类型为 CharField,标签为“password”,小部件使用 PasswordInput(密码输入框,内容会隐藏)
|
|
|
|
|
password2 = forms.CharField(label=_('Enter password again'), widget=forms.PasswordInput)# 定义 password2 字段,类型为 CharField,标签为“Enter password again”,小部件同样使用 PasswordInput
|
|
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
|
class Meta:# Meta 类用于定义模型表单的元数据
|
|
|
|
|
model = BlogUser # 关联的模型是 BlogUser
|
|
|
|
|
fields = ('email',) # 表单中包含的字段是 'email'
|
|
|
|
|
|
|
|
|
|
def clean_password2(self): # 定义 clean_password2 方法,用于验证两个密码是否一致
|
|
|
|
|
# Check that the two password entries match
|
|
|
|
|
password1 = self.cleaned_data.get("password1") # 获取清洗后的 password1 数据
|
|
|
|
|
password2 = self.cleaned_data.get("password2") # 获取清洗后的 password2 数据
|
|
|
|
|
if password1 and password2 and password1 != password2:# 如果 password1 和 password2 都存在且不相等
|
|
|
|
|
raise forms.ValidationError(_("passwords do not match"))# 抛出验证错误,提示“密码不匹配”
|
|
|
|
|
return password2 # 返回 password2
|
|
|
|
|
>>>>>>> xyr-branch
|
|
|
|
|
|
|
|
|
|
def save(self, commit=True): # 定义 save 方法,用于保存用户数据,commit 参数控制是否立即提交到数据库
|
|
|
|
|
# Save the provided password in hashed format
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
user = super().save(commit=False)
|
|
|
|
|
# 使用 Django 内置方法加密密码
|
|
|
|
|
user.set_password(self.cleaned_data["password1"])
|
|
|
|
|
if commit:
|
|
|
|
|
user.source = 'adminsite'
|
|
|
|
|
user.save()
|
|
|
|
|
return user
|
|
|
|
|
=======
|
|
|
|
|
user = super().save(commit=False) # 调用父类的 save 方法,commit=False 表示先不提交到数据库
|
|
|
|
|
user.set_password(self.cleaned_data["password1"]) # 对 password1 进行哈希处理后设置到用户对象中
|
|
|
|
|
if commit: # 如果 commit 为 True
|
|
|
|
|
user.source = 'adminsite' # 设置用户的来源为 'adminsite'
|
|
|
|
|
user.save() # 保存用户对象到数据库
|
|
|
|
|
return user # 返回用户对象
|
|
|
|
|
>>>>>>> xyr-branch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 自定义用户修改表单(用于在后台编辑用户信息)
|
|
|
|
|
class BlogUserChangeForm(UserChangeForm):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = BlogUser
|
|
|
|
|
@ -43,10 +101,13 @@ class BlogUserChangeForm(UserChangeForm):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 自定义用户管理类(用于在后台管理用户)
|
|
|
|
|
class BlogUserAdmin(UserAdmin):
|
|
|
|
|
# 指定编辑用户时使用的表单
|
|
|
|
|
form = BlogUserChangeForm
|
|
|
|
|
# 指定添加用户时使用的表单
|
|
|
|
|
add_form = BlogUserCreationForm
|
|
|
|
|
# 列表页显示的字段
|
|
|
|
|
list_display = (
|
|
|
|
|
'id',
|
|
|
|
|
'nickname',
|
|
|
|
|
@ -55,5 +116,44 @@ class BlogUserAdmin(UserAdmin):
|
|
|
|
|
'last_login',
|
|
|
|
|
'date_joined',
|
|
|
|
|
'source')
|
|
|
|
|
# 列表页中可点击的字段(用于进入详情页)
|
|
|
|
|
list_display_links = ('id', 'username')
|
|
|
|
|
# 默认排序方式(按 ID 降序)
|
|
|
|
|
ordering = ('-id',)
|
|
|
|
|
|
|
|
|
|
# Meta 类用于定义模型表单的元数据
|
|
|
|
|
class Meta:
|
|
|
|
|
# 关联的模型是 BlogUser
|
|
|
|
|
model = BlogUser
|
|
|
|
|
# 表单中包含的字段是 'email'
|
|
|
|
|
fields = ('email',)
|
|
|
|
|
|
|
|
|
|
# 定义 clean_password2 方法,用于验证两个密码是否一致
|
|
|
|
|
def clean_password2(self):
|
|
|
|
|
# 注释:检查两个密码输入是否匹配
|
|
|
|
|
# 获取清洗后的 password1 数据
|
|
|
|
|
password1 = self.cleaned_data.get("password1")
|
|
|
|
|
# 获取清洗后的 password2 数据
|
|
|
|
|
password2 = self.cleaned_data.get("password2")
|
|
|
|
|
# 如果 password1 和 password2 都存在且不相等
|
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
|
# 抛出验证错误,提示“密码不匹配”
|
|
|
|
|
raise forms.ValidationError(_("passwords do not match"))
|
|
|
|
|
# 返回 password2
|
|
|
|
|
return password2
|
|
|
|
|
|
|
|
|
|
# 定义 save 方法,用于保存用户数据,commit 参数控制是否立即提交到数据库
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
|
# 注释:以哈希格式保存提供的密码
|
|
|
|
|
# 调用父类的 save 方法,commit=False 表示先不提交到数据库
|
|
|
|
|
user = super().save(commit=False)
|
|
|
|
|
# 对 password1 进行哈希处理后设置到用户对象中
|
|
|
|
|
user.set_password(self.cleaned_data["password1"])
|
|
|
|
|
# 如果 commit 为 True
|
|
|
|
|
if commit:
|
|
|
|
|
# 设置用户的来源为 'adminsite'
|
|
|
|
|
user.source = 'adminsite'
|
|
|
|
|
# 保存用户对象到数据库
|
|
|
|
|
user.save()
|
|
|
|
|
# 返回用户对象
|
|
|
|
|
return user
|
|
|
|
|
|