From a47d69a95482de279231909318d67e318c6e6828 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Fri, 10 Sep 2021 22:30:39 +0800 Subject: [PATCH] Day 2, added homeroom vie, date selection, and signatures (pending homeroom confirmation) --- app.py | 282 +++++++++++++++++++++++++++---------- static/allpages.css | 44 ++++++ temp/placeholderfile.txt | 1 + templates/group_teach.html | 114 ++++++++++++--- templates/homeroom.html | 152 ++++++++++++++++++++ templates/uploadcsv.html | 5 + 6 files changed, 499 insertions(+), 99 deletions(-) create mode 100644 temp/placeholderfile.txt 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 @@ -

Group_Teach_View

-

{{cclass['category']}}: {{cclass['class_id']}}: {{cclass['name']}}

-
-
-
Class Code
-
Number
-
Name
-
Eng Name
-
Attendance
+
+

Group_Teach_View

+

{{cclass['category']}}: {{cclass['class_id']}}: {{cclass['name']}}

+

Status: {{status}} ({{currDate}})

+
+ +
+ {% if status == 1 %} + - {% if data != None %} - {% for homeroom in all_stud_list %} - {% for student in all_stud_list[homeroom] %} -
-
{{homeroom}}
-
{{ student }}
-
{{ all_stud_list[homeroom][student]['name'] }}
-
{{ all_stud_list[homeroom][student]['eng_name'] }}
-
-
- {% endfor %} - {% endfor %} {% endif %} + {% if status == 0 %} +
+ + +
+
+
Grade
+
Class Code
+
Number
+
Name
+
Eng Name
+
Attendance (Check Absent)
+
+ {% if data != None %} + {% for grade in all_stud_list %} + {% for homeroom in all_stud_list[grade] %} + {% for student in all_stud_list[grade][homeroom] %} +
+
{{grade}}
+
{{homeroom}}
+
{{ student }}
+
{{ all_stud_list[grade][homeroom][student]['name'] }}
+
{{ all_stud_list[grade][homeroom][student]['eng_name'] }}
+
+
+ {% endfor %} + {% endfor %} + {% endfor %} + {% endif %} +

Please Sign Below

+
+ + +
+
+ + + {% endif %} + + + \ No newline at end of file diff --git a/templates/homeroom.html b/templates/homeroom.html index e69de29..4c40f56 100644 --- a/templates/homeroom.html +++ b/templates/homeroom.html @@ -0,0 +1,152 @@ + + + + + + + + Homeroom_View + + + + + + +
+

Homeroom_View

+

{{homeroomCode[0]}}: {{homeroomCode[1]}}

+

Status: ({{currDate}})

+
+ +
+
+ + + +
+
+
+
Homeroom
+
Number
+
Name
+
Eng Name
+
GP_Class
+
Attendance (Here=V, Abs=X)
+
+ {% if data != None %} + {% for i in all_stud_list %} +
+
{{homeroomCode[0]}}{{homeroomCode[1]}}
+
{{i}}
+
{{ all_stud_list[i]['name'] }}
+
{{ all_stud_list[i]['eng_name'] }}
+
{{ all_stud_list[i]['gpClass'] }}: + {{gpClasses['Class'][all_stud_list[i]['gpClass']]['name']}}
+
+

+ {% if all_stud_list[i]['status'] == 1 %} + V + {% elif all_stud_list[i]['status'] == 2 %} + X + {% endif %} +

+
+
+ {% endfor %} + {% endif %} + + {% for c in range(confirmedClasses|length) %} + {% if c % 2 == 0 %} +
+ {% endif %} +
+
{{confirmedClasses[c]}}: {{gpClasses['Class'][confirmedClasses[c]]['name']}} +
+
+
+ {% if c % 2 == 1 %} +
+ {% endif %} + {% endfor %} + +
+

Classes in {{currCategory}}

+
    + {% for c in gpClasses['Class'] %} +
  • +

    {{c}}: {{gpClasses['Class'][c]['name']}}

    +
  • +
      +
    • +

      Classroom: {{ gpClasses['Class'][c]['classroom'] }}

      +
    • +
    • +

      Teacher: {{ gpClasses['Class'][c]['teacher'] }}

      +
    • +
    + {% endfor %} +
+
+ {% if status == 1 %} +

Please Sign Below

+
+ + + {% else %} +

Please wait for all {{currCategory}} teachers to enter data.

+ {% endif %} +
+
+ {% if status == 1 %} + + + {% endif %} + + + + + + \ No newline at end of file diff --git a/templates/uploadcsv.html b/templates/uploadcsv.html index 2b3b45b..7fb32e5 100644 --- a/templates/uploadcsv.html +++ b/templates/uploadcsv.html @@ -20,6 +20,11 @@
+
+ + +