-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
48 lines (40 loc) · 2.07 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
48
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from models import User
class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')
class RegistrationForm(FlaskForm):
username = StringField("Username", validators=[DataRequired(), Length(1,64)])
email = StringField("Email", validators=[Email(), DataRequired()])
password = PasswordField("Passsword", validators=[Length(8,100), DataRequired()])
passwordconfirm = PasswordField("Passsword", validators=[EqualTo('password')])
submit = SubmitField("Register")
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first()
if user is not None:
raise ValidationError("That username is taken.")
def validate_email(self, email):
email = User.query.filter_by(email=email.data).first()
if email is not None:
raise ValidationError("That email is taken.")
class EditForm(FlaskForm):
username = StringField("Username", validators=[DataRequired(), Length(1,64)])
about = TextAreaField("About Me", validators=[Length(0,200)])
submit = SubmitField("Submit")
def __init__(self, original_username, *args, **kwargs):
super(EditForm, self).__init__(*args, **kwargs)
self.original_username=original_username
def validate_username(self, username):
if username.data != self.original_username:
user = User.query.filter_by(username=self.username.data).first()
if user is not None:
raise ValidationError("That username is in use.")
class EmptyForm(FlaskForm):
submit=SubmitField("Submit")
class PostForm(FlaskForm):
post = TextAreaField("Say Something", validators=[DataRequired(), Length(1,500)])
submit = SubmitField("Post")