Initial commit
This commit is contained in:
60
accounts/forms.py
Normal file
60
accounts/forms.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from django import forms
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.forms import PasswordChangeForm
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
login = forms.CharField(label='用户名或邮箱', max_length=150, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
password = forms.CharField(label='密码', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
|
||||
captcha = forms.CharField(label='验证码', required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.enable_captcha = kwargs.pop('enable_captcha', False)
|
||||
super().__init__(*args, **kwargs)
|
||||
if not self.enable_captcha:
|
||||
# 隐藏或移除验证码字段
|
||||
self.fields['captcha'].widget = forms.HiddenInput()
|
||||
|
||||
|
||||
class RegisterForm(forms.Form):
|
||||
username = forms.CharField(label='用户名', max_length=150, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
email = forms.EmailField(label='邮箱', widget=forms.EmailInput(attrs={'class': 'form-control'}))
|
||||
password1 = forms.CharField(label='密码', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
|
||||
password2 = forms.CharField(label='确认密码', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
|
||||
display_name = forms.CharField(label='显示名称', required=False, max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
contact_phone = forms.CharField(label='联系电话', required=False, max_length=30, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
captcha = forms.CharField(label='验证码', required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.enable_captcha = kwargs.pop('enable_captcha', False)
|
||||
super().__init__(*args, **kwargs)
|
||||
if not self.enable_captcha:
|
||||
self.fields['captcha'].widget = forms.HiddenInput()
|
||||
|
||||
def clean(self):
|
||||
cleaned = super().clean()
|
||||
if cleaned.get('password1') != cleaned.get('password2'):
|
||||
raise forms.ValidationError('两次密码不一致')
|
||||
username = cleaned.get('username')
|
||||
email = cleaned.get('email')
|
||||
if username and User.objects.filter(username=username).exists():
|
||||
raise forms.ValidationError('该用户名已存在')
|
||||
if email and User.objects.filter(email=email).exists():
|
||||
raise forms.ValidationError('该邮箱已注册')
|
||||
return cleaned
|
||||
|
||||
|
||||
class ProfileForm(forms.Form):
|
||||
display_name = forms.CharField(label='显示名称', required=False, max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
contact_phone = forms.CharField(label='联系电话', required=False, max_length=30, widget=forms.TextInput(attrs={'class': 'form-control'}))
|
||||
|
||||
|
||||
class SimplePasswordChangeForm(PasswordChangeForm):
|
||||
# 仅用于注入 bootstrap 样式
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
for field in self.fields.values():
|
||||
field.widget.attrs['class'] = 'form-control'
|
||||
Reference in New Issue
Block a user