-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·331 lines (264 loc) · 11.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
import re
from flask import Flask, render_template, redirect, url_for, request, Markup, make_response, session, send_file, escape
import os
import git
import functools
from icalendar import Calendar, Event
from flask_dance.contrib.google import make_google_blueprint, google
from config import *
import secrets
from schedule import Schedule, ScheduleManager, check_choate_email, check_teacher, block_iter
from ical import make_calendar
import auth
import config
from utils import *
# Temp (INSECURE, REMOVE IN PROD)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
app = Flask(__name__)
random_secret_key = secrets.token_urlsafe(32)
app.config.update(
DEBUG=False,
SECRET_KEY=random_secret_key
)
# Credit: oauth boilerplate stuff from library documentation
app.config["GOOGLE_OAUTH_CLIENT_ID"] = GOOGLE_CLIENT_ID
app.config["GOOGLE_OAUTH_CLIENT_SECRET"] = GOOGLE_CLIENT_SECRET
app.config["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "true"
app.config['SERVER_NAME'] = SERVER_NAME
app.config['PREFERRED_URL_SCHEME'] = "https"
os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "true"
google_bp = make_google_blueprint(scope=["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"], offline=True)
# app.register_blueprint(google_bp, url_prefix="/login", reprompt_select_account=True, reprompt_consent=True)
app.register_blueprint(google_bp, url_prefix="/login")
@app.route('/admin/secure.log')
def secure():
"""
Securely returns a log of all actions taken
"""
# email, firstname, lastname = get_profile()
# if email and firstname and lastname and check_choate_email(email):
# log.info("here")
token = request.args.get('token')
if secrets.compare_digest(token,config.TOKEN):
log_info("Log accessed "+ str(request.args))
return send_file("live_detector.log")
else:
log_info("Log deny access "+ str(request.args))
return redirect('/')
@app.route('/api/calendar.ics')
def cal():
"""
Checks authorization of user to request ical file
"""
# email, firstname, lastname = get_profile()
# if email and firstname and lastname and check_choate_email(email):
# log.info("here")
token = request.args.get('token')
email = request.args.get('email')
firstname = request.args.get('first')
lastname = request.args.get('last')
authentication = auth.Auth()
authentication.init_db_connection()
email = authentication.get_email_from_token(token)
authentication.end_db_connection()
if email:
cal = make_response(make_calendar(email, firstname, lastname).to_ical())
cal.mimetype = 'text/calendar'
return cal
return redirect('/')
@app.route('/search')
def search():
"""
Searches for teacher meeting ids
"""
email, firstname, lastname = auth.check_login(request)
if email and firstname and lastname:
query = request.args.get('search')
# ScheduleManager().createSchedule(email, firstname, lastname, check_teacher(email))
user_schedule = ScheduleManager().getSchedule(email, firstname, lastname, check_teacher(email))
user_schedule.init_db_connection()
search_results = user_schedule.search_teacher(query)
cards = ""
for result in search_results:
desc = result.get('office_desc')
if desc is None:
desc = ""
result["office_desc"] = Markup(str(escape(desc)).replace("\n", "<br>"))
cards += render_template("teacher_card.html", **result)
commit = get_commit()
calendar_token = auth.get_token(request)
user_schedule.end_db_connection()
return render_template("index.html", cards=Markup(cards), card_js="", commit=commit, calendar_token=calendar_token, email=email, firstname=firstname, lastname=lastname)
else:
return redirect("/")
@app.route('/update', methods=['POST'])
def update():
"""
Gets the Zoom meeting ids
"""
course = request.form.get('course')
section = request.form.get('section')
meeting_id = str(request.form.get('meeting_id'))
if course == "Office Hours" and section == "DESC":
email, firstname, lastname = auth.check_login(request)
if email and firstname and lastname:
if meeting_id is None:
meeting_id = ''
user_schedule = ScheduleManager().getSchedule(email, firstname, lastname, check_teacher(email))
user_schedule.init_db_connection()
user_schedule.update_teacher_database_office_description(email, escape(meeting_id))
return "Success"
return "Error"
if not(course and section and meeting_id):
return "Error"
lines = meeting_id.split("\n")
id_num = -1
for l in lines:
id = str(re.sub(r"\D", "", l))
if len(id) > 8 and len(id) < 12:
id_num = int(id)
if (id_num == -1):
return "Error"
# with urllib.request.urlopen('https://zoom.us/j/' + str(id_num)) as response:
# html = response.read()
# if "Invalid meeting ID." in str(html):
# return "Error"
email, firstname, lastname = auth.check_login(request)
if course and meeting_id and email and firstname and lastname:
user_schedule = ScheduleManager().getSchedule(email, firstname, lastname, check_teacher(email))
user_schedule.init_db_connection()
if course == "Office Hours":
if section == "ID":
user_schedule.update_teacher_database_office_id(email, id_num)
if section == "DESC":
user_schedule.update_teacher_database_office_description(email, id_num)
elif email:
user_schedule.update_schedule(course, section, id_num)
user_schedule.end_db_connection()
return str(id_num)
return "Error"
@app.route('/')
def index():
"""
Will contain the default views for faculty, students, and teachers
"""
# if email := get_email():
email, firstname, lastname = auth.check_login(request)
if email and firstname and lastname:
# ScheduleManager().createSchedule(email, firstname, lastname, check_teacher(email))
user_schedule = ScheduleManager().getSchedule(email, firstname, lastname, check_teacher(email))
user_schedule.init_db_connection()
# render_template here
# log.info(Schedule().search_teacher_exact("Guelakis Patrick"))
user_schedule.fetch_schedule()
card_script = ""
cards = ""
toc = {'A': '', 'B': '', 'C': '', 'D': '', 'E': '', 'F': '', 'G': ''}
# log.info(block_iter())
# if check_teacher(email):
# uuid = secrets.token_hex(8)
# cards += render_template("class_card.html")
# card_script += render_template("card.js")
top_label = "Today's Classes:"
bottom_label = "Not Today"
for block, start_time in block_iter(is_teacher=check_teacher(email)):
if block == "Not Today":
top_label = start_time + "'s Classes"
bottom_label = "Not On " + start_time
continue
if block == "Break":
cards += "<br><br><hr><br><h4>" + bottom_label + "</h4><br>"
continue
uuid = secrets.token_hex(8)
schedule = None
if block == "Office Hours":
try:
teacher = user_schedule.search_teacher_email_with_creation(user_schedule.email, user_schedule.lastname, user_schedule.firstname)
schedule = {"block": "Office",
"course": "Office Hours",
"course_name": "Office Hours",
"teacher_name": str(user_schedule.firstname).title() + " " + str(user_schedule.lastname).title(),
"meeting_id": teacher['office_id'],
"teacher_email": 'placeholder',
"office_desc": teacher.get('office_desc')}
if schedule['office_desc'] is None:
schedule['office_desc'] = ''
except TypeError as e:
log_error("Unable to create teacher schedule due to failed query")
else:
schedule = user_schedule.schedule[block]
if schedule is None:
continue
elif not check_teacher(email):
teacher = user_schedule.search_teacher_email(schedule["teacher_email"])
schedule["office_meeting_id"] = teacher.get('office_id')
desc = teacher.get('office_desc')
if desc is None:
desc = ""
schedule["office_desc"] = Markup(str(escape(desc)).replace("\n", "<br>"))
schedule["user_can_change"] = not bool(teacher.get(schedule.get('block') + "_id"))
else:
schedule["user_can_change"] = True
if len(block) == 1:
# toc[block] = '<br><li><a href="#' + block + '-block">' + block + ' Block</a></li>'
toc[block] = render_template("toc.html", block=block)
schedule["uuid"] = uuid
schedule["time"] = start_time
if block == "Office Hours":
schedule['office_desc'] = str(escape(str(schedule['office_desc']).replace('\\', '\\\\'))).replace('\n', '\\n')
cards += render_template("office_hours_card.html", **schedule)
card_script += render_template("office_hours.js", **schedule)
else:
cards += render_template("class_card.html", **schedule)
card_script += render_template("card.js", **schedule)
commit = get_commit()
calendar_token = auth.get_token(request)
user_schedule.end_db_connection()
response = make_response(render_template("index.html",
cards=Markup(cards),
card_js=Markup(card_script),
toc=Markup(toc['A'] + toc['B'] + toc['C'] + toc['D'] + toc['E'] + toc['F'] + toc['G']),
top_label=top_label,
calendar_token=calendar_token,
email=email,
firstname=str(firstname).title(),
lastname=str(lastname).title(),
refresh=True,
commit=commit))
return auth.set_login(response, request)
else:
button = render_template("login.html")
commit = get_commit()
return render_template("landing.html", button=Markup(button), commit=commit)
# return redirect("/login")
@app.route('/help')
def help():
commit = get_commit()
button = render_template("back.html")
return render_template("landing.html", button=Markup(button), commit=commit)
@app.route('/login')
def login():
"""
Redirects to the proper login page (right now /google/login), but may change
"""
if (not google.authorized) or auth.get_token(request):
return redirect(url_for("google.login"))
else:
resp = make_response("Invalid credentials! Make sure you're logging in with your Choate account. <a href=\"/logout\">Try again.</a>")
return resp
@app.route('/logout')
def logout():
auth.deauth_token(request)
session.clear()
return redirect("/")
@app.route('/up')
def up():
return "Yay!"
@functools.lru_cache()
def get_commit():
"""
Returns latest commit hash
"""
repo = git.Repo(search_parent_directories=True)
return repo.head.object.hexsha