-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (60 loc) · 2.78 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
65
66
67
68
69
70
71
72
from flask import Flask, render_template, redirect, url_for
from flask_bootstrap import Bootstrap5
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms.validators import DataRequired, URL
import csv
app = Flask(__name__)
app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
Bootstrap5(app)
class CafeForm(FlaskForm):
cafe = StringField('Cafe name', validators=[DataRequired()])
location = StringField('Cafe Location on Goodle Maps (URL)', validators=[DataRequired(), URL()])
open = StringField('Opening Time e.g. 8AM', validators=[DataRequired()])
close = StringField('Closing Time e.g. 5:30PM')
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')
# Exercise:
# add: Location URL, open time, closing time, coffee rating, wifi rating, power outlet rating fields
# make coffee/wifi/power a select element with choice of 0 to 5.
#e.g. You could use emojis ☕️/💪/✘/🔌
# make all fields required except submit
# use a validator to check that the URL field has a URL entered.
# ---------------------------------------------------------------------------
# all Flask routes below
@app.route("/")
def home():
return render_template("index.html")
@app.route('/add', methods=["GET", "POST"])
def add_cafe():
form = CafeForm()
if form.validate_on_submit():
# Exercise:
# Make the form write a new row into cafe-data.csv
# with if form.validate_on_submit()
new_data = [
form.cafe.data,
form.location.data,
form.open.data,
form.close.data,
form.coffee.data,
form.wifi.data,
form.power.data
]
with open('cafe-data.csv', 'a', encoding="utf8") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(new_data)
return redirect(url_for('cafes'))
return render_template('add.html', form=form)
@app.route('/cafes')
def cafes():
with open('cafe-data.csv', newline='', encoding="utf8") as csv_file:
csv_data = csv.reader(csv_file, delimiter=',')
list_of_rows = []
for row in csv_data:
list_of_rows.append(row)
return render_template('cafes.html', cafes=list_of_rows)
if __name__ == '__main__':
app.run(debug=True)