diff --git a/app.py b/app.py index f1da1fe..e7fca03 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,15 @@ from flask import * import pyrebase from datetime import datetime +import time import pytz from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail import csv import os -# from dotenv import load_dotenv -# from pprint import pprint -# load_dotenv() +from dotenv import load_dotenv +from pprint import pprint +load_dotenv() app = Flask(__name__) app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') @@ -34,6 +35,158 @@ def check_login_status(): (datetime.now(tz) - session['loginTime']).total_seconds() > 3600) +def manageProcess(fCommand, fData): + # this is to fix a bug where pyrebase doesnt load the first request + db.child("Users").child( + session['uid']).child("permission").get().val() + # end bug fix + pl = db.child("Users").child( + session['uid']).child("permission").get().val() + print(pl) + print(db.child("Users").child( + 'DRZqqSSpg3OkPSCuWkv417dv0vh1').child("permission").get().val()) + print(fCommand, fData, session['uid'], pl) + if pl == 'admin': + return pl + elif pl == 'group': + classes = db.child("Users").child( + session['uid']).child("class").get().val() + cclass = {} + for i in classes: + cclass = { + "name": db.child("Classes").child(i).child( + "Class").child(classes[i]).child("name").get().val(), + "category": i, + "class_id": classes[i] + } + print("got class") + students = db.child("Classes").child(cclass['category']).child( + "Class").child(cclass['class_id']).child("Students").get().val() + print(students['9']['11']) + all_stud_list = {} + for grade in students: + print(grade) + all_stud_list[grade] = {} + print(type(students[grade])) + for homeroom in students[grade]: + print(homeroom) + roomData = db.child("Homerooms").child( + grade).child(homeroom).get().val() + all_stud_list[grade][homeroom] = {} + if type(students[grade][homeroom]) == list: + i = 0 + for student in students[grade][homeroom]: + if student == 0: + all_stud_list[grade][homeroom][i] = { + "name": roomData[str(i)]["name"], + "eng_name": roomData[str(i)]["eng_name"] + } + i += 1 + else: + for student in students[grade][homeroom]: + print(student) + all_stud_list[grade][homeroom][student] = { + "name": roomData[student]["name"], + "eng_name": roomData[student]["eng_name"] + } + print("got students") + dates = db.child("Classes").child( + cclass['category']).child("Dates").get().val() + status = 0 + attendance = {} + + if fCommand == 'date': + currDate = fData + if cclass['class_id'] in dates[currDate]: + status = 1 + for grade in dates[currDate]['Absent']: + attendance[grade] = {} + for homeroom in dates[currDate]['Absent'][grade]: + attendance[grade][homeroom] = {} + for student in dates[currDate]['Absent'][grade][homeroom]: + attendance[grade][homeroom][student] = 0 + else: + for i in dates: + if i >= datetime.now(tz).strftime("%Y-%m-%d"): + currDate = i + if cclass['class_id'] in dates[currDate]: + status = 1 + for grade in dates[currDate]['Absent']: + attendance[grade] = {} + for homeroom in dates[currDate]['Absent'][grade]: + attendance[grade][homeroom] = {} + for student in dates[currDate]['Absent'][grade][homeroom]: + attendance[grade][homeroom][student] = 0 + break + dates[i].pop('placeholder') + print("got dates") + return render_template('group_teach.html', cclass=cclass, all_stud_list=all_stud_list, dates=dates, currDate=currDate, status=status, attendance=attendance) + elif pl == 'homeroom': + homeroom = db.child("Users").child( + session['uid']).child("homeroom").get().val() + homeroomCode = homeroom.split('^') + homeroom = db.child("Homerooms").child( + homeroomCode[0]).child(homeroomCode[1]).get().val() + categories = homeroom['Categories'].split('^') + currCategory = categories[0] + currDate = "" + gpClasses = db.child("Classes").child(currCategory).get().val() + dates = gpClasses['Dates'] + confirmedClasses = [] + status = 0 + + if fCommand == 'date': + currDate = fData + tmp1 = 0 + tmp2 = 0 + for k in gpClasses['Class']: + if k in dates[currDate]: + confirmedClasses.append(k) + tmp2 += 1 + tmp1 += 1 + if tmp1 == tmp2: + status = 1 + else: + for i in dates: + if i >= datetime.now(tz).strftime("%Y-%m-%d"): + currDate = i + tmp1 = 0 + tmp2 = 0 + for k in gpClasses['Class']: + if k in dates[currDate]: + confirmedClasses.append(k) + tmp2 += 1 + tmp1 += 1 + if tmp1 == tmp2: + status = 1 + break + print("got dates") + db.child("Classes").child(currCategory).child("Dates") + homeroom.pop('Categories') + all_stud_list = {} + for i in homeroom: + all_stud_list[i] = {} + all_stud_list[i]['name'] = homeroom[i]['name'] + all_stud_list[i]['eng_name'] = homeroom[i]['eng_name'] + all_stud_list[i]['gpClass'] = homeroom[i]['Classes'][currCategory] + if all_stud_list[i]['gpClass'] in confirmedClasses: + if (homeroomCode[0] in gpClasses['Dates'][currDate]['Absent'] and + homeroomCode[1] in gpClasses['Dates'][currDate]['Absent'][homeroomCode[0]] and + i in gpClasses['Dates'][currDate]['Absent'][homeroomCode[0]][homeroomCode[1]]): + # confirmed by teacher and absent + all_stud_list[i]['status'] = 2 + else: + # confirmed by teacher and not absent + all_stud_list[i]['status'] = 1 + else: + all_stud_list[i]['status'] = 0 # not yet confirmed by teacher + return render_template('homeroom.html', all_stud_list=all_stud_list, currDate=currDate, dates=dates, gpClasses=gpClasses, confirmedClasses=confirmedClasses, + currCategory=currCategory, categories=categories, homeroomCode=homeroomCode, status=status) + + else: + return redirect('/') + + @ app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'GET': @@ -60,75 +213,46 @@ def index(): @app.route('/manage', methods=['GET']) def manage(): - pl = db.child("Users").child( - session['uid']).child("permission").get().val() - print(pl) - s = str(pl) - if pl == 'admin': - return s - elif pl == 'group': - classes = db.child("Users").child( - session['uid']).child("class").get().val() - cclass = {} - for i in classes: - cclass = { - "name": db.child("Classes").child(i).child( - "Class").child(classes[i]).child("name").get().val(), - "category": i, - "class_id": classes[i] - } - print("got class") - students = db.child("Classes").child(cclass['category']).child( - "Class").child(cclass['class_id']).child("Students").get().val() - all_stud_list = {} - for homeroom in students: - print(homeroom) - all_stud_list[homeroom] = {} - if type(students[homeroom]) == list: - i = 0 - for student in students[homeroom]: - if student == 0: - # print(i) - # print(db.child("Homerooms").child( - # homeroom).child(i).child("name").get().val()) - all_stud_list[homeroom][i] = { - "name": db.child("Homerooms").child(homeroom).child(i).child("name").get().val(), - "eng_name": db.child("Homerooms").child(homeroom).child(i).child("eng_name").get().val(), - } - i += 1 - else: - for student in students[homeroom]: - all_stud_list[homeroom][student] = { - "name": db.child("Homerooms").child(homeroom).child(student).child("name").get().val(), - "eng_name": db.child("Homerooms").child(homeroom).child(student).child("eng_name").get().val(), - } + return manageProcess("", "") - print("got students") - # for homeroom in all_stud_list: - # for student in all_stud_list[homeroom]: - # print("homeroom: ", homeroom) - # print("student: ", student) - # print("all_stud_list['homeroom']['student']['name']: ", - # all_stud_list['homeroom']['student']['name']) - # print("all_stud_list['homeroom']['student']['eng_name']: ", - # all_stud_list['homeroom']['student']['eng_name']) - # get dates - dates = db.child("Classes").child( - cclass['category']).child("Dates").get().val() - for i in dates: - dates[i].pop('placeholder') - if i >= datetime.now(tz).strftime("%Y-%m-%d"): - currDate = i - break - print("got dates") - return render_template('group_teach.html', cclass=cclass, all_stud_list=all_stud_list, dates=dates, currDate=currDate) - elif pl == 'homeroom': - homeroom = db.child("Users").child( - session['uid']).child("homeroom").get().val() - s += " " + homeroom # 912 - return s - else: - return "no permission" + +@app.route('/manage/date', methods=['POST']) +def manage_date(): + return manageProcess("date", request.form['date']) + + +@app.route('/manage/group_teach_publish', methods=['POST']) +def group_teach_publish(): + classes = db.child("Users").child( + session['uid']).child("class").get().val() + cclass = {} + for i in classes: + cclass = { + "name": db.child("Classes").child(i).child( + "Class").child(classes[i]).child("name").get().val(), + "category": i, + "class_id": classes[i] + } + print("got class") + cDate = "" + for key in request.form.keys(): + print(type(key), key) + if key == 'date': + print('here') + cDate = request.form[key] + db.child("Classes").child(cclass['category']).child( + "Dates").child(request.form[key]).update({'confirmed': 0}) + db.child("Classes").child(cclass['category']).child( + "Dates").child(request.form[key]).update({cclass['class_id']: request.form['signatureData']}) + elif key == 'signatureData': + pass + else: + # spilt string + id = key.split('^') + print(id) + db.child("Classes").child(cclass['category']).child("Dates").child( + cDate).child("Absent").child(id[0]).child(id[1]).update({id[2]: 1}) + return "Success!" @ app.route('/upload/homeroom', methods=['GET', 'POST']) @@ -138,6 +262,7 @@ def upload_homeroom(): elif request.method == 'POST': try: # get csv + gradec = request.form['gradeCode'] classc = request.form['classcode'] csv_file = request.files['csv'] filepath = os.path.join('./temp', csv_file.filename) @@ -145,14 +270,14 @@ def upload_homeroom(): with open(filepath) as file: csv_dict = csv.DictReader(file) for row in csv_dict: - db.child("Homerooms").child( + db.child("Homerooms").child(gradec).child( classc).child(row['number']).set(row) # row['class'] row['number'] row['name'] row['eng_name'] os.remove(filepath) except Exception as e: os.remove(filepath) return "Error. Please try again\n("+str(e)+")" - return "Successfully uploaded " + classc + return "Successfully uploaded " + gradec + "-" + classc @ app.route('/upload/stud_in_group', methods=['GET', 'POST']) @@ -161,6 +286,7 @@ def upload_stud_in_group(): return render_template('uploadcsv.html', title="Student in Group List", url="/upload/stud_in_group") elif request.method == 'POST': try: + gradec = request.form['gradeCode'] classc = request.form['classcode'] csv_file = request.files['csv'] filepath = os.path.join('./temp', csv_file.filename) @@ -171,16 +297,16 @@ def upload_stud_in_group(): headers = headers[1:] for row in csv_dict: for h in headers: - db.child("Homerooms").child(classc).child( + db.child("Homerooms").child(gradec).child(classc).child( row['number']).child("Classes").child(h).set(row[h]) db.child("Classes").child(h).child("Class").child(row[h]).child( - "Students").child(classc).update({str(row['number']): 0}) + "Students").child(gradec).child(classc).update({str(row['number']): 0}) os.remove(filepath) except Exception as e: os.remove(filepath) return "Error. Please try again\n("+str(e)+")" - return "Successfully uploaded " + classc + return "Successfully uploaded " + gradec + "-" + classc # @ app.route('/upload/rm_all_data_of_class', methods=['GET', "POST"]) @@ -202,5 +328,5 @@ def logout(): return redirect('/') -# if __name__ == '__main__': -# app.run(debug=True) +if __name__ == '__main__': + app.run(debug=True) diff --git a/static/allpages.css b/static/allpages.css index 646735d..2486ace 100644 --- a/static/allpages.css +++ b/static/allpages.css @@ -1,3 +1,47 @@ div.col .row .col { border: 1px solid black; +} + +div.signatures { + margin-top: 30px; +} + +div.signatures .col .row { + border: 1px solid black; +} + +div.forSign { + border: 1px solid black; + margin-left: 10%; + margin-right: 10%; + width: 80%; + height: 300px; + position: relative; + -webkit-box-flex: 1; + -ms-flex: 1; + flex: 1; +} + +div.forSign canvas { + position: absolute; + left: 0; + top: 0; + width: 100%; + height: 100%; +} + +p.highlightAbs { + margin-bottom: 0; +} + +p.highlightAbs.n-1 { + color: blue; +} + +p.highlightAbs.n-2 { + color: red; +} + +div.classlist { + text-align: left; } \ No newline at end of file diff --git a/temp/placeholderfile.txt b/temp/placeholderfile.txt new file mode 100644 index 0000000..8f11eb7 --- /dev/null +++ b/temp/placeholderfile.txt @@ -0,0 +1 @@ +this is just so that /temp doesn't disappear \ No newline at end of file diff --git a/templates/group_teach.html b/templates/group_teach.html index b3d1c7e..9b62b63 100644 --- a/templates/group_teach.html +++ b/templates/group_teach.html @@ -13,30 +13,102 @@
-