Done with upload format

This commit is contained in:
Aaron Lee 2021-09-14 22:55:11 +08:00
parent 419a71be2b
commit 745b43dff3
5 changed files with 278 additions and 212 deletions

114
app.py
View file

@ -41,6 +41,10 @@ def check_login_status():
(datetime.now(tz) - session['loginTime']).total_seconds() > 3600) (datetime.now(tz) - session['loginTime']).total_seconds() > 3600)
def check_permission():
return db.child('Users').child(session['uid']).child('permission').get().val() == 'admin'
def manageProcess(fCommand, fData): def manageProcess(fCommand, fData):
if (check_login_status()): if (check_login_status()):
return redirect('/logout') return redirect('/logout')
@ -65,6 +69,7 @@ def manageProcess(fCommand, fData):
homeroomData = homerooms[currRoom[0]][currRoom[1]] homeroomData = homerooms[currRoom[0]][currRoom[1]]
absData = homeroomData["Absent"] absData = homeroomData["Absent"]
homeroomData.pop('Absent') homeroomData.pop('Absent')
if 'placeholder' in homeroomData:
homeroomData.pop('placeholder') homeroomData.pop('placeholder')
currDate = "" currDate = ""
if fCommand != "": if fCommand != "":
@ -85,8 +90,6 @@ def manageProcess(fCommand, fData):
"GP_Class").child(i).get().val() "GP_Class").child(i).get().val()
cclass = { cclass = {
"name": cateData['Class'][classes[i]]['name'], "name": cateData['Class'][classes[i]]['name'],
"teacher": cateData['Class'][classes[i]]['teacher'],
"classroom": cateData['Class'][classes[i]]['classroom'],
"category": i, "category": i,
"class_id": classes[i] "class_id": classes[i]
} }
@ -100,6 +103,7 @@ def manageProcess(fCommand, fData):
hrData = db.child("Homerooms").child(h[0]).child(h[1]).get().val() hrData = db.child("Homerooms").child(h[0]).child(h[1]).get().val()
tmpAbsData = hrData['Absent'] tmpAbsData = hrData['Absent']
hrData.pop('Absent') hrData.pop('Absent')
if 'placeholder' in hrData:
hrData.pop('placeholder') hrData.pop('placeholder')
periods = [] periods = []
dow = "" dow = ""
@ -196,6 +200,7 @@ def manageProcess(fCommand, fData):
break break
absData = homeroomData["Absent"] absData = homeroomData["Absent"]
homeroomData.pop('Absent') homeroomData.pop('Absent')
if 'placeholder' in homeroomData:
homeroomData.pop('placeholder') homeroomData.pop('placeholder')
currDate = "" currDate = ""
if fCommand == 'date': if fCommand == 'date':
@ -221,7 +226,7 @@ def index():
if check_login_status(): if check_login_status():
try: try:
user = auth.sign_in_with_email_and_password( user = auth.sign_in_with_email_and_password(
request.form['username'] + "@group-attendence.fhjh.tp.edu.tw", request.form['password']) request.form['username'] + "@group-attendance.fhjh.tp.edu.tw", request.form['password'])
session['is_logged_in'] = True session['is_logged_in'] = True
session['email'] = user['email'] session['email'] = user['email']
session['uid'] = user['localId'] session['uid'] = user['localId']
@ -297,7 +302,6 @@ def group_teach_publish():
h = h.split('^') h = h.split('^')
db.child("Homerooms").child(h[0]).child(h[1]).child( db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).child("signature").update({cclass['class_id']: str(storage.child(os.path.join('signatures', rand)).get_url(None))}) "Absent").child(date).child(period).child("signature").update({cclass['class_id']: str(storage.child(os.path.join('signatures', rand)).get_url(None))})
# upload notes
currPeriodData = db.child("Homerooms").child(h[0]).child(h[1]).child( currPeriodData = db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).get().val() "Absent").child(date).child(period).get().val()
if 'notes' in currPeriodData: if 'notes' in currPeriodData:
@ -306,6 +310,8 @@ def group_teach_publish():
else: else:
db.child("Homerooms").child(h[0]).child(h[1]).child( db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).update({'notes': notes}) "Absent").child(date).child(period).update({'notes': notes})
# upload notes
os.remove(os.path.join('temp', rand)) os.remove(os.path.join('temp', rand))
return redirect('/manage') return redirect('/manage')
@ -368,10 +374,11 @@ def homeroom_confirm():
return redirect('/manage') return redirect('/manage')
@ app.route('/upload/homeroom', methods=['GET', 'POST']) @ app.route('/upload/1', methods=['GET', 'POST'])
def upload_homeroom(): def upload_homeroom():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET': if request.method == 'GET':
return render_template('uploadcsv.html', title="Homeroom List", url="/upload/homeroom") return render_template('uploadcsv.html', title="Homeroom List", url="/upload/1")
elif request.method == 'POST': elif request.method == 'POST':
try: try:
# get csv # get csv
@ -383,6 +390,17 @@ def upload_homeroom():
with open(filepath) as file: with open(filepath) as file:
csv_dict = csv.DictReader(file) csv_dict = csv.DictReader(file)
for row in csv_dict: for row in csv_dict:
if row['number'] == 'password':
auth.create_user_with_email_and_password(
gradec + '^' + classc + "@group-attendance.fhjh.tp.edu.tw", row['name'])
user = auth.sign_in_with_email_and_password(
gradec + '^' + classc + "@group-attendance.fhjh.tp.edu.tw", row['name'])
db.child("Users").child(user['localId']).update({
"permission": 'homeroom',
"username": gradec + '^' + classc,
"homeroom": gradec + '^' + classc
})
else:
db.child("Homerooms").child(gradec).child( db.child("Homerooms").child(gradec).child(
classc).child(row['number']).set(row) classc).child(row['number']).set(row)
# row['class'] row['number'] row['name'] row['eng_name'] # row['class'] row['number'] row['name'] row['eng_name']
@ -391,12 +409,15 @@ def upload_homeroom():
os.remove(filepath) os.remove(filepath)
return "Error. Please try again\n("+str(e)+")" return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc return "Successfully uploaded " + gradec + "-" + classc
else:
return redirect('/logout')
@ app.route('/upload/gp_classes', methods=['GET', 'POST']) @ app.route('/upload/2', methods=['GET', 'POST'])
def upload_gp_classes(): def upload_gp_classes():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET': if request.method == 'GET':
return render_template('uploadcsv.html', title="Group Classes", url="/upload/gp_classes") return render_template('uploadcsv.html', title="Group Classes", url="/upload/2")
elif request.method == 'POST': elif request.method == 'POST':
try: try:
csv_file = request.files['csv'] csv_file = request.files['csv']
@ -409,24 +430,34 @@ def upload_gp_classes():
for j in range(len(tmp_csv)): for j in range(len(tmp_csv)):
if type(tmp_csv[j]) == float: if type(tmp_csv[j]) == float:
break break
if j % 4 == 0: if j % 5 == 0:
db.child("Classes").child("GP_Class").child(csv_dict.columns[i+1]).child("Class").child( db.child("Classes").child("GP_Class").child(csv_dict.columns[i+1]).child("Class").child(
tmp_csv[j]).child("name").set(tmp_csv[j+1]) tmp_csv[j]).child("name").set(tmp_csv[j+1] + " : " + tmp_csv[j+2] + " (" + tmp_csv[j+3] + ")")
db.child("Classes").child("GP_Class").child(csv_dict.columns[i+1]).child("Class").child( auth.create_user_with_email_and_password(
tmp_csv[j]).child("teacher").set(tmp_csv[j+2]) csv_dict.columns[i+1] + "^" + tmp_csv[j] + "@group-attendance.fhjh.tp.edu.tw", tmp_csv[j+4])
db.child("Classes").child("GP_Class").child(csv_dict.columns[i+1]).child("Class").child( user = auth.sign_in_with_email_and_password(
tmp_csv[j]).child("classroom").set(tmp_csv[j+3]) csv_dict.columns[i+1] + "^" + tmp_csv[j] + "@group-attendance.fhjh.tp.edu.tw", tmp_csv[j+4])
db.child("Users").child(user['localId']).update({
"permission": 'group',
"username": csv_dict.columns[i+1] + "^" + tmp_csv[j],
"class": {
csv_dict.columns[i+1]: tmp_csv[j],
}
})
os.remove(filepath) os.remove(filepath)
except Exception as e: except Exception as e:
os.remove(filepath) os.remove(filepath)
return "Error. Please try again\n("+str(e)+")" return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded" return "Successfully uploaded"
else:
return redirect('/logout')
@ app.route('/upload/stud_in_group', methods=['GET', 'POST']) @ app.route('/upload/3', methods=['GET', 'POST'])
def upload_stud_in_group(): def upload_stud_in_group():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET': if request.method == 'GET':
return render_template('uploadcsv.html', title="Student in Group List", url="/upload/stud_in_group") return render_template('uploadcsv.html', title="Student in Group List", url="/upload/3")
elif request.method == 'POST': elif request.method == 'POST':
try: try:
gradec = request.form['gradeCode'] gradec = request.form['gradeCode']
@ -450,12 +481,15 @@ def upload_stud_in_group():
os.remove(filepath) os.remove(filepath)
return "Error. Please try again\n("+str(e)+")" return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc return "Successfully uploaded " + gradec + "-" + classc
else:
return redirect('/logout')
@ app.route('/upload/period_list', methods=['GET', 'POST']) @ app.route('/upload/4', methods=['GET', 'POST'])
def upload_period_list(): def upload_period_list():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET': if request.method == 'GET':
return render_template('uploadcsv.html', title="Period List", url="/upload/period_list") return render_template('uploadcsv.html', title="Period List", url="/upload/4")
elif request.method == 'POST': elif request.method == 'POST':
try: try:
# get csv # get csv
@ -485,10 +519,13 @@ def upload_period_list():
os.remove(filepath) os.remove(filepath)
return "Error. Please try again\n("+str(e)+")" return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc return "Successfully uploaded " + gradec + "-" + classc
else:
return redirect('/logout')
@ app.route('/upload/dates', methods=['GET', 'POST']) @ app.route('/upload/dates', methods=['GET', 'POST'])
def upload_dates(): def upload_dates():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET': if request.method == 'GET':
return render_template('uploadcsv.html', title="School Days", url="/upload/dates") return render_template('uploadcsv.html', title="School Days", url="/upload/dates")
elif request.method == 'POST': elif request.method == 'POST':
@ -517,19 +554,38 @@ def upload_dates():
os.remove(filepath) os.remove(filepath)
return "Error. Please try again\n("+str(e)+")" return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded dates" return "Successfully uploaded dates"
else:
return redirect('/logout')
# @ app.route('/upload/rm_all_data_of_class', methods=['GET', "POST"]) @app.route('/upload/admin_acc', methods=['GET', 'POST'])
# def rm_all_data_of_class(): def upload_admin_acc():
# if request.method == 'GET': if ((not check_login_status()) and check_permission()):
# return render_template('uploadcsv.html', title="Remove all data of class", url="/upload/rm_all_data_of_class") if request.method == 'GET':
# elif request.method == 'POST': return render_template('uploadcsv.html', title="Admin Accounts", url="/upload/admin_acc")
# try: elif request.method == 'POST':
# classc = request.form['classcode'] try:
# db.child("Homerooms").child(classc).remove() csv_file = request.files['csv']
# except Exception as e: filepath = os.path.join('./temp', csv_file.filename)
# return "Error. Please try again\n("+str(e)+")" csv_file.save(filepath)
# return "Successfully removed " + classc with open(filepath) as file:
csv_dict = csv.DictReader(file)
for row in csv_dict:
auth.create_user_with_email_and_password(
row['username'] + '@group-attendance.fhjh.tp.edu.tw', row['password'])
user = auth.sign_in_with_email_and_password(
row['username'] + '@group-attendance.fhjh.tp.edu.tw', row['password'])
db.child("Users").child(user['localId']).update({
'permission': 'admin',
'username': row['username']
})
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded admin accounts"
else:
return redirect('/logout')
@ app.route('/logout', methods=['GET']) @ app.route('/logout', methods=['GET'])

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View file

