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

390
app.py
View file

@ -41,6 +41,10 @@ def check_login_status():
(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):
if (check_login_status()):
return redirect('/logout')
@ -65,7 +69,8 @@ def manageProcess(fCommand, fData):
homeroomData = homerooms[currRoom[0]][currRoom[1]]
absData = homeroomData["Absent"]
homeroomData.pop('Absent')
homeroomData.pop('placeholder')
if 'placeholder' in homeroomData:
homeroomData.pop('placeholder')
currDate = ""
if fCommand != "":
currDate = fData[1]
@ -85,8 +90,6 @@ def manageProcess(fCommand, fData):
"GP_Class").child(i).get().val()
cclass = {
"name": cateData['Class'][classes[i]]['name'],
"teacher": cateData['Class'][classes[i]]['teacher'],
"classroom": cateData['Class'][classes[i]]['classroom'],
"category": i,
"class_id": classes[i]
}
@ -100,7 +103,8 @@ def manageProcess(fCommand, fData):
hrData = db.child("Homerooms").child(h[0]).child(h[1]).get().val()
tmpAbsData = hrData['Absent']
hrData.pop('Absent')
hrData.pop('placeholder')
if 'placeholder' in hrData:
hrData.pop('placeholder')
periods = []
dow = ""
if currDate == "":
@ -196,7 +200,8 @@ def manageProcess(fCommand, fData):
break
absData = homeroomData["Absent"]
homeroomData.pop('Absent')
homeroomData.pop('placeholder')
if 'placeholder' in homeroomData:
homeroomData.pop('placeholder')
currDate = ""
if fCommand == 'date':
currDate = fData
@ -221,7 +226,7 @@ def index():
if check_login_status():
try:
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['email'] = user['email']
session['uid'] = user['localId']
@ -297,15 +302,16 @@ def group_teach_publish():
h = h.split('^')
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))})
currPeriodData = db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).get().val()
if 'notes' in currPeriodData:
db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).update({'notes': currPeriodData['notes']+'; '+notes})
else:
db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).update({'notes': notes})
# upload notes
currPeriodData = db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).get().val()
if 'notes' in currPeriodData:
db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).update({'notes': currPeriodData['notes']+'; '+notes})
else:
db.child("Homerooms").child(h[0]).child(h[1]).child(
"Absent").child(date).child(period).update({'notes': notes})
os.remove(os.path.join('temp', rand))
return redirect('/manage')
@ -368,168 +374,218 @@ def homeroom_confirm():
return redirect('/manage')
@ app.route('/upload/homeroom', methods=['GET', 'POST'])
@ app.route('/upload/1', methods=['GET', 'POST'])
def upload_homeroom():
if request.method == 'GET':
return render_template('uploadcsv.html', title="Homeroom List", url="/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)
csv_file.save(filepath)
with open(filepath) as file:
csv_dict = csv.DictReader(file)
for row in csv_dict:
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 " + gradec + "-" + classc
@ app.route('/upload/gp_classes', methods=['GET', 'POST'])
def upload_gp_classes():
if request.method == 'GET':
return render_template('uploadcsv.html', title="Group Classes", url="/upload/gp_classes")
elif request.method == 'POST':
try:
csv_file = request.files['csv']
filepath = os.path.join('./temp', csv_file.filename)
csv_file.save(filepath)
csv_dict = pd.read_csv(filepath)
category_cnt = csv_dict.shape[1] - 1
for i in range(category_cnt):
tmp_csv = csv_dict[csv_dict.columns[i+1]].tolist()
for j in range(len(tmp_csv)):
if type(tmp_csv[j]) == float:
break
if j % 4 == 0:
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])
db.child("Classes").child("GP_Class").child(csv_dict.columns[i+1]).child("Class").child(
tmp_csv[j]).child("teacher").set(tmp_csv[j+2])
db.child("Classes").child("GP_Class").child(csv_dict.columns[i+1]).child("Class").child(
tmp_csv[j]).child("classroom").set(tmp_csv[j+3])
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded"
@ app.route('/upload/stud_in_group', methods=['GET', 'POST'])
def upload_stud_in_group():
if request.method == 'GET':
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)
csv_file.save(filepath)
with open(filepath) as file:
csv_dict = csv.DictReader(file)
headers = csv_dict.fieldnames
headers = headers[1:]
for h in headers:
db.child("Classes").child("GP_Class").child(
h).child("Homerooms").update({gradec+'^'+classc: 0})
for row in csv_dict:
for h in headers:
db.child("Homerooms").child(gradec).child(classc).child(
row['number']).child("GP_Class").update({h: row[h]})
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc
@ app.route('/upload/period_list', methods=['GET', 'POST'])
def upload_period_list():
if request.method == 'GET':
return render_template('uploadcsv.html', title="Period List", url="/upload/period_list")
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)
csv_file.save(filepath)
csv_dict = pd.read_csv(filepath)
periodCodes = csv_dict['Period Day'].tolist()
for i in range(5):
tmp_csv = csv_dict[str(i+1)].tolist()
for j in range(len(tmp_csv)):
if not (periodCodes[j].endswith('-t')):
if type(tmp_csv[j]) == float:
db.child("Classes").child("Homeroom").child(gradec).child(classc).child(
str(i+1)).child(periodCodes[j]).update({'name': '--'})
if ((not check_login_status()) and check_permission()):
if request.method == 'GET':
return render_template('uploadcsv.html', title="Homeroom List", url="/upload/1")
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)
csv_file.save(filepath)
with open(filepath) as file:
csv_dict = csv.DictReader(file)
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("Classes").child("Homeroom").child(gradec).child(classc).child(
str(i+1)).child(periodCodes[j]).update({'name': tmp_csv[j]})
if not(periodCodes[j] == 'm' or periodCodes[j] == 'n'):
j += 1
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 " + gradec + "-" + classc
else:
return redirect('/logout')
@ app.route('/upload/2', methods=['GET', 'POST'])
def upload_gp_classes():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET':
return render_template('uploadcsv.html', title="Group Classes", url="/upload/2")
elif request.method == 'POST':
try:
csv_file = request.files['csv']
filepath = os.path.join('./temp', csv_file.filename)
csv_file.save(filepath)
csv_dict = pd.read_csv(filepath)
category_cnt = csv_dict.shape[1] - 1
for i in range(category_cnt):
tmp_csv = csv_dict[csv_dict.columns[i+1]].tolist()
for j in range(len(tmp_csv)):
if type(tmp_csv[j]) == float:
break
if j % 5 == 0:
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+2] + " (" + tmp_csv[j+3] + ")")
auth.create_user_with_email_and_password(
csv_dict.columns[i+1] + "^" + tmp_csv[j] + "@group-attendance.fhjh.tp.edu.tw", tmp_csv[j+4])
user = auth.sign_in_with_email_and_password(
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)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded"
else:
return redirect('/logout')
@ app.route('/upload/3', methods=['GET', 'POST'])
def upload_stud_in_group():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET':
return render_template('uploadcsv.html', title="Student in Group List", url="/upload/3")
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)
csv_file.save(filepath)
with open(filepath) as file:
csv_dict = csv.DictReader(file)
headers = csv_dict.fieldnames
headers = headers[1:]
for h in headers:
db.child("Classes").child("GP_Class").child(
h).child("Homerooms").update({gradec+'^'+classc: 0})
for row in csv_dict:
for h in headers:
db.child("Homerooms").child(gradec).child(classc).child(
row['number']).child("GP_Class").update({h: row[h]})
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc
else:
return redirect('/logout')
@ app.route('/upload/4', methods=['GET', 'POST'])
def upload_period_list():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET':
return render_template('uploadcsv.html', title="Period List", url="/upload/4")
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)
csv_file.save(filepath)
csv_dict = pd.read_csv(filepath)
periodCodes = csv_dict['Period Day'].tolist()
for i in range(5):
tmp_csv = csv_dict[str(i+1)].tolist()
for j in range(len(tmp_csv)):
if not (periodCodes[j].endswith('-t')):
if type(tmp_csv[j]) == float:
db.child("Classes").child("Homeroom").child(gradec).child(classc).child(
str(i+1)).child(periodCodes[j-1]).update({'teacher': tmp_csv[j]})
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc
str(i+1)).child(periodCodes[j]).update({'name': '--'})
else:
db.child("Classes").child("Homeroom").child(gradec).child(classc).child(
str(i+1)).child(periodCodes[j]).update({'name': tmp_csv[j]})
if not(periodCodes[j] == 'm' or periodCodes[j] == 'n'):
j += 1
db.child("Classes").child("Homeroom").child(gradec).child(classc).child(
str(i+1)).child(periodCodes[j-1]).update({'teacher': tmp_csv[j]})
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded " + gradec + "-" + classc
else:
return redirect('/logout')
@ app.route('/upload/dates', methods=['GET', 'POST'])
def upload_dates():
if request.method == 'GET':
return render_template('uploadcsv.html', title="School Days", url="/upload/dates")
elif request.method == 'POST':
try:
csv_file = request.files['csv']
filepath = os.path.join('./temp', csv_file.filename)
csv_file.save(filepath)
with open(filepath) as file:
csv_dict = csv.DictReader(file)
headers = csv_dict.fieldnames
temp = db.child("Homerooms").get().val()
for row in csv_dict:
for h in headers:
for t in temp:
for i in temp[t]:
periodData = db.child("Classes").child(
"Homeroom").child(t).child(i).get().val()
db.child("Homerooms").child(t).child(i).child(
"Absent").child(h).update({"dow": row[h]})
db.child("Homerooms").child(t).child(i).child(
"Absent").child(h).update(
periodData[int(row[h])]
)
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded dates"
if ((not check_login_status()) and check_permission()):
if request.method == 'GET':
return render_template('uploadcsv.html', title="School Days", url="/upload/dates")
elif request.method == 'POST':
try:
csv_file = request.files['csv']
filepath = os.path.join('./temp', csv_file.filename)
csv_file.save(filepath)
with open(filepath) as file:
csv_dict = csv.DictReader(file)
headers = csv_dict.fieldnames
temp = db.child("Homerooms").get().val()
for row in csv_dict:
for h in headers:
for t in temp:
for i in temp[t]:
periodData = db.child("Classes").child(
"Homeroom").child(t).child(i).get().val()
db.child("Homerooms").child(t).child(i).child(
"Absent").child(h).update({"dow": row[h]})
db.child("Homerooms").child(t).child(i).child(
"Absent").child(h).update(
periodData[int(row[h])]
)
os.remove(filepath)
except Exception as e:
os.remove(filepath)
return "Error. Please try again\n("+str(e)+")"
return "Successfully uploaded dates"
else:
return redirect('/logout')
# @ app.route('/upload/rm_all_data_of_class', methods=['GET', "POST"])
# def rm_all_data_of_class():
# if request.method == 'GET':
# return render_template('uploadcsv.html', title="Remove all data of class", url="/upload/rm_all_data_of_class")
# elif request.method == 'POST':
# try:
# classc = request.form['classcode']
# db.child("Homerooms").child(classc).remove()
# except Exception as e:
# return "Error. Please try again\n("+str(e)+")"
# return "Successfully removed " + classc
@app.route('/upload/admin_acc', methods=['GET', 'POST'])
def upload_admin_acc():
if ((not check_login_status()) and check_permission()):
if request.method == 'GET':
return render_template('uploadcsv.html', title="Admin Accounts", url="/upload/admin_acc")
elif request.method == 'POST':
try:
csv_file = request.files['csv']
filepath = os.path.join('./temp', csv_file.filename)
csv_file.save(filepath)
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'])

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View file

@ -52,40 +52,42 @@
</form>
</div>
<div class="col">
<div class="row title">
<div class="col">HR</div>
<div class="col">Number</div>
<div class="col">Name</div>
<div class="col">Eng Name</div>
<div class="col">Morning</div>
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
<div class="col">Nap</div>
<div class="col">5</div>
<div class="col">6</div>
<div class="col">7</div>
<div class="col">8</div>
<div class="col">9</div>
</div>
<div class="row title">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
{% for i in periods %}
<div class="col">{{absData[currDate][i]['name']}}</div>
{% endfor %}
</div>
<div class="row title">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
{% for i in periods %}
<div class="col">{{absData[currDate][i]['teacher']}}</div>
{% endfor %}
<div class="sticky-top" style="background-color: white;">
<div class="row title">
<div class="col">HR</div>
<div class="col">Number</div>
<div class="col">Name</div>
<div class="col">Eng Name</div>
<div class="col">Morning</div>
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
<div class="col">Nap</div>
<div class="col">5</div>
<div class="col">6</div>
<div class="col">7</div>
<div class="col">8</div>
<div class="col">9</div>
</div>
<div class="row title">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
{% for i in periods %}
<div class="col">{{absData[currDate][i]['name']}}</div>
{% endfor %}
</div>
<div class="row title">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
{% for i in periods %}
<div class="col">{{absData[currDate][i]['teacher']}}</div>
{% endfor %}
</div>
</div>
{% if data != None %}
{% for i in homeroomData %}
@ -128,7 +130,7 @@
{% endfor %}
{% endif %}
{% for c in range(periods|length + 1) %}
{% if c % 2 == 0 %}
{% if c % 4 == 0 %}
<div class="row signatures">
{% endif %}
<div class="col half">
@ -140,7 +142,8 @@
{% if 'signature' in absData[currDate][periods[c-1]] %}
{% 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"><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 %}
{% else %}
<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']}}:
{{absData[currDate][periods[c-1]]['teacher']}}
</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 %}
</div>
{% if c % 2 == 1 %}
{% if c % 4 == 3 %}
</div>
{% endif %}
{% endfor %}

View file

@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<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"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<link rel="stylesheet" href="/static/allpages.css">
@ -14,9 +14,9 @@
<body>
<div class="container">
<h1>Homeroom_View</h1>
<h1>Attendance | Homeroom</h1>
<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>
<form action="/manage/date" id="dateSelForm" method="post">
<select name="date" id="date" class="form-select" onchange="chgDate();">
@ -137,10 +137,10 @@
</form>
{% if 'confirm' in absData[currDate] %}
<button class="btn btn-primary margin-top afterSelButton" onclick="homeroomCfrm()" disabled="disabled">
Already Confirmed</button>
Homeroom Teacher Already Confirmed | 班導已確認</button>
{% else %}
<button class="btn btn-primary margin-top afterSelButton" onclick="homeroomCfrm()">Homeroom
Teacher Confirm</button>
Teacher Confirm | 班導已確認</button>
{% endif %}
<div id="finalCheck" hidden="hidden">
<!-- show warning -->

View file

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