from django import forms
from django.contrib import auth
from django.contrib.auth.models import User

class LoginForm(forms.Form):
    username=forms.CharField(label='用户名',required=True,\
                            widget=forms.TextInput(attrs={'class':"form-control",'placeholder':'请输入用户名'}))#required默认为True
    password=forms.CharField(label='密码',\
                            widget=forms.PasswordInput(attrs={'class':"form-control",'placeholder':'请输入密码'}))
    
    def clean(self):
        username=self.cleaned_data['username']
        password=self.cleaned_data['password']
        user=auth.authenticate(username=username,password=password)
        if user is None:
            raise forms.ValidationError('用户名或密码不正确!!!')
        else:
            self.cleaned_data['user']=user
        return self.cleaned_data
        
        
class RegForm(forms.Form):
    username=forms.CharField(label='用户名',required=True,max_length=30,min_length=3,\
                            widget=forms.TextInput(attrs={'class':"form-control",'placeholder':'请输入3-30位用户名'}))
    email=forms.EmailField(label='邮箱',required=True,\
                            widget=forms.EmailInput(attrs={'class':"form-control",'placeholder':'请输入邮箱'}))
    password=forms.CharField(label='密码',min_length=6,\
                            widget=forms.PasswordInput(attrs={'class':"form-control",'placeholder':'请输入密码(至少6位)'}))
    password_again=forms.CharField(label='再次输入密码',min_length=6,\
                            widget=forms.PasswordInput(attrs={'class':"form-control",'placeholder':'请再次输入密码'}))
    
    def clean_username(self):
        username=self.cleaned_data['username']
        if User.objects.filter(username=username).count()>0:
            raise forms.ValidationError('用户名已存在')
        return username
        
    def clean_email(self):
        email=self.cleaned_data['email']
        if User.objects.filter(email=email).count()>0:
            raise forms.ValidationError('邮箱已存在')
        return email
        
    def clean_password_again(self):
        password=self.cleaned_data['password']
        password_again=self.cleaned_data['password_again']
        if password!=password_again:
            raise forms.ValidationError('两次输入的密码不一致')
        return password_again
        
class ChangeInfoForm(forms.Form):
    username=forms.CharField(label='用户名',required=True,max_length=30,min_length=3,\
                            widget=forms.TextInput(attrs={'class':"form-control",'placeholder':'请输入3-30位用户名'}))
    text=forms.CharField(label='简介',widget=forms.Textarea(attrs={'class':"form-control",'placeholder':'请介绍一下你自己','rows':4}))
    
    def clean_username(self):
        username=self.cleaned_data['username']
        if User.objects.filter(username=username).count()>0:
            raise forms.ValidationError('用户名已存在')
        return username