-
Notifications
You must be signed in to change notification settings - Fork 142
/
main.py
64 lines (48 loc) · 2.39 KB
/
main.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
# Coffee & Wi-Fi v1.1
# Note: The course did not have an actual coding project for this day, so I've decided to experiment
# with different color & font combinations for the website from Day 62 instead, along with some other
# tweaks to the page templates.
from flask import Flask, render_template, redirect
from flask_wtf import FlaskForm
from wtforms import StringField, SelectField, SubmitField
from wtforms.validators import DataRequired, URL
from flask_bootstrap import Bootstrap
from cafe_data import CafeManager
app = Flask(__name__)
app.secret_key = "ThisIsASecretKey"
Bootstrap(app)
class CafeForm(FlaskForm):
name = StringField("Cafe Name", validators=[DataRequired()])
# make sure this is a proper URL
location = StringField("Cafe Location on Google Maps (URL)", validators=[DataRequired(), URL()])
time_open = StringField("Opening Time e.g. 8:00 AM", validators=[DataRequired()])
time_close = StringField("Closing Time e.g. 5:30 PM", validators=[DataRequired()])
coffee = SelectField("Coffee Rating", choices=["☕️", "☕☕", "☕☕☕", "☕☕☕☕", "☕☕☕☕☕"],
validators=[DataRequired()])
wifi = SelectField("Wifi Strength Rating", choices=["✘", "💪", "💪💪", "💪💪💪", "💪💪💪💪", "💪💪💪💪💪"],
validators=[DataRequired()])
power = SelectField("Power Socket Availability", choices=["✘", "🔌", "🔌🔌", "🔌🔌🔌", "🔌🔌🔌🔌", "🔌🔌🔌🔌🔌"],
validators=[DataRequired()])
submit = SubmitField('Submit')
cafe_list = CafeManager()
@app.route("/")
def home():
return render_template("index.html")
# "secret" page to add a new entry
@app.route("/add", methods=["GET", "POST"])
def add_cafe():
form = CafeForm()
if form.validate_on_submit():
# using list comprehension to get just elements that are needed
new_cafe = [form.data[item] for item in form.data][:7]
cafe_list.add_cafe(new_cafe)
# redirect to the cafes page
return redirect("/cafes")
return render_template("add.html", add_cafe_form=form)
@app.route("/cafes")
def cafes():
# get the header and the list of cafes separately, to allow different formatting
header, entries = cafe_list.get_cafes()
return render_template("cafes.html", header=header, cafes=entries)
if __name__ == "__main__":
app.run(debug=True)