-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController_AdminOperation.py
More file actions
executable file
·107 lines (83 loc) · 3.09 KB
/
Copy pathController_AdminOperation.py
File metadata and controls
executable file
·107 lines (83 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pyhton
from os import path
from datetime import datetime
#flask
from flask import request,url_for,redirect,flash
#library
from openpyxl import load_workbook
#modal
import Modal_Student
#module
from Module_exporetStdUPData import ExportUPData
def isStdListExcelTemp(excelFileName):
wb = load_workbook(excelFileName,True)
#the template must be 3col with
#Id,Student Full Name,Stream
sheet = wb.active
#totalRows and col
rows = sheet.max_row
cols = sheet.max_column
if(cols == 3 and rows < 1):
return False
elif(cols == 3 and rows > 1):
col1 = sheet['A1'].value.lower().lstrip()
col2 = sheet['B1'].value.lower().lstrip()
col3 = sheet['C1'].value.lower().lstrip()
if(col1 == 'id' and col2 == 'student full name' and col3 == 'stream'):
return True
else:
return False
else:
return False
def stdListExcel2Tuple(excelFileName):
stdListTumble = []
wb = load_workbook(excelFileName,True)
sheet = wb.active
#totalRows and col
rows = sheet.max_row
row = 2
for row in range(2,rows):
#id
id = sheet.cell(row=row, column=1).value
#name
name = sheet.cell(row=row, column=2).value.upper()
#stream
stream = sheet.cell(row=row, column=3).value.upper()
stdListTumble.append((name,id,stream,))
row += 1
return stdListTumble
def importStdList():
acceptFileFormat = ('.csv','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel')
stdListExcelFile = request.files['stdListData']
#check if file is excel
msgs = {}
if stdListExcelFile.content_type not in acceptFileFormat:
msgs = {'error':"import file format must be excel"}
#if file is excel
if(len(msgs) == 0):
#rename the file
file_name, file_extension = path.splitext(stdListExcelFile.filename)
stdListExcelFile.filename = f'stdList{file_extension}'
stdListExcelFile.save(path.join('./db',stdListExcelFile.filename))
if(isStdListExcelTemp(path.join('./db',stdListExcelFile.filename)) == True):
stdData = stdListExcel2Tuple(path.join('./db',stdListExcelFile.filename))
Modal_Student.addStudents(stdData)
msgs = {'success':'you have successfully imported student list into the system'}
else:
msgs = {'error':'you have imported system incompatible excel template'}
for msg in msgs:
flashMsg = msgs[msg]
flash(flashMsg,msg)
return redirect(url_for('adminDashboard'))
def exportUPData(stream):
#used as id for the file
fileNameExt = datetime.now().strftime('%S-%f')
fileName = ''
if(stream.upper() == 'N'):
fileName = 'Natural-Student-University-Placement'
else:
fileName = 'Social-Student-University-Placement'
#export it save to /download path
exportedFile = f'{fileName}-{fileNameExt}.xlsx'
ExportUPData(stream,f'../app/public/download/{exportedFile}')
return exportedFile