@ -52,6 +52,7 @@
</form> </form>
</div> </div>
<div class="col"> <div class="col">
<div class="sticky-top" style="background-color: white;">
<div class="row title"> <div class="row title">
<div class="col">HR</div> <div class="col">HR</div>
<div class="col">Number</div> <div class="col">Number</div>
@ -87,6 +88,7 @@
<div class="col">{{absData[currDate][i]['teacher']}}</div> <div class="col">{{absData[currDate][i]['teacher']}}</div>
{% endfor %} {% endfor %}
</div> </div>
</div>
{% if data != None %} {% if data != None %}
{% for i in homeroomData %} {% for i in homeroomData %}
<div class="row"> <div class="row">
@ -128,7 +130,7 @@
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% for c in range(periods|length + 1) %} {% for c in range(periods|length + 1) %}
{% if c % 2 == 0 %} {% if c % 4 == 0 %}
<div class="row signatures"> <div class="row signatures">
{% endif %} {% endif %}
<div class="col half"> <div class="col half">
@ -140,7 +142,8 @@
{% if 'signature' in absData[currDate][periods[c-1]] %} {% if 'signature' in absData[currDate][periods[c-1]] %}
{% for i in absData[currDate][periods[c-1]]['signature'] %} {% for i in absData[currDate][periods[c-1]]['signature'] %}
<div class="row">{{periods[c-1]}}: {{absData[currDate][periods[c-1]]['teacher']}}: {{i}}</div> <div class="row">{{periods[c-1]}}: {{absData[currDate][periods[c-1]]['teacher']}}: {{i}}</div>
<div class="row"><img src="{{absData[currDate][periods[c-1]]['signature'][i]}}" alt=""></div> <div class="row"><img src="{{absData[currDate][periods[c-1]]['signature'][i]}}" alt=""><br>備註:
{{absData[currDate][periods[c-1]]['notes']}}</div>
{% endfor %} {% endfor %}
{% else %} {% else %}
<div class="row">{{periods[c-1]}}: {{absData[currDate][periods[c-1]]['teacher']}}: No Signature <div class="row">{{periods[c-1]}}: {{absData[currDate][periods[c-1]]['teacher']}}: No Signature
@ -151,11 +154,12 @@
<div class="row">{{periods[c-1]}}: {{absData[currDate][periods[c-1]]['name']}}: <div class="row">{{periods[c-1]}}: {{absData[currDate][periods[c-1]]['name']}}:
{{absData[currDate][periods[c-1]]['teacher']}} {{absData[currDate][periods[c-1]]['teacher']}}
</div> </div>
<div class="row"><img src="{{absData[currDate][periods[c-1]]['signature']}}" alt=""></div> <div class="row"><img src="{{absData[currDate][periods[c-1]]['signature']}}"
alt=""><br>備註:{{absData[currDate][periods[c-1]]['notes']}}</div>
{% endif %} {% endif %}
{% endif %} {% endif %}
</div> </div>
{% if c % 2 == 1 %} {% if c % 4 == 3 %}
</div> </div>
{% endif %} {% endif %}
{% endfor %} {% endfor %}

