forked from metakgp/wimp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
109 lines (82 loc) · 2.33 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import json
import os
from flask import Flask, abort, render_template, request
from werkzeug.exceptions import BadRequest
from main import correct_spelling, get_attr, get_table, get_times
app = Flask(__name__)
path = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(path, "data/data.json")) as f:
profs = list(set(json.load(f).keys()))
profs.sort()
def fetch_results(prof):
tb = [[["Monday"]], [["Tuesday"]], [["Wednesday"]], [["Thursday"]], [["Friday"]]]
times = [
"",
"8 AM",
"9 AM",
"10 AM",
"11 AM",
"12 PM",
"2 PM",
"3 PM",
"4 PM",
"5 PM",
]
prof = correct_spelling(prof)
slot_data = get_times(prof)
dept = get_attr(prof, "dept")
website = get_attr(prof, "website")
if len(slot_data) == 0 and len(dept) == 0:
abort(404)
data = get_table(slot_data)
for row in tb:
for i in range(9):
row.append([])
for item in data:
for venue in data[item]:
if venue == "0":
venue = "In Dept"
tb[int(item[0])][int(item[1]) + 1].append(venue)
return [tb, times, dept, website, prof.title()]
@app.route("/", methods=["POST"])
def result():
prof = request.form["prof"]
tb, times, dept, website, prof = fetch_results(prof)
return render_template(
"main.html",
name=prof,
website=website,
data=tb,
times=times,
profs=profs,
dept=dept,
error=False,
)
@app.errorhandler(404)
def prof_not_found(error):
form = dict(request.form)
if form.get("prof") is None and request.path != "/favicon.ico":
raise BadRequest()
prof = form.get("prof")
return render_template("main.html", error=True, name=prof), 404
@app.route("/", methods=["GET"])
def main():
prof = request.args.get("prof")
if prof:
tb, times, dept, website, prof = fetch_results(prof)
return render_template(
"main.html",
name=prof,
website=website,
data=tb,
times=times,
profs=profs,
dept=dept,
error=False,
)
else:
return render_template("main.html", profs=profs)
if __name__ == "__main__":
app.run()