|
|
|
|
@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _
|
|
|
|
|
from . import utils
|
|
|
|
|
from .models import BlogUser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 登录表单,继承自Django内置的AuthenticationForm
|
|
|
|
|
class LoginForm(AuthenticationForm):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
|
|
|
@ -16,11 +16,11 @@ class LoginForm(AuthenticationForm):
|
|
|
|
|
self.fields['password'].widget = widgets.PasswordInput(
|
|
|
|
|
attrs={'placeholder': "password", "class": "form-control"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 注册表单,继承自Django内置的UserCreationForm
|
|
|
|
|
class RegisterForm(UserCreationForm):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super(RegisterForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
# 自定义用户名、邮箱和密码字段的HTML属性
|
|
|
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
|
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
|
|
|
self.fields['email'].widget = widgets.EmailInput(
|
|
|
|
|
@ -29,18 +29,18 @@ class RegisterForm(UserCreationForm):
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 忘记密码表单(验证邮箱和验证码)
|
|
|
|
|
class ForgetPasswordForm(forms.Form):
|
|
|
|
|
new_password1 = forms.CharField(
|
|
|
|
|
label=_("New password"),
|
|
|
|
|
@ -51,7 +51,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 新密码字段2(用于确认)
|
|
|
|
|
new_password2 = forms.CharField(
|
|
|
|
|
label="确认密码",
|
|
|
|
|
widget=forms.PasswordInput(
|
|
|
|
|
@ -61,7 +61,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 邮箱字段
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label='邮箱',
|
|
|
|
|
widget=forms.TextInput(
|
|
|
|
|
@ -71,7 +71,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 验证码字段
|
|
|
|
|
code = forms.CharField(
|
|
|
|
|
label=_('Code'),
|
|
|
|
|
widget=forms.TextInput(
|
|
|
|
|
@ -81,16 +81,16 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 验证两次输入的密码是否一致,并检查密码强度
|
|
|
|
|
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"))
|
|
|
|
|
password_validation.validate_password(password2)
|
|
|
|
|
password_validation.validate_password(password2)# 使用Django的密码验证器
|
|
|
|
|
|
|
|
|
|
return password2
|
|
|
|
|
|
|
|
|
|
# 验证邮箱是否已注册
|
|
|
|
|
def clean_email(self):
|
|
|
|
|
user_email = self.cleaned_data.get("email")
|
|
|
|
|
if not BlogUser.objects.filter(
|
|
|
|
|
@ -99,10 +99,10 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
# todo 这里的报错提示可以判断一个邮箱是不是注册过,如果不想暴露可以修改
|
|
|
|
|
raise ValidationError(_("email does not exist"))
|
|
|
|
|
return user_email
|
|
|
|
|
|
|
|
|
|
# 验证用户输入的验证码是否正确
|
|
|
|
|
def clean_code(self):
|
|
|
|
|
code = self.cleaned_data.get("code")
|
|
|
|
|
error = utils.verify(
|
|
|
|
|
error = utils.verify(# 调用工具函数验证验证码
|
|
|
|
|
email=self.cleaned_data.get("email"),
|
|
|
|
|
code=code,
|
|
|
|
|
)
|
|
|
|
|
@ -110,7 +110,7 @@ class ForgetPasswordForm(forms.Form):
|
|
|
|
|
raise ValidationError(error)
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 忘记密码功能中的验证码发送表单(仅需邮箱字段)
|
|
|
|
|
class ForgetPasswordCodeForm(forms.Form):
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label=_('Email'),
|
|
|
|
|
|