View file

@ -5,7 +5,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Homeroom_View</title> <title>Attendance | Homeroom</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous"> integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<link rel="stylesheet" href="/static/allpages.css"> <link rel="stylesheet" href="/static/allpages.css">
@ -14,9 +14,9 @@
<body> <body>
<div class="container"> <div class="container">
<h1>Homeroom_View</h1> <h1>Attendance | Homeroom</h1>
<h2>{{homeroomCode[0]}}: {{homeroomCode[1]}}</h2> <h2>{{homeroomCode[0]}}: {{homeroomCode[1]}}</h2>
<h2>Status: ({{currDate}}, 星期{{absData[currDate]['dow']}})</h2> <h2>{{currDate}}, 依照 星期{{absData[currDate]['dow']}} 課表</h2>
<a href="/logout"><button class="btn btn-primary">Logout 登出</button></a> <a href="/logout"><button class="btn btn-primary">Logout 登出</button></a>
<form action="/manage/date" id="dateSelForm" method="post"> <form action="/manage/date" id="dateSelForm" method="post">
<select name="date" id="date" class="form-select" onchange="chgDate();"> <select name="date" id="date" class="form-select" onchange="chgDate();">
@ -137,10 +137,10 @@
</form> </form>
{% if 'confirm' in absData[currDate] %} {% if 'confirm' in absData[currDate] %}
<button class="btn btn-primary margin-top afterSelButton" onclick="homeroomCfrm()" disabled="disabled"> <button class="btn btn-primary margin-top afterSelButton" onclick="homeroomCfrm()" disabled="disabled">
Already Confirmed</button> Homeroom Teacher Already Confirmed | 班導已確認</button>
{% else %} {% else %}
<button class="btn btn-primary margin-top afterSelButton" onclick="homeroomCfrm()">Homeroom <button class="btn btn-primary margin-top afterSelButton" onclick="homeroomCfrm()">Homeroom
Teacher Confirm</button> Teacher Confirm | 班導已確認</button>
{% endif %} {% endif %}
<div id="finalCheck" hidden="hidden"> <div id="finalCheck" hidden="hidden">
<!-- show warning --> <!-- show warning -->

