This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
application.py
78 lines (63 loc) · 2.35 KB
/
application.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
75
76
77
78
import cs50
import csv
from flask import Flask, jsonify, redirect, render_template, request
# Configure application
app = Flask(__name__)
# Reload templates when they are changed
app.config["TEMPLATES_AUTO_RELOAD"] = True
@app.after_request
def after_request(response):
"""Disable caching"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/", methods=["GET"])
def get_index():
return redirect("/form")
@app.route("/form", methods=["GET"])
def get_form():
return render_template("form.html")
@app.route("/form", methods=["POST"])
def post_form():
# Get content from form
name = request.form.get("name")
email = request.form.get("email")
number = request.form.get("number")
catlover = request.form.get("catlover")
behaviors = request.form.get("behaviors")
soylent = request.form.get("soylent")
comments = request.form.get("comments")
# If required fields are there:
if name and email and number and soylent:
with open("survey.csv", "a", newline="") as file:
fieldnames = ["name", "email", "number", "catlover", "behaviors", "soylent", "comments"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write content to survey.csv
writer.writerow({
"name": name,
"email": email,
"number": number,
"catlover": catlover,
"behaviors": behaviors,
"soylent": soylent,
"comments": comments
})
# Redirect to /sheet
return redirect("/sheet")
# If required fields are not there, render error template
else:
return render_template("error.html", message="Please fill out the form before submitting.")
@app.route("/sheet", methods=["GET"])
def get_sheet():
# Open survey.csv
with open("survey.csv") as file:
reader = csv.DictReader(file)
# Create a list from file content
data = list(reader)
# If data, render sheet template
if data:
return render_template("sheet.html", data=data)
# If not data, render error template
else:
return render_template("error.html", message="No data was found. Please try again later.")