forked from miguelgrinberg/flasky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
74 lines (58 loc) · 2.15 KB
/
hello.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
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, ValidationError, StopValidation
import re
def email_validation(form, field):
message = "Please include an '@' in the email address."
if "@" not in field.data:
raise ValidationError(message + f"'{form.email.data}' is missing an '@'.")
# User Info submission
class UserForm(FlaskForm):
name = StringField("What is your name?", validators=[DataRequired()])
email = StringField(
"What is your UofT email address?",
validators=[
DataRequired(),
email_validation,
],
)
submit = SubmitField("Submit")
app = Flask(__name__)
app.config["SECRET_KEY"] = "hard to guess string"
Bootstrap(app)
Moment(app)
# Handle GET and POST requests (form submission)
@app.route("/", methods=["GET", "POST"])
def index():
form = UserForm()
if form.validate_on_submit():
old = session.get("name")
# Pop up message if the user changes their name
if old != None and old != form.name.data:
flash("You have changed your name.")
session["name"] = form.name.data
old_email = session.get("email")
# Pop up message if the user changes their email
if old_email != None and old_email != form.email.data:
flash("You have changed your email.")
# Check for string "utoronto" in input email
if "@mail.utoronto" not in form.email.data:
session["message"] = "Please use your UofT email."
else:
session["message"] = f"Your UofT email is {form.email.data}"
session["email"] = form.email.data
return redirect(url_for("index"))
return render_template(
"index.html",
form=form,
name=session.get("name"),
emailMesssage=session.get("message"),
)
@app.route("/user/<name>")
def user(name):
return render_template("user.html", name=name)
if __name__ == "__main__":
app.run(debug=True)