-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
64 lines (47 loc) · 2.5 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import DataRequired, EqualTo, ValidationError
from wtforms_sqlalchemy.fields import QuerySelectField
from flask_login import current_user
import main
def author_query():
return main.Author.query.all()
def books_query():
return main.Books.query.all()
class AddNewBookForm(FlaskForm):
book_name = StringField('Book name', [DataRequired()])
author = QuerySelectField(query_factory=author_query, allow_blank=True, get_label="name", get_pk=lambda obj: str(obj))
submit = SubmitField('Add New Book')
def validate_book_name(self, book_name):
books = main.Books.query.filter_by(book_name=self.book_name.data).first()
if books:
raise ValidationError('Book exists. Add another one, please.')
class AddNewReviewForm(FlaskForm):
book_name = QuerySelectField(query_factory=books_query, allow_blank=True, get_label="book_name", get_pk=lambda obj: str(obj))
content = StringField('Review', [DataRequired()])
submit = SubmitField('Submit')
class SignUpForm(FlaskForm):
email_address = StringField('Email Address', [DataRequired()])
first_name = StringField('First Name', [DataRequired()])
last_name = StringField('Last Name')
password1 = PasswordField('Password', [DataRequired()])
password2 = PasswordField('Password confirm', [DataRequired(), EqualTo('password1', 'Passwords must match')])
submit = SubmitField('Sign Up')
def validate_email_address(self, email_address):
user = main.User.query.filter_by(email_address=self.email_address.data).first()
if user:
raise ValidationError('Email already exists. Sign in or use another email address.')
class SignInForm(FlaskForm):
email_address = StringField('Email Address', [DataRequired()])
password = PasswordField('Password', [DataRequired()])
submit = SubmitField('Sign In')
class UpdateAccountInformationForm(FlaskForm):
email_address = StringField('Email Address', [DataRequired()])
first_name = StringField('First Name', [DataRequired()])
last_name = StringField('Last Name')
submit = SubmitField('Update Info')
def validate_email_address(self, email_address):
if current_user.email_address != self.email_address.data:
user = main.User.query.filter_by(email_address=self.email_address.data).first()
if user:
raise ValidationError('Email already exists. Sign in or use another email address.')