-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.py
151 lines (132 loc) · 5.46 KB
/
operation.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from db_operation import database, table
from scrap_nu import grab
from flask import jsonify
from adminPanel import admin
from multiprocessing import Process
import calculator
import os
class backend:
def __init__(self):
self.db = database()
self.table = table()
def txt2list(self, file):
'''This function read .txt file of result sheet and return a list of each lines information'''
with open(file, 'r') as f:
li = f.readlines()
info = []
for i, l in enumerate(li):
if l[0] == '|':
l = l.strip().split('|')
l.pop(0)
l.pop(-1)
for i in range(int(len(l))):
l[i] = l[i].strip()
if l[0] != 'ROLL NO':
info.append(l)
return info
def create_tables_in_database(self):
'''This mathod will pass 2 lists to database operetion class to create all tables into the database'''
file1 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static/course_files/courselist1.txt') #put the .txt files location of course
file2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static/course_files/courselist2.txt') #put the .txt files location of course
courseList1 = []
courseList2 = []
with open(file1, 'r') as f1:
courses1 = f1.readlines()
with open(file2, 'r') as f2:
courses2 = f2.readlines()
for course in courses1:
co = course.split('|')
for i, c in enumerate(co):
co[i] = c.strip()
if len(co)>1:
courseList1.append(co)
for course in courses2:
co = course.split('|')
for i, c in enumerate(co):
co[i] = c.strip()
if len(co)>1:
courseList2.append(co)
return self.table.create_tables(courseList1, courseList2)
def upload_results(self, filename, semester, year, user):
'''This will take result (.txt) file and pass to the student_info and result table'''
file = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'upload'), filename)
li = self.txt2list(file)
for l in li:
reg_no, _, session, name, res = l[1], l[0], l[2], l[3], l[4:]
batch = int(session[2:4])-8
self.db.insert_student(reg_no, name, batch, session)
self.db.insert_result(reg_no, semester, res, year, session)
try:
os.remove(file)
admin().post_log({'admin': user, 'semester': semester, 'year': year})
self.send_mail(semester, year)
except Exception:
pass
def scrap_results(self, batch, semester, xm_code, year, user):
reg_list = self.db.find_regi(batch)
if not reg_list:
return "registration_error"
xm_code_error = True
for reg in reg_list:
try:
res = grab(reg, xm_code, year)
except:
return 'connection_failed'
c_code = []
grade = []
if res['result']:
xm_code_error = False
for sub in res['result']:
c_code.append(sub)
grade.append(res['result'][sub]['grade'])
self.db.insertByScrapping(semester, reg, year, c_code, grade)
self.db.conn_close()
if xm_code_error:
return 'xm_code_error'
else:
admin().post_log({'admin': user, 'semester': semester, 'year': year})
return "updated"
def send_mail(self, semester, year):
from service import mail
email = mail()
process = Process(target=email.send, args=(semester, year))
process.start()
def generate_API(self, reg_no, semester):
'''This function will generate a json API for template and requested clients'''
info = self.db.show_info(reg_no)
if info == None:
return {'exception': 'student_not_found', 'registration': reg_no, 'name': '', 'batch': '', 'session': '', 'semester': '','result': '','cgpa': '',}
courses = list([list(course) for course in self.db.show_courses(semester, info[3])])
credit = [cr[2] for cr in courses]
res = self.db.show_result(reg_no, semester, info[3])
if res:
list(res)
exception = "result_found"
else:
exception = "result_not_published_yet"
return {'exception': exception,'registration': info[0],'name': info[1],'batch': info[2],'session': info[3],'semester': semester,'courses': courses,'grades': None,'result': None,'cgpa': None,'exam year': None,}
grades = {}
cgpa = calculator.cgpa(res[:-1], credit)
if cgpa=='Fail':
result = 'Failed'.upper()
cgpa = 0
else:
result='Passed'.upper()
for i, v in enumerate([code[0] for code in courses]):
grades[v]=res[i]
json = {
'exception': exception,
'registration': info[0],
'name': info[1],
'batch': info[2],
'session': info[3],
'semester': semester,
'courses': courses,
'grades': grades,
'result': result,
'cgpa': cgpa,
'exam year': res[-1],
}
self.db.conn_close()
return json
# backend().scrap_results(8, '4th', '5614', '2018')