|
|
|
|
@ -9,8 +9,13 @@ from .models import BlogUser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LoginForm(AuthenticationForm):
|
|
|
|
|
"""
|
|
|
|
|
用户登录表单
|
|
|
|
|
继承Django的AuthenticationForm,添加Bootstrap样式支持
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
|
|
|
#ZXY: 自定义表单字段的widget,添加placeholder和CSS类
|
|
|
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
|
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
|
|
|
self.fields['password'].widget = widgets.PasswordInput(
|
|
|
|
|
@ -18,9 +23,14 @@ class LoginForm(AuthenticationForm):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterForm(UserCreationForm):
|
|
|
|
|
"""
|
|
|
|
|
用户注册表单
|
|
|
|
|
扩展Django的UserCreationForm,添加邮箱字段和样式支持
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super(RegisterForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
#ZXY: 为所有字段添加Bootstrap样式和占位符文本
|
|
|
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
|
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
|
|
|
self.fields['email'].widget = widgets.EmailInput(
|
|
|
|
|
@ -31,17 +41,26 @@ class RegisterForm(UserCreationForm):
|
|
|
|
|
attrs={'placeholder': "repeat password", "class": "form-control"})
|
|
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
|
"""
|
|
|
|
|
邮箱验证方法
|
|
|
|
|
确保注册的邮箱地址在系统中是唯一的
|
|
|
|
|
"""
|
|
|
|
|
email = self.cleaned_data['email']
|
|
|
|
|
if get_user_model().objects.filter(email=email).exists():
|
|
|
|
|
raise ValidationError(_("email already exists"))
|
|
|
|
|
return email
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = get_user_model()
|
|
|
|
|
fields = ("username", "email")
|
|
|
|
|
model = get_user_model() #ZXY: 使用项目中自定义的用户模型
|
|
|
|
|
fields = ("username", "email") #ZXY: 表单包含的字段
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ForgetPasswordForm(forms.Form):
|
|
|
|
|
"""
|
|
|
|
|
忘记密码重置表单
|
|
|
|
|
包含新密码设置、邮箱验证和验证码确认
|
|
|
|
|
"""
|
|
|
|
|
#ZXY: 新密码字段 - 使用Django的密码输入组件
|
|
|
|
|
new_password1 = forms.CharField(
|
|
|
|
|
label=_("New password"),
|
|
|
|
|
widget=forms.PasswordInput(
|
|
|
|
|
@ -52,6 +71,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#ZXY: 确认密码字段 - 标签没有国际化,需要改进
|
|
|
|
|
new_password2 = forms.CharField(
|
|
|
|
|
label="确认密码",
|
|
|
|
|
widget=forms.PasswordInput(
|
|
|
|
|
@ -62,6 +82,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#ZXY: 邮箱字段 - 用于识别用户和发送验证码
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label='邮箱',
|
|
|
|
|
widget=forms.TextInput(
|
|
|
|
|
@ -72,6 +93,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#ZXY: 验证码字段 - 用于安全验证
|
|
|
|
|
code = forms.CharField(
|
|
|
|
|
label=_('Code'),
|
|
|
|
|
widget=forms.TextInput(
|
|
|
|
|
@ -83,15 +105,22 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def clean_new_password2(self):
|
|
|
|
|
"""
|
|
|
|
|
验证两次输入的新密码是否一致
|
|
|
|
|
并使用Django的密码验证器检查密码强度
|
|
|
|
|
"""
|
|
|
|
|
password1 = self.data.get("new_password1")
|
|
|
|
|
password2 = self.data.get("new_password2")
|
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
|
raise ValidationError(_("passwords do not match"))
|
|
|
|
|
password_validation.validate_password(password2)
|
|
|
|
|
password_validation.validate_password(password2) #ZXY: Django密码强度验证
|
|
|
|
|
|
|
|
|
|
return password2
|
|
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
|
"""
|
|
|
|
|
验证邮箱是否存在系统中
|
|
|
|
|
"""
|
|
|
|
|
user_email = self.cleaned_data.get("email")
|
|
|
|
|
if not BlogUser.objects.filter(
|
|
|
|
|
email=user_email
|
|
|
|
|
@ -101,6 +130,10 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
return user_email
|
|
|
|
|
|
|
|
|
|
def clean_code(self):
|
|
|
|
|
"""
|
|
|
|
|
验证邮箱验证码是否正确
|
|
|
|
|
调用utils模块的验证功能
|
|
|
|
|
"""
|
|
|
|
|
code = self.cleaned_data.get("code")
|
|
|
|
|
error = utils.verify(
|
|
|
|
|
email=self.cleaned_data.get("email"),
|
|
|
|
|
@ -112,6 +145,10 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ForgetPasswordCodeForm(forms.Form):
|
|
|
|
|
"""
|
|
|
|
|
获取忘记密码验证码的表单
|
|
|
|
|
简单的邮箱输入表单,用于请求发送验证码邮件
|
|
|
|
|
"""
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label=_('Email'),
|
|
|
|
|
)
|
|
|
|
|
|