-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
127 lines (104 loc) · 4.4 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, HiddenField, SubmitField, \
DateTimeField, BooleanField, DateTimeLocalField
from wtforms.widgets import HiddenInput
from wtforms.validators import DataRequired, Length, ValidationError
from app_manager import db
from models import User, Nomination
passmin = 7
passmax = 128
def available(form, field):
user = User.query.filter_by(username=field.data.lower()).first()
if user is not None:
raise ValidationError("Username already exists")
def exists(form, field):
user = User.query.filter_by(username=field.data.lower()).first()
if user is None:
raise ValidationError("Username does not exist")
return user
def check_ban(form, field):
user = exists(form, field)
if form.ban.data and user.banned:
raise ValidationError("User is already banned")
elif form.unban.data and not user.banned:
raise ValidationError("User is not banned")
def check_admin(form, field):
user = exists(form, field)
if form.give.data and user.is_admin:
raise ValidationError("User is already admin")
elif form.take.data and not user.is_admin:
raise ValidationError("User is not admin")
def check_nom(form, field):
# IntegerField just ignores invalid input instead of letting the
# user know that they need to enter an integer
nomid = integer(form, field)
nom = Nomination.query.filter_by(id=nomid).first()
if nom is None:
raise ValidationError("Nomination ID does not exist")
def integer(form, field):
try:
i = int(field.data)
except ValueError:
raise ValidationError("Must be an integer") from None
return i
class SignupForm(FlaskForm):
username = StringField("Andrew ID",
validators=[DataRequired(), Length(max=16), available])
password = PasswordField("Password",
validators=[DataRequired(), Length(min=passmin, max=passmax)])
class LoginForm(FlaskForm):
username = StringField("Andrew ID", validators=[DataRequired(), exists])
password = PasswordField("Password", validators=[DataRequired()])
class UsernameForm(FlaskForm):
username = StringField("Andrew ID", validators=[DataRequired(), exists])
class BanForm(FlaskForm):
banuser = StringField("Username", validators=[DataRequired(), check_ban])
email = BooleanField("Email")
ban = SubmitField("Ban User")
unban = SubmitField("Unban User")
class AdminForm(FlaskForm):
adminuser = StringField("Username",
validators=[DataRequired(), check_admin])
give = SubmitField("Give Admin")
take = SubmitField("Take Admin")
class NomIDForm(FlaskForm):
nomid = StringField("Nomination ID", validators=[DataRequired(), check_nom])
rem = SubmitField("Remove")
rwarn = SubmitField("Remove & Warn")
rban = SubmitField("Remove, Ban, & Notify")
class ResetPasswordForm(FlaskForm):
password = PasswordField("New Password",
validators=[DataRequired(), Length(min=passmin, max=passmax)])
class ChangePasswordForm(FlaskForm):
currentpass = PasswordField("Current Password", validators=[DataRequired()])
password = PasswordField("New Password",
validators=[DataRequired(), Length(min=passmin, max=passmax)])
class NominationForm(FlaskForm):
entry = HiddenField("Nomination",
validators=[DataRequired(), Length(max=128)])
award_id = HiddenField("Award ID")
class VoteForm(FlaskForm):
nomid = HiddenField("Nomination ID")
class SetPhaseForm(FlaskForm):
static = SubmitField("Switch to static phase")
nom = SubmitField("Switch to nominating phase")
vote = SubmitField("Switch to voting phase")
class PhaseNomForm(FlaskForm):
dtnom = DateTimeLocalField(format="%Y-%m-%dT%H:%M")
pnon = SubmitField("Schedule Nominating Phase")
pnoff = SubmitField("Cancel")
class PhaseVoteForm(FlaskForm):
dtvote = DateTimeLocalField(format="%Y-%m-%dT%H:%M")
pvon = SubmitField("Schedule Voting Phase")
pvoff = SubmitField("Cancel")
class PhaseStaticForm(FlaskForm):
dtstatic = DateTimeLocalField(format="%Y-%m-%dT%H:%M")
pson = SubmitField("Schedule Static Phase")
psoff = SubmitField("Cancel")
class ClearForm(FlaskForm):
cnoms = SubmitField("Clear Nominations")
cvotes = SubmitField("Clear Votes")
class RemoveNomForm(FlaskForm):
nomid = HiddenField("Nomination ID")
warn = BooleanField("Warn", widget=HiddenInput())
ban = BooleanField("Ban", widget=HiddenInput())