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.
32 lines
1.1 KiB
32 lines
1.1 KiB
from flask_wtf import Form
|
|
from wtforms import TextField, PasswordField
|
|
from wtforms.validators import DataRequired
|
|
|
|
from {{cookiecutter.repo_name}}.user.models import User
|
|
|
|
class LoginForm(Form):
|
|
username = TextField('Username', validators=[DataRequired()])
|
|
password = PasswordField('Password', validators=[DataRequired()])
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
self.user = None
|
|
|
|
def validate(self):
|
|
initial_validation = super(LoginForm, self).validate()
|
|
if not initial_validation:
|
|
return False
|
|
|
|
self.user = User.query.filter_by(username=self.username.data).first()
|
|
if not self.user:
|
|
self.username.errors.append("Unknown username")
|
|
return False
|
|
|
|
if not self.user.check_password(self.password.data):
|
|
self.password.errors.append("Invalid password")
|
|
return False
|
|
|
|
if not self.user.active:
|
|
self.username.errors.append("User not activated")
|
|
return False
|
|
return True
|
|
|