forked from puhanfmc3/tentest
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.5 KiB
128 lines
4.5 KiB
from django import forms
|
|
from django.contrib.auth import get_user_model, password_validation
|
|
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
|
|
from django.core.exceptions import ValidationError
|
|
from django.forms import widgets
|
|
from django.utils.translation import gettext_lazy as _
|
|
from . import utils
|
|
from .models import BlogUser
|
|
|
|
|
|
class LoginForm(AuthenticationForm):
|
|
#sjt 登录表单:定义登录表单字段样式
|
|
def __init__(self, *args, **kwargs):
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
# sjt 设置用户名输入框样式
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
# sjt 设置密码输入框样式
|
|
self.fields['password'].widget = widgets.PasswordInput(
|
|
attrs={'placeholder': "password", "class": "form-control"})
|
|
|
|
|
|
class RegisterForm(UserCreationForm):
|
|
#sjt 注册表单:验证用户名、邮箱、密码合法性
|
|
def __init__(self, *args, **kwargs):
|
|
super(RegisterForm, self).__init__(*args, **kwargs)
|
|
# sjt 设置用户名输入框样式
|
|
self.fields['username'].widget = widgets.TextInput(
|
|
attrs={'placeholder': "username", "class": "form-control"})
|
|
# sjt 设置邮箱输入框样式
|
|
self.fields['email'].widget = widgets.EmailInput(
|
|
attrs={'placeholder': "email", "class": "form-control"})
|
|
# sjt 设置密码输入框样式
|
|
self.fields['password1'].widget = widgets.PasswordInput(
|
|
attrs={'placeholder': "password", "class": "form-control"})
|
|
# sjt 设置确认密码输入框样式
|
|
self.fields['password2'].widget = widgets.PasswordInput(
|
|
attrs={'placeholder': "repeat password", "class": "form-control"})
|
|
|
|
def clean_email(self):
|
|
#sjt 验证邮箱唯一性:已存在则抛出异常
|
|
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() # sjt 关联用户模型
|
|
fields = ("username", "email") # sjt 表单包含的字段
|
|
|
|
|
|
class ForgetPasswordForm(forms.Form):
|
|
#sjt 密码找回表单:验证邮箱、验证码、新密码合法性
|
|
new_password1 = forms.CharField(
|
|
label=_("New password"),
|
|
widget=forms.PasswordInput(
|
|
attrs={
|
|
"class": "form-control",
|
|
'placeholder': _("New password")
|
|
}
|
|
),
|
|
) # sjt 新密码字段
|
|
|
|
new_password2 = forms.CharField(
|
|
label="确认密码",
|
|
widget=forms.PasswordInput(
|
|
attrs={
|
|
"class": "form-control",
|
|
'placeholder': _("Confirm password")
|
|
}
|
|
),
|
|
) # sjt 确认密码字段
|
|
|
|
email = forms.EmailField(
|
|
label='邮箱',
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
'class': 'form-control',
|
|
'placeholder': _("Email")
|
|
}
|
|
),
|
|
) # sjt 邮箱字段
|
|
|
|
code = forms.CharField(
|
|
label=_('Code'),
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
'class': 'form-control',
|
|
'placeholder': _("Code")
|
|
}
|
|
),
|
|
) # sjt 验证码字段
|
|
|
|
def clean_new_password2(self):
|
|
#sjt 验证两次输入密码一致性,并检查密码强度
|
|
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) # sjt 调用Django密码验证器
|
|
return password2
|
|
|
|
def clean_email(self):
|
|
#sjt 验证邮箱是否已注册
|
|
user_email = self.cleaned_data.get("email")
|
|
if not BlogUser.objects.filter(
|
|
email=user_email
|
|
).exists():
|
|
raise ValidationError(_("email does not exist"))
|
|
return user_email
|
|
|
|
def clean_code(self):
|
|
#sjt 验证验证码有效性
|
|
code = self.cleaned_data.get("code")
|
|
error = utils.verify(
|
|
email=self.cleaned_data.get("email"),
|
|
code=code,
|
|
)
|
|
if error:
|
|
raise ValidationError(error)
|
|
return code
|
|
|
|
|
|
class ForgetPasswordCodeForm(forms.Form):
|
|
#sjt 发送验证码表单:验证邮箱格式
|
|
email = forms.EmailField(
|
|
label=_('Email'),
|
|
) # sjt 邮箱字段(用于发送验证码) |