-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
512 lines (462 loc) · 23.3 KB
/
main.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from flask import Flask, render_template, request, redirect, url_for, session, flash, send_from_directory
from werkzeug import secure_filename
from flask_oauth import OAuth
import os
import random
import csv
import json
import xlrd
import time
import hmac
import hashlib
import re
from shutil import move
from flask.ext.compress import Compress
import hmac
import hashlib
import subprocess
from flask_sslify import SSLify
compress = Compress()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
def start_app():
app = Flask(__name__)
app.config['COMPRESS_MIMETYPES'] = ['text/html', 'text/css', 'text/xml', 'application/json', 'application/javascript', "image/png", "image/jpeg"]
compress.init_app(app)
sslify = SSLify(app, permanent=True, age=31536000)
return app
app = start_app()
html_escape_table = {
"&": "&",
'"': "\"",
"'": "\\'",
">": ">",
"<": "<",
}
def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)
def file_get_contents(filename):
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + filename) as f:
return f.read()
def randBG():
filepath = os.path.dirname(os.path.realpath(__file__))
files = [os.path.join(path, filename)
for path, dirs, files in os.walk(filepath + "/static/portfolio/")
for filename in files
if not filename.endswith(".bak")]
return random.choice(files)[len(filepath)+1:]
def remove_extra_spaces(data):
p = re.compile(r'\s+')
return p.sub(' ', data)
def remove_html_tags(data):
p = re.compile(r'<.*?>')
return remove_extra_spaces(p.sub('', data))
def getAttendancePanelContent():
return file_get_contents("panelContent/attendancePanel.txt")
def getOutreachPanelContent():
return file_get_contents("panelContent/outreachPanel.txt")
def convertDate(inDate):
if inDate == "Design Day 1" or inDate == "Design Day 2" or inDate == "Kickoff":
if inDate == "Design Day 1":
return '2016-01-09'
if inDate == "Kickoff":
return '2016-01-09'
if inDate == "Design Day 2":
return '2016-01-10'
else:
month = inDate[:inDate.find("/")]
day = inDate[inDate.find("/") + 1:inDate.find("/",inDate.find("/") + 1)]
year = inDate[inDate.find("/",inDate.find("/") + 1) + 1:]
if len(month) == 1:
month = str(0) + month
if len(day) == 1:
day = str(0) + day
return str(year) + "-" + str(month) + "-" + str(day)
def getFN():
fnList = []
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "csv/nrgAttendance.csv") as csvfile:
cr = csv.DictReader(csvfile)
for row in cr:
if fnList.count(row['First Name'].strip()) == 0:
fnList.append(row['First Name'].strip())
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "csv/nrgOutreach.csv") as csvfile:
cr = csv.DictReader(csvfile)
for row in cr:
if fnList.count(row['First Name'].strip()) == 0:
fnList.append(row['First Name'].strip())
return fnList
def getLN():
lnList = []
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "csv/nrgAttendance.csv") as csvfile:
cr = csv.DictReader(csvfile)
for row in cr:
if lnList.count(row['Last Name'].strip()) == 0:
lnList.append(row['Last Name'].strip())
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "csv/nrgOutreach.csv") as csvfile:
cr = csv.DictReader(csvfile)
for row in cr:
if lnList.count(row['Last Name'].strip()) == 0:
lnList.append(row['Last Name'].strip())
return lnList
def jsonifyFN():
fnList = getFN()
JSON = "["
for x in fnList:
JSON = JSON + '''{"label":"''' + html_escape(x) + '''"},'''
JSON = JSON[:len(JSON) - 1] + ']'
return JSON
def jsonifyLN():
lnList = getLN()
JSON = "["
for x in lnList:
JSON = JSON + '''{"label":"''' + html_escape(x) + '''"},'''
JSON = JSON[:len(JSON) - 1] + ']'
return JSON
@app.route("/")
def index():
try:
oldRef = remove_html_tags(request.args.get('oldRef', None))
except:
oldRef = None
if not oldRef == None:
flash(u'''It appears that you have been redirected from our old domain (''' + oldRef + '''). Please update your bookmarks to the new domain (<a href="http://nrgRecords.cf/">http://nrgRecords.cf/</a>). Thanks!''', 'warning')
flashcfg = file_get_contents("globalHeaderMSG.json")
flashcfg = json.loads(flashcfg)
if flashcfg['enabled']:
flash(flashcfg['message'], flashcfg['category'])
return render_template("header.html", randBackground=randBG()) + render_template("panel_empty.html") + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
@app.route("/record")
def record():
flashcfg = file_get_contents("globalHeaderMSG.json")
flashcfg = json.loads(flashcfg)
if flashcfg['enabled']:
flash(flashcfg['message'], flashcfg['category'])
fn = request.args.get('fn', 'null')
ln = request.args.get('ln', 'null')
#0 = Both Attendance & Outreach works
#1 = Both fails
#2 = Attendance fails, not outreach
#3 = Outreach fails, not attendance
error = 0
if (fn.lower().title() in getFN() and ln.lower().title() in getLN()):
try:
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "csv/nrgAttendance.csv") as csvfile:
cr = csv.DictReader(csvfile)
for row in cr:
if row['First Name'].replace(" ", "").lower() == fn.replace(" ", "").lower() and row['Last Name'].replace(" ", "").lower() == ln.replace(" ", "").lower():
dictionary = row
del dictionary['First Name']
del dictionary['Last Name']
dateCount = 0
attendedCount = 0
for x in dictionary:
dateCount = dateCount + 1
if dictionary[x].lower() == "x":
attendedCount = attendedCount + 1
attnPercentage = (float(attendedCount) / dateCount) * 100
attendanceDictionary = {}
for key in dictionary:
convertedKey = convertDate(key)
if convertedKey not in attendanceDictionary:
attendanceDictionary[convertedKey] = 0
if dictionary[key] == "x":
attendanceDictionary[convertedKey] = attendanceDictionary[convertedKey] + 1
else:
if dictionary[key] == "x":
attendanceDictionary[convertedKey] = attendanceDictionary[convertedKey] + 1
attendanceJSON = "{"
for key in attendanceDictionary:
if attendanceDictionary[key] == 0:
attendanceJSON = attendanceJSON + '"' + key + '''":{},'''
else:
attendanceJSON = attendanceJSON + '"' + key + '''":{"number": ''' + str(attendanceDictionary[key]) + '},'
attendanceJSON = attendanceJSON[:len(attendanceJSON) - 1] + '}'
except:
error = 2
try:
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "csv/nrgOutreach.csv") as csvfile:
cr = csv.DictReader(csvfile)
for row in cr:
if row['First Name'].replace(" ", "").lower() == fn.replace(" ", "").lower() and row['Last Name'].replace(" ", "").lower() == ln.replace(" ", "").lower():
odictionary = row
del odictionary['First Name']
del odictionary['Last Name']
outreachStr = ""
totalHrs = 0
for x in odictionary:
if odictionary[x] == "":
odictionary[x] = 0
outreachStr = outreachStr + x + ": "
outreachStr = outreachStr + str(odictionary[x])
totalHrs = totalHrs + float(odictionary[x])
outreachStr = outreachStr + "</br>"
outreachStr = outreachStr + '''<hr><h4 style="color: black;"><strong>Total hours participated in outreach: ''' + str(totalHrs) + "</strong></h4>"
except:
if error == 2:
error = 1
else:
error = 3
if error == 0:
return render_template("header.html", randBackground=randBG()) + render_template("panel_content.html", firstName=fn, lastName=ln, attnPercentage=attnPercentage, outreachData=outreachStr) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("calendarData.html", calendarJSON=attendanceJSON) + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
elif error == 1:
return render_template("header.html", randBackground=randBG()) + render_template("panel_bothMissing.html", firstName=fn, lastName=ln) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
elif error == 2:
return render_template("header.html", randBackground=randBG()) + render_template("panel_attendanceMissing.html", firstName=fn, lastName=ln, outreachData=outreachStr) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
elif error == 3:
return render_template("header.html", randBackground=randBG()) + render_template("panel_outreachMissing.html", firstName=fn, lastName=ln, attnPercentage=attnPercentage) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("calendarData.html", calendarJSON=attendanceJSON) + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
else:
return render_template("header.html", randBackground=randBG()) + render_template("panel_bothMissing.html", firstName=fn, lastName=ln) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
else:
return render_template("header.html", randBackground=randBG()) + render_template("panel_bothMissing.html", firstName=fn, lastName=ln) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
return render_template("header.html", randBackground=randBG()) + render_template("panel_bothMissing.html", firstName=fn, lastName=ln) + render_template("reminderPanel.html", attendancePanelContent=getAttendancePanelContent(), outreachPanelContent=getOutreachPanelContent()) + render_template("endScripts.html") + render_template("footer.html", fnTypeaheadJSON=jsonifyFN(), lnTypeaheadJSON=jsonifyLN())
### DOWNLOAD CSVS ###
@app.route('/csv/<filename>')
def download_file(filename):
return send_from_directory(app.config['CSV_UPLOAD_FOLDER'],
filename, as_attachment=True)
### ADMIN PANEL ###
CSV_UPLOAD_FOLDER = os.path.dirname(os.path.realpath(__file__)) + "/" + "csv"
ALLOWED_EXTENSIONS = set(['csv', 'xlsx', 'xls'])
app.config['CSV_UPLOAD_FOLDER'] = CSV_UPLOAD_FOLDER
app_config = file_get_contents("config.json")
app_config = json.loads(app_config)
GOOGLE_CLIENT_ID = app_config["gClientID"]
GOOGLE_CLIENT_SECRET = app_config["gClientSecret"]
REDIRECT_URI = '/oauth2callback' # one of the Redirect URIs from Google APIs console
SECRET_KEY = 'development key'
app.secret_key = SECRET_KEY
oauth = OAuth()
google = oauth.remote_app('google',
base_url='https://www.google.com/accounts/',
authorize_url='https://accounts.google.com/o/oauth2/auth',
request_token_url=None,
request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code'},
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_method='POST',
access_token_params={'grant_type': 'authorization_code'},
consumer_key=GOOGLE_CLIENT_ID,
consumer_secret=GOOGLE_CLIENT_SECRET)
def loggedInCheck():
access_token = session.get('access_token')
if access_token is None:
return False
access_token = access_token[0]
from urllib2 import Request, urlopen, URLError
headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
None, headers)
try:
res = urlopen(req)
except URLError, e:
if e.code == 401:
# Unauthorized - bad token
session.pop('access_token', None)
return False
loginStr = res.read()
loginStr = json.loads(loginStr)
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "admins.txt") as f:
adminEmails = f.read().splitlines()
if loginStr['email'] in adminEmails:
return True
else:
return False
def getAdminEmail():
access_token = session.get('access_token')
if access_token is None:
return "null"
access_token = access_token[0]
from urllib2 import Request, urlopen, URLError
headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
None, headers)
try:
res = urlopen(req)
except URLError, e:
if e.code == 401:
# Unauthorized - bad token
session.pop('access_token', None)
return "null"
loginStr = res.read()
loginStr = json.loads(loginStr)
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "admins.txt") as f:
adminEmails = f.read().splitlines()
if loginStr['email'] in adminEmails:
return str(loginStr['email'])
else:
return "null"
@app.route('/admin')
def admin():
loggedIn = loggedInCheck()
if loggedIn:
return render_template("adminPanel.html", adminEmail=getAdminEmail())
else:
return redirect(url_for('login'))
@app.route('/login')
def login():
return render_template("adminLogin.html")
@app.route('/login-process')
def login_process():
callback=url_for('authorized', _external=True)
return google.authorize(callback=callback)
@app.route('/logout')
def logout():
session.pop('access_token', None)
return redirect(url_for('admin'))
@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
access_token = resp['access_token']
session['access_token'] = access_token, ''
return redirect(url_for('admin'))
@google.tokengetter
def get_access_token():
return session.get('access_token')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def excel2csvAttendance(fileName):
wb = xlrd.open_workbook(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + fileName)
sh = wb.sheet_by_name('Main')
your_csv_file = open('csv/nrgAttendance.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
@app.route('/uploadAttendance', methods=['GET', 'POST'])
def uploadAttendance():
loggedIn = loggedInCheck()
if loggedIn:
if request.method == 'POST':
afile = request.files['file']
if afile and allowed_file(afile.filename):
filename = secure_filename(afile.filename)
afile.save(os.path.join(app.config['CSV_UPLOAD_FOLDER'], filename))
if filename[len(filename)-3:] == "csv":
move(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + filename, os.path.dirname(os.path.realpath(__file__)) + "/csv/nrgAttendance.csv")
flash(u'Successfully uploaded a CSV file!', 'success')
else:
try:
os.remove(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + "nrgAttendance.csv")
except:
pass
excel2csvAttendance(filename)
os.remove(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + filename)
flash(u'Successfully uploaded a Microsoft Excel file!', 'success')
else:
flash(u'Only .csv, .xls, and .xlsx are accepted!', 'danger')
return render_template('uploadAttendance.html', adminEmail=str(getAdminEmail()))
else:
return redirect(url_for('logout'))
def excel2csvOutreach(fileName):
wb = xlrd.open_workbook(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + fileName)
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('csv/nrgOutreach.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
@app.route('/uploadOutreach', methods=['GET', 'POST'])
def uploadOutreach():
loggedIn = loggedInCheck()
if loggedIn:
if request.method == 'POST':
afile = request.files['file']
if afile and allowed_file(afile.filename):
filename = secure_filename(afile.filename)
afile.save(os.path.join(app.config['CSV_UPLOAD_FOLDER'], filename))
if filename[len(filename)-3:] == "csv":
move(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + filename, os.path.dirname(os.path.realpath(__file__)) + "/csv/nrgOutreach.csv")
flash(u'Successfully uploaded a CSV file!', 'success')
else:
try:
os.remove(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + "nrgOutreach.csv")
except:
pass
excel2csvOutreach(filename)
os.remove(os.path.dirname(os.path.realpath(__file__)) + "/csv/" + filename)
flash(u'Successfully uploaded a Microsoft Excel file!', 'success')
else:
flash(u'Only .csv, .xls, and .xlsx are accepted!', 'danger')
return render_template('uploadOutreach.html', adminEmail=getAdminEmail())
else:
return redirect(url_for('logout'))
@app.route('/editAttendancePanel', methods=['GET','POST'])
def editAttendancePanel():
loggedIn = loggedInCheck()
if loggedIn:
if request.method == 'POST':
boxContent = request.form['textContent']
fo = open(os.path.dirname(os.path.realpath(__file__)) + "/" + "panelContent/attendancePanel.txt", "wb")
fo.write(boxContent);
flash(u'Successfully updated attendance panel!', 'success')
return redirect(url_for('editAttendancePanel'))
return render_template("editAttendancePanel.html", adminEmail=getAdminEmail(), prefilledContent=file_get_contents("panelContent/attendancePanel.txt"))
else:
return redirect(url_for('logout'))
@app.route('/editOutreachPanel', methods=['GET','POST'])
def editOutreachPanel():
loggedIn = loggedInCheck()
if loggedIn:
if request.method == 'POST':
boxContent = request.form['textContent']
fo = open(os.path.dirname(os.path.realpath(__file__)) + "/" + "panelContent/outreachPanel.txt", "wb")
fo.write(boxContent);
flash(u'Successfully updated outreach panel!', 'success')
return redirect(url_for('editOutreachPanel'))
return render_template("editOutreachPanel.html", adminEmail=getAdminEmail(), prefilledContent=file_get_contents("panelContent/outreachPanel.txt"))
else:
return redirect(url_for('logout'))
@app.route('/editAdmins', methods=['GET','POST'])
def editAdmins():
loggedIn = loggedInCheck()
if loggedIn:
if request.method == 'POST':
boxContent = request.form['textContent']
fo = open(os.path.dirname(os.path.realpath(__file__)) + "/" + "admins.txt", "wb")
fo.write(boxContent);
flash(u'Successfully updated admins list!', 'success')
return redirect(url_for('editAdmins'))
return render_template("editAdmins.html", adminEmail=getAdminEmail(), prefilledContent=file_get_contents("admins.txt"))
else:
return redirect(url_for('logout'))
@app.route('/editGlobalMSG', methods=['GET','POST'])
def editGlobalMSG():
loggedIn = loggedInCheck()
if loggedIn:
if request.method == 'POST':
fstate = request.form['enabled'] == 'True'
fcategory = request.form['category']
fmessage = request.form['message']
with open(os.path.dirname(os.path.realpath(__file__)) + "/" + "globalHeaderMSG.json", 'r+') as f:
json_data = json.load(f)
json_data['enabled'] = fstate
json_data['category'] = fcategory
json_data['message'] = fmessage
f.seek(0)
f.write(json.dumps(json_data))
f.truncate()
flash(u'Successfully updated global header message!', 'success')
return redirect(url_for('editGlobalMSG'))
globalMSGDef = file_get_contents("globalHeaderMSG.json")
globalMSGDef = json.loads(globalMSGDef)
return render_template("editGlobalMSG.html", adminEmail=getAdminEmail(), fstate=globalMSGDef['enabled'], fcategory=globalMSGDef['category'], fmessage=globalMSGDef['message'])
else:
return redirect(url_for('logout'))
# Github Webhook - Auto Update site for deployment
@app.route("/github-update", methods=["POST"])
def github_update():
app_config = file_get_contents("config.json")
app_config = json.loads(app_config)
h = hmac.new(str(app_config["githubWebhookSecret"]), request.data, hashlib.sha1)
if h.hexdigest() != request.headers.get("X-Hub-Signature", "")[5:]: # A timing attack here is nearly impossible.
return "FAIL"
try:
subprocess.Popen("git pull", shell=True).wait()
except OSError:
return "ERROR"
return "OK"
if __name__ == "__main__":
app_config = file_get_contents("config.json")
app_config = json.loads(app_config)
app.run(host=str(app_config["ip"]),port=int(float(str(app_config["port"]))),debug=app_config["debug"] in ["True","true"])