View file

@ -18,7 +18,7 @@
<div class="col-md-4 offset-md-4"> <div class="col-md-4 offset-md-4">
<h1 class="text-center">Upload {{title}}</h1> <h1 class="text-center">Upload {{title}}</h1>
<!-- upload csv --> <!-- upload csv -->
<form action="{{url}}" method="post" enctype="multipart/form-data"> <form action="{{url}}" id="uploadCsvForm" method="post" enctype="multipart/form-data">
<!-- input class code --> <!-- input class code -->
<div class="form-group"> <div class="form-group">
<label for="gradeCode">Grade Code</label> <label for="gradeCode">Grade Code</label>
@ -34,7 +34,7 @@
<label for="csv">{{title}}</label> <label for="csv">{{title}}</label>
<input type="file" class="form-control-file" id="csv" name="csv"> <input type="file" class="form-control-file" id="csv" name="csv">
</div> </div>
<button onclick="loadingAnimation()" type="submit" class="btn btn-primary">Upload</button> <button onclick="loadingAnimation()" type="button" class="btn btn-primary">Upload</button>
</form> </form>
<!-- show danger zone --> <!-- show danger zone -->
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
@ -61,6 +61,12 @@
<script type="text/javascript" src="/static/jquery.min.js"></script> <script type="text/javascript" src="/static/jquery.min.js"></script>
<script> <script>
function loadingAnimation() { function loadingAnimation() {
// if csv is empty
if ($("#csv").val() == "") {
alert("Please select a file!");
return;
}
$('#uploadCsvForm').submit();
$('.container').hide(); $('.container').hide();
$('#loading').show(); $('#loading').show();
} }