Added foolproof and edit format for export

This commit is contained in:
Aaron Lee 2021-12-30 22:29:43 +08:00
parent 14ad35d268
commit 30ef7581a4
3 changed files with 90 additions and 55 deletions

121
export.py
View file

@ -155,19 +155,20 @@ def create_period_sheets(workbook, class_code):
def create_student_list(workbook, class_code): def create_student_list(workbook, class_code):
ws = workbook.create_sheet(class_code[0] + class_code[1]) ws = workbook.create_sheet(class_code[0] + class_code[1])
ws.merge_cells('A1:J1') ws.merge_cells('A1:K1')
ws['A1'] = '台北市私立復興實驗高級中學學生名單' ws['A1'] = '台北市私立復興實驗高級中學學生名單'
ws['A1'].font = Font(name="DFKai-SB", size=15, bold=True) ws['A1'].font = Font(name="DFKai-SB", size=15, bold=True)
ws['A1'].alignment = center ws['A1'].alignment = center
# loop over A:J # loop over A:K
for i in range(0, 11): for i in range(0, 12):
ws[str(chr(ord('A') + i)) + '1'].border = border ws[str(chr(ord('A') + i)) + '1'].border = border
ws.merge_cells('K1:L1') ws.merge_cells('L1:N1')
ws['K1'] = class_code[0] + class_code[1] ws['L1'] = class_code[0] + class_code[1]
ws['K1'].font = Font(name='Courier New', size=20, bold=True) ws['L1'].font = Font(name='Courier New', size=20, bold=True)
ws['K1'].alignment = center ws['L1'].alignment = center
ws['K1'].border = border
ws['L1'].border = border ws['L1'].border = border
ws['M1'].border = border
ws['N1'].border = border
ws.column_dimensions['A'].width = 5 ws.column_dimensions['A'].width = 5
ws.column_dimensions['B'].width = 11 ws.column_dimensions['B'].width = 11
ws.column_dimensions['C'].width = 12 ws.column_dimensions['C'].width = 12
@ -186,14 +187,16 @@ def create_student_list(workbook, class_code):
ws['C2'].font = Font(name="Calibri", size=13, bold=True) ws['C2'].font = Font(name="Calibri", size=13, bold=True)
ws['C2'].alignment = center ws['C2'].alignment = center
ws['C2'].border = bold_bottom ws['C2'].border = bold_bottom
for i in range(3, 12): for i in range(3, 14):
ws[str(chr(ord('A') + i)) + '2'].border = bold_bottom ws[str(chr(ord('A') + i)) + '2'].border = bold_bottom
ws.column_dimensions[str(chr(ord('A') + i))].width = 5.8 ws.column_dimensions[str(chr(ord('A') + i))].width = 4.6
db = refresh_db() db = refresh_db()
cursor = db.cursor() cursor = db.cursor()
cursor.execute('SELECT num,name,ename FROM students WHERE grade=%s AND class_=%s ORDER BY num ASC', cursor.execute('SELECT num,name,ename FROM students WHERE grade=%s AND class_=%s ORDER BY num ASC',
(class_code[0], class_code[1])) (class_code[0], class_code[1]))
data = cursor.fetchall() data = cursor.fetchall()
if data == []:
return workbook
last = data[-1][0] last = data[-1][0]
delcnt = 0 delcnt = 0
for i in range(0, last): for i in range(0, last):
@ -207,7 +210,7 @@ def create_student_list(workbook, class_code):
ws['C' + str(3 + i)].font = std_font ws['C' + str(3 + i)].font = std_font
ws['C' + str(3 + i)].alignment = center ws['C' + str(3 + i)].alignment = center
ws.row_dimensions[3 + i].height = 19 ws.row_dimensions[3 + i].height = 19
for j in range(0, 12): for j in range(0, 14):
ws[str(chr(ord('A') + j)) + str(3 + i)].border = bold_bottom if (i + 1) % 5 == 0 else border ws[str(chr(ord('A') + j)) + str(3 + i)].border = bold_bottom if (i + 1) % 5 == 0 else border
if data[i - delcnt][0] != i + 1: if data[i - delcnt][0] != i + 1:
delcnt += 1 delcnt += 1
@ -217,65 +220,77 @@ def create_student_list(workbook, class_code):
def create_group_student_list(workbook, cclass, data): def create_group_student_list(workbook, cclass, data):
class_code = [cclass['category'], cclass['class_id']] class_code = [cclass['category'], cclass['class_id']]
ws = workbook.create_sheet(class_code[0] + '.' + class_code[1]) ws = workbook.create_sheet(class_code[0] + '.' + class_code[1])
ws.merge_cells('A1:H1') ws.merge_cells('A1:J1')
ws['A1'] = '台北市私立復興實驗高級中學分組課學生名單' ws['A1'] = '台北市私立復興實驗高級中學分組課學生名單'
ws['A1'].font = Font(name="DFKai-SB", size=15, bold=True) ws['A1'].font = Font(name="DFKai-SB", size=15, bold=True)
ws['A1'].alignment = center ws['A1'].alignment = center
# loop over A:I # loop over A:J
for i in range(0, 10): for i in range(0, 12):
ws[str(chr(ord('A') + i)) + '1'].border = border ws[str(chr(ord('A') + i)) + '1'].border = border
ws.merge_cells('I1:L1') ws.merge_cells('K1:O1')
ws['I1'] = class_code[0] + class_code[1] ws['K1'] = class_code[0] + class_code[1]
ws['I1'].font = Font(name='Calibri', size=15, bold=True) ws['K1'].font = Font(name='Calibri', size=15, bold=True)
ws['I1'].alignment = center ws['K1'].alignment = center
ws['I1'].border = border
ws['J1'].border = border
ws['K1'].border = border ws['K1'].border = border
ws['L1'].border = border ws['L1'].border = border
ws['M1'].border = border
ws['N1'].border = border
ws['O1'].border = border
ws.column_dimensions['A'].width = 6 ws.column_dimensions['A'].width = 6
ws.column_dimensions['B'].width = 4 ws.column_dimensions['B'].width = 4
ws.column_dimensions['C'].width = 11 ws.column_dimensions['C'].width = 11
ws.column_dimensions['D'].width = 12 ws.column_dimensions['D'].width = 12
ws.row_dimensions[1].height = 25 ws.row_dimensions[1].height = 25
ws.row_dimensions[2].height = 20 ws.row_dimensions[2].height = 20
ws.row_dimensions[3].height = 20
ws['A2'] = '班級' ws['A3'] = '班級'
ws['A3'].font = Font(name="Calibri", size=13, bold=True)
ws['A3'].alignment = center
ws['A3'].border = bold_bottom
ws['B3'] = '#'
ws['B3'].font = Font(name="Calibri", size=13, bold=True)
ws['B3'].alignment = center
ws['B3'].border = bold_bottom
ws['C3'] = '姓名'
ws['C3'].font = Font(name="DFKai-SB", size=13, bold=True)
ws['C3'].alignment = center
ws['C3'].border = bold_bottom
ws['D3'] = 'Name'
ws['D3'].font = Font(name="Calibri", size=13, bold=True)
ws['D3'].alignment = center
ws['D3'].border = bold_bottom
db = refresh_db()
cursor = db.cursor()
cursor.execute("SELECT about FROM gpclasses WHERE category=%s AND subclass=%s LIMIT 1", (class_code[0], class_code[1]))
ws.merge_cells('A2:O2')
ws['A2'] = cursor.fetchone()[0]
ws['A2'].font = Font(name="Calibri", size=13, bold=True) ws['A2'].font = Font(name="Calibri", size=13, bold=True)
ws['A2'].alignment = center ws['A2'].alignment = center
ws['A2'].border = bold_bottom for i in range(0, 15):
ws['B2'] = '#' ws[str(chr(ord('A') + i)) + '2'].border = border
ws['B2'].font = Font(name="Calibri", size=13, bold=True) for i in range(4, 15):
ws['B2'].alignment = center ws[str(chr(ord('A') + i)) + '3'].border = bold_bottom
ws['B2'].border = bold_bottom ws.column_dimensions[str(chr(ord('A') + i))].width = 4.4
ws['C2'] = '姓名'
ws['C2'].font = Font(name="DFKai-SB", size=13, bold=True)
ws['C2'].alignment = center
ws['C2'].border = bold_bottom
ws['D2'] = 'Name'
ws['D2'].font = Font(name="Calibri", size=13, bold=True)
ws['D2'].alignment = center
ws['D2'].border = bold_bottom
for i in range(4, 12):
ws[str(chr(ord('A') + i)) + '2'].border = bold_bottom
ws.column_dimensions[str(chr(ord('A') + i))].width = 5.8
cnt = 0 cnt = 0
for i in data: for i in data:
ws['A' + str(3 + cnt)] = str(i[0]) + str(i[1]) ws['A' + str(4 + cnt)] = str(i[0]) + str(i[1])
ws['A' + str(3 + cnt)].font = std_font ws['A' + str(4 + cnt)].font = std_font
ws['A' + str(3 + cnt)].alignment = center ws['A' + str(4 + cnt)].alignment = center
ws['B' + str(3 + cnt)] = i[2] ws['B' + str(4 + cnt)] = i[2]
ws['B' + str(3 + cnt)].font = std_font ws['B' + str(4 + cnt)].font = std_font
ws['B' + str(3 + cnt)].alignment = center ws['B' + str(4 + cnt)].alignment = center
ws['C' + str(3 + cnt)] = i[3] ws['C' + str(4 + cnt)] = i[3]
ws['C' + str(3 + cnt)].font = Font(name="DFKai-SB", size=14) ws['C' + str(4 + cnt)].font = Font(name="DFKai-SB", size=14)
ws['C' + str(3 + cnt)].alignment = center ws['C' + str(4 + cnt)].alignment = center
ws['D' + str(3 + cnt)] = i[4] ws['D' + str(4 + cnt)] = i[4]
ws['D' + str(3 + cnt)].font = std_font ws['D' + str(4 + cnt)].font = std_font
ws['D' + str(3 + cnt)].alignment = center ws['D' + str(4 + cnt)].alignment = center
ws.row_dimensions[3 + cnt].height = 19 ws.row_dimensions[4 + cnt].height = 19
for j in range(0, 12): for j in range(0, 15):
ws[str(chr(ord('A') + j)) + str(3 + cnt)].border = bold_bottom if (cnt + 1) % 5 == 0 else border ws[str(chr(ord('A') + j)) + str(4 + cnt)].border = bold_bottom if (cnt + 1) % 5 == 0 else border
cnt += 1 cnt += 1
return workbook return workbook
@ -323,7 +338,7 @@ def create_teacher_periods(workbook, teacher_name, orig_username=''):
data = {} data = {}
db = refresh_db() db = refresh_db()
cursor = db.cursor() cursor = db.cursor()
if orig_username is not '': if orig_username != '':
cursor.execute('SELECT category,subclass FROM gpclasses WHERE accs LIKE %s', ('%' + orig_username + '%',)) cursor.execute('SELECT category,subclass FROM gpclasses WHERE accs LIKE %s', ('%' + orig_username + '%',))
gp_sql = cursor.fetchall() gp_sql = cursor.fetchall()
for i in gp_sql: for i in gp_sql:

