|
|
|
|
@ -9,90 +9,116 @@ from .models import BlogUser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LoginForm(AuthenticationForm):
|
|
|
|
|
"""自定义登录表单,继承自Django的AuthenticationForm"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
"""初始化方法,设置表单字段的widget属性"""
|
|
|
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
|
|
|
# 设置用户名字段的输入框属性
|
|
|
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
|
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
|
|
|
# 设置密码字段的输入框属性
|
|
|
|
|
self.fields['password'].widget = widgets.PasswordInput(
|
|
|
|
|
attrs={'placeholder': "password", "class": "form-control"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterForm(UserCreationForm):
|
|
|
|
|
"""自定义用户注册表单,继承自Django的UserCreationForm"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
"""初始化方法,设置所有表单字段的widget属性"""
|
|
|
|
|
super(RegisterForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
# 设置用户名字段的输入框属性
|
|
|
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
|
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
|
|
|
# 设置邮箱字段的输入框属性
|
|
|
|
|
self.fields['email'].widget = widgets.EmailInput(
|
|
|
|
|
attrs={'placeholder': "email", "class": "form-control"})
|
|
|
|
|
# 设置密码字段的输入框属性
|
|
|
|
|
self.fields['password1'].widget = widgets.PasswordInput(
|
|
|
|
|
attrs={'placeholder': "password", "class": "form-control"})
|
|
|
|
|
# 设置确认密码字段的输入框属性
|
|
|
|
|
self.fields['password2'].widget = widgets.PasswordInput(
|
|
|
|
|
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() # 使用当前激活的用户模型
|
|
|
|
|
fields = ("username", "email") # 表单包含的字段
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ForgetPasswordForm(forms.Form):
|
|
|
|
|
"""忘记密码重置表单"""
|
|
|
|
|
|
|
|
|
|
# 新密码字段
|
|
|
|
|
new_password1 = forms.CharField(
|
|
|
|
|
label=_("New password"),
|
|
|
|
|
widget=forms.PasswordInput(
|
|
|
|
|
label=_("New password"), # 字段标签
|
|
|
|
|
widget=forms.PasswordInput( # 密码输入框
|
|
|
|
|
attrs={
|
|
|
|
|
"class": "form-control",
|
|
|
|
|
'placeholder': _("New password")
|
|
|
|
|
"class": "form-control", # CSS类
|
|
|
|
|
'placeholder': _("New password") # 占位符文本
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 确认新密码字段
|
|
|
|
|
new_password2 = forms.CharField(
|
|
|
|
|
label="确认密码",
|
|
|
|
|
widget=forms.PasswordInput(
|
|
|
|
|
label="确认密码", # 字段标签
|
|
|
|
|
widget=forms.PasswordInput( # 密码输入框
|
|
|
|
|
attrs={
|
|
|
|
|
"class": "form-control",
|
|
|
|
|
'placeholder': _("Confirm password")
|
|
|
|
|
"class": "form-control", # CSS类
|
|
|
|
|
'placeholder': _("Confirm password") # 占位符文本
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 邮箱字段
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label='邮箱',
|
|
|
|
|
widget=forms.TextInput(
|
|
|
|
|
label='邮箱', # 字段标签
|
|
|
|
|
widget=forms.TextInput( # 文本输入框
|
|
|
|
|
attrs={
|
|
|
|
|
'class': 'form-control',
|
|
|
|
|
'placeholder': _("Email")
|
|
|
|
|
'class': 'form-control', # CSS类
|
|
|
|
|
'placeholder': _("Email") # 占位符文本
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 验证码字段
|
|
|
|
|
code = forms.CharField(
|
|
|
|
|
label=_('Code'),
|
|
|
|
|
widget=forms.TextInput(
|
|
|
|
|
label=_('Code'), # 字段标签
|
|
|
|
|
widget=forms.TextInput( # 文本输入框
|
|
|
|
|
attrs={
|
|
|
|
|
'class': 'form-control',
|
|
|
|
|
'placeholder': _("Code")
|
|
|
|
|
'class': 'form-control', # CSS类
|
|
|
|
|
'placeholder': _("Code") # 占位符文本
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def clean_new_password2(self):
|
|
|
|
|
"""确认密码字段验证方法"""
|
|
|
|
|
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"))
|
|
|
|
|
# 使用Django的密码验证器验证密码强度
|
|
|
|
|
password_validation.validate_password(password2)
|
|
|
|
|
|
|
|
|
|
return password2
|
|
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
|
"""邮箱字段验证方法"""
|
|
|
|
|
user_email = self.cleaned_data.get("email")
|
|
|
|
|
# 检查邮箱对应的用户是否存在
|
|
|
|
|
if not BlogUser.objects.filter(
|
|
|
|
|
email=user_email
|
|
|
|
|
).exists():
|
|
|
|
|
@ -101,7 +127,9 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
return user_email
|
|
|
|
|
|
|
|
|
|
def clean_code(self):
|
|
|
|
|
"""验证码字段验证方法"""
|
|
|
|
|
code = self.cleaned_data.get("code")
|
|
|
|
|
# 使用utils模块验证邮箱和验证码是否匹配
|
|
|
|
|
error = utils.verify(
|
|
|
|
|
email=self.cleaned_data.get("email"),
|
|
|
|
|
code=code,
|
|
|
|
|
@ -112,6 +140,8 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ForgetPasswordCodeForm(forms.Form):
|
|
|
|
|
"""忘记密码验证码请求表单(仅包含邮箱字段)"""
|
|
|
|
|
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label=_('Email'),
|
|
|
|
|
)
|
|
|
|
|
label=_('Email'), # 邮箱字段标签
|
|
|
|
|
)
|