<reponame>xiaoxiaomeng0/web_development_projects
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
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'] = '<KEY>'
Bootstrap(app)


class CafeForm(FlaskForm):
    cafe = StringField('Cafe Name', validators=[DataRequired()])
    location = StringField('Cafe Location on Google Maps (URL)', validators=[DataRequired(), URL()])
    open_time = StringField('Opening Time e.g. 8AM', validators=[DataRequired()])
    close_time = StringField('Closing Time e.g. 5:30PM', validators=[DataRequired()])
    coffee_rating = SelectField('Coffee Rating',
                                # choices=[("0", "✘"), ("1", "☕"), ("2", "☕☕"), ("3", "☕☕☕"), ("4", "☕☕☕☕"), ("5", "☕☕☕☕☕")],
                                choices=[("✘"), ("☕"), ("☕☕"), ("☕☕☕"), ("☕☕☕☕"), ("☕☕☕☕☕")],
                                validators=[DataRequired()])
    wifi_rating = SelectField('Wifi Strength Rating',
                              # choices=[("0", "✘"), ("1", "💪"), ("2", "💪💪"), ("3", "💪💪💪"), ("4", "💪💪💪💪"),
                              #          ("5", "💪💪💪💪💪")],
                              choices=[("✘"), ("💪"), ("💪💪"), ("💪💪💪"), ("💪💪💪💪"), ("💪💪💪💪💪")],
                              validators=[DataRequired()])
    power_rating = SelectField('Power Socket Availibility',
                               # choices=[("0", "✘"), ("1", "🔌"), ("2", "🔌🔌"), ("3", "🔌🔌🔌"), ("4", "🔌🔌🔌🔌"),
                               #          ("5", "🔌🔌🔌🔌🔌")],
                               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():
        with open("cafe-data.csv", "a", newline="") as csv_file:
            csv_file.write(f"\n{form.cafe.data},{form.location.data},{form.open_time.data},{form.close_time.data},"
                           f"{form.coffee_rating.data},{form.wifi_rating.data},{form.power_rating.data}")

    # Exercise:
    # Make the form write a new row into cafe-data.csv
    # with   if form.validate_on_submit()
    return render_template('add.html', form=form)


@app.route('/cafes')
def cafes():
    with open('cafe-data.csv', newline='') 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)

from flask import Flask, render_template
from flask_bootstrap import Bootstrap
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'] = '<KEY>'
Bootstrap(app)


class CafeForm(FlaskForm):
    cafe = StringField('Cafe Name', validators=[DataRequired()])
    location = StringField('Cafe Location on Google Maps (URL)', validators=[DataRequired(), URL()])
    open_time = StringField('Opening Time e.g. 8AM', validators=[DataRequired()])
    close_time = StringField('Closing Time e.g. 5:30PM', validators=[DataRequired()])
    coffee_rating = SelectField('Coffee Rating',
                                # choices=[("0", "✘"), ("1", "☕"), ("2", "☕☕"), ("3", "☕☕☕"), ("4", "☕☕☕☕"), ("5", "☕☕☕☕☕")],
                                choices=[("✘"), ("☕"), ("☕☕"), ("☕☕☕"), ("☕☕☕☕"), ("☕☕☕☕☕")],
                                validators=[DataRequired()])
    wifi_rating = SelectField('Wifi Strength Rating',
                              # choices=[("0", "✘"), ("1", "💪"), ("2", "💪💪"), ("3", "💪💪💪"), ("4", "💪💪💪💪"),
                              #          ("5", "💪💪💪💪💪")],
                              choices=[("✘"), ("💪"), ("💪💪"), ("💪💪💪"), ("💪💪💪💪"), ("💪💪💪💪💪")],
                              validators=[DataRequired()])
    power_rating = SelectField('Power Socket Availibility',
                               # choices=[("0", "✘"), ("1", "🔌"), ("2", "🔌🔌"), ("3", "🔌🔌🔌"), ("4", "🔌🔌🔌🔌"),
                               #          ("5", "🔌🔌🔌🔌🔌")],
                               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():
        with open("cafe-data.csv", "a", newline="") as csv_file:
            csv_file.write(f"\n{form.cafe.data},{form.location.data},{form.open_time.data},{form.close_time.data},"
                           f"{form.coffee_rating.data},{form.wifi_rating.data},{form.power_rating.data}")

    # Exercise:
    # Make the form write a new row into cafe-data.csv
    # with   if form.validate_on_submit()
    return render_template('add.html', form=form)


@app.route('/cafes')
def cafes():
    with open('cafe-data.csv', newline='') 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)
# choices=[("0", "✘"), ("1", "☕"), ("2", "☕☕"), ("3", "☕☕☕"), ("4", "☕☕☕☕"), ("5", "☕☕☕☕☕")], # choices=[("0", "✘"), ("1", "💪"), ("2", "💪💪"), ("3", "💪💪💪"), ("4", "💪💪💪💪"), #          ("5", "💪💪💪💪💪")], # choices=[("0", "✘"), ("1", "🔌"), ("2", "🔌🔌"), ("3", "🔌🔌🔌"), ("4", "🔌🔌🔌🔌"), #          ("5", "🔌🔌🔌🔌🔌")], # 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 # Exercise: # Make the form write a new row into cafe-data.csv # with   if form.validate_on_submit()