-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
47 lines (38 loc) · 1.64 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from flask.ext.wtf import Form
from wtforms import StringField, BooleanField, PasswordField
from wtforms.validators import DataRequired
from models import User
class LoginForm(Form):
username = StringField('username', validators=[DataRequired(message="All fields are required.")])
password = PasswordField('password', validators=[DataRequired("All fields are required.")])
remember_me = BooleanField('remember_me', default=False)
def validate(self):
if not Form.validate(self):
del self.username.errors[:]
del self.password.errors[:]
self.username.errors.append("All fields are required.")
return False
return True
class SignupForm(Form):
username = StringField('username', validators=[DataRequired(message="All fields are required.")])
password = PasswordField('password', validators=[DataRequired(message="All fields are required.")])
cpassword = PasswordField('cpassword', validators=[DataRequired(message="All fields are required.")])
remember_me = BooleanField('remember_me', default=False)
def validate(self):
if not Form.validate(self):
del self.username.errors[:]
del self.password.errors[:]
del self.cpassword.errors[:]
self.username.errors.append("All fields are required.")
return False
if len(self.username.data) > 20:
self.username.errors.append('Usernames must be 20 characters or less.')
return False
user = User.query.filter_by(name=self.username.data).first()
if user != None:
self.username.errors.append('Username already taken.')
return False
if not self.password.data == self.cpassword.data:
self.password.errors.append('Passwords don\'t match!')
return False
return True