View file

@ -138,6 +138,9 @@ def admin_export_group_student_list():
cursor.execute("SELECT grade,class_,num,name,ename FROM students WHERE classes LIKE " + '\'%\"' + cclass[ cursor.execute("SELECT grade,class_,num,name,ename FROM students WHERE classes LIKE " + '\'%\"' + cclass[
'category'] + '\": \"' + cclass['class_id'] + '\"%\'' + " ORDER BY grade ASC,class_ ASC,num ASC") 'category'] + '\": \"' + cclass['class_id'] + '\"%\'' + " ORDER BY grade ASC,class_ ASC,num ASC")
students = cursor.fetchall() students = cursor.fetchall()
if students == []:
flash("該課程不存在或沒有學生!")
return redirect('/manage/admin/export')
workbook = Workbook() workbook = Workbook()
workbook.remove_sheet(workbook.get_sheet_by_name('Sheet')) workbook.remove_sheet(workbook.get_sheet_by_name('Sheet'))
workbook = create_group_student_list(workbook, cclass, students) workbook = create_group_student_list(workbook, cclass, students)
@ -147,6 +150,7 @@ def admin_export_group_student_list():
return send_file(excel_stream, attachment_filename='student_list_' + cclass['category'] + '_' + cclass[ return send_file(excel_stream, attachment_filename='student_list_' + cclass['category'] + '_' + cclass[
'class_id'] + '.xlsx', as_attachment=True) 'class_id'] + '.xlsx', as_attachment=True)
@admin.route('/manage/admin/export/group_student_list_only_category', methods=['POST']) @admin.route('/manage/admin/export/group_student_list_only_category', methods=['POST'])
def admin_export_group_student_list_only_category(): def admin_export_group_student_list_only_category():
if check_login_status() or session['subuser_type'] != 'admin': if check_login_status() or session['subuser_type'] != 'admin':
@ -159,6 +163,9 @@ def admin_export_group_student_list_only_category():
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT subclass FROM gpclasses WHERE category=%s", (cclass['category'],)) cursor.execute("SELECT subclass FROM gpclasses WHERE category=%s", (cclass['category'],))
classes = cursor.fetchall() classes = cursor.fetchall()
if classes == []:
flash("該分類不存在!")
return redirect('/manage/admin/export')
workbook = Workbook() workbook = Workbook()
workbook.remove_sheet(workbook.get_sheet_by_name('Sheet')) workbook.remove_sheet(workbook.get_sheet_by_name('Sheet'))
for i in classes: for i in classes:
@ -170,7 +177,9 @@ def admin_export_group_student_list_only_category():
excel_stream = io.BytesIO() excel_stream = io.BytesIO()
workbook.save(excel_stream) workbook.save(excel_stream)
excel_stream.seek(0) excel_stream.seek(0)
return send_file(excel_stream, attachment_filename='student_list_' + cclass['category'] + '.xlsx', as_attachment=True) return send_file(excel_stream, attachment_filename='student_list_' + cclass['category'] + '.xlsx',
as_attachment=True)
@admin.route('/manage/admin/export/teacher_period', methods=['POST']) @admin.route('/manage/admin/export/teacher_period', methods=['POST'])
def admin_export_teacher_period(): def admin_export_teacher_period():

View file

@ -94,6 +94,16 @@
</form> </form>
</div> </div>
</div> </div>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
{% autoescape false %}{{message}}{% endautoescape %}
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% include 'footer.html' %} {% include 'footer.html' %}
</div> </div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
@ -106,6 +116,7 @@
function onlyCategory() { function onlyCategory() {
document.getElementById('group_student_list').action = '/manage/admin/export/group_student_list_only_category'; document.getElementById('group_student_list').action = '/manage/admin/export/group_student_list_only_category';
document.getElementById('group_student_list').submit(); document.getElementById('group_student_list').submit();
document.getElementById('group_student_list').action = '/manage/admin/export/group_student_list';
} }
</script> </script>
</body> </body>