-
Notifications
You must be signed in to change notification settings - Fork 59
/
mainfile.py
55 lines (44 loc) · 2.23 KB
/
mainfile.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
from flask import Flask, request, render_template, flash, url_for, redirect
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, RadioField, DateField, IntegerField, BooleanField, ValidationError # see some new ones... + ValidationError
from wtforms.validators import Required, Length, Email, Regexp
#####################
##### APP SETUP #####
#####################
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string' # Can be whatever for us, for now; this is what 'seeds' the CSRF protection
app.debug=True
####################
###### FORMS #######
####################
class SecretClubForm(FlaskForm):
name = StringField("Enter the name of your secret club:", validators=[Required(),Length(3,64)]) # Must be at least 3 and no more than 64 chars
number_members = StringField("How many people may be in this secret club (must enter an integer):", validators=[Required(),Regexp('^\d+$')]) # Validated with a regular expression: only digits
passcode = StringField("What is the secret code to enter the secret society? Must not have any vowels.",validators=[Required()]) # Required, plus the below validator...
def validate_passcode(self, field):
## Replace comments w code here...
## With code here -- I had 2 lines above the code already provided to make it so that this validator would reject anything with vowels. Could also do it in 1 line.
for ch in "hello": # REPLACE THIS LINE WITH SOMETHING ELSE
if ch in vowels:
raise ValidationError("Your passcode was not valid because there was at least 1 vowel in it.")
####################
###### ROUTES ######
####################
@app.route('/')
def hello_world():
return render_template('home.html')
@app.route('/form')
def form_entry():
form = SecretClubForm()
@app.route('/answers')
def show_answers():
form = SecretClubForm()
if form.validate_on_submit():
name = form.name.data
passcode = form.passcode.data
capacity = form.number_members.data
return render_template('results.html',name=name,passcode=passcode,capacity=capacity)
flash(form.errors)
return redirect(url_for('form_entry'))
if __name__ == "__main__":
app.run(use_reloader=True,debug=True)