|
| 1 | +from flask import Flask, render_template, redirect, url_for |
| 2 | +from flask_bootstrap import Bootstrap |
| 3 | +from flask_wtf import FlaskForm |
| 4 | +from wtforms import StringField, SubmitField, SelectField |
| 5 | +from wtforms.validators import DataRequired, URL |
| 6 | +import csv |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | +app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b' |
| 10 | +Bootstrap(app) |
| 11 | + |
| 12 | + |
| 13 | +class CafeForm(FlaskForm): |
| 14 | + cafe = StringField('Cafe name', validators=[DataRequired()]) |
| 15 | + location = StringField("Cafe Location on Google Maps (URL)", validators=[DataRequired(), URL()]) |
| 16 | + open = StringField("Opening Time e.g. 8AM", validators=[DataRequired()]) |
| 17 | + close = StringField("Closing Time e.g. 5:30PM", validators=[DataRequired()]) |
| 18 | + coffee_rating = SelectField("Coffee Rating", choices=["☕️", "☕☕", "☕☕☕", "☕☕☕☕", "☕☕☕☕☕"], validators=[DataRequired()]) |
| 19 | + wifi_rating = SelectField("Wifi Strength Rating", choices=["✘", "💪", "💪💪", "💪💪💪", "💪💪💪💪", "💪💪💪💪💪"], validators=[DataRequired()]) |
| 20 | + power_rating = SelectField("Power Socket Availability", choices=["✘", "🔌", "🔌🔌", "🔌🔌🔌", "🔌🔌🔌🔌", "🔌🔌🔌🔌🔌"], validators=[DataRequired()]) |
| 21 | + submit = SubmitField('Submit') |
| 22 | + |
| 23 | + |
| 24 | +@app.route("/") |
| 25 | +def home(): |
| 26 | + return render_template("index.html") |
| 27 | + |
| 28 | + |
| 29 | +@app.route('/add', methods=["GET", "POST"]) |
| 30 | +def add_cafe(): |
| 31 | + form = CafeForm() |
| 32 | + if form.validate_on_submit(): |
| 33 | + with open("cafe-data.csv", mode="a") as csv_file: |
| 34 | + csv_file.write(f"\n{form.cafe.data}," |
| 35 | + f"{form.location.data}," |
| 36 | + f"{form.open.data}," |
| 37 | + f"{form.close.data}," |
| 38 | + f"{form.coffee_rating.data}," |
| 39 | + f"{form.wifi_rating.data}," |
| 40 | + f"{form.power_rating.data}") |
| 41 | + return redirect(url_for('cafes')) |
| 42 | + return render_template('add.html', form=form) |
| 43 | + |
| 44 | + |
| 45 | +@app.route('/cafes') |
| 46 | +def cafes(): |
| 47 | + with open('cafe-data.csv', newline='') as csv_file: |
| 48 | + csv_data = csv.reader(csv_file, delimiter=',') |
| 49 | + list_of_rows = [] |
| 50 | + for row in csv_data: |
| 51 | + list_of_rows.append(row) |
| 52 | + return render_template('cafes.html', cafes=list_of_rows) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + app.run(debug=True) |
0 commit comments