-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
191 lines (138 loc) · 5.64 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from db_setup import DBSetup
from support_functions import SupportFunctions
import threading
from form_filler import form_filler
class Validation(BaseModel):
login: str = None
password: str = None
section: str = None
class Config:
orm_mode = True
class SubjectRequest(BaseModel):
login: str = None
term: str = None
class Config:
orm_mode = True
class ImageRequest(BaseModel):
login: str = None
term: str = None
subject: str
class Config:
orm_mode = True
class Cred(BaseModel):
password: str
class Config:
orm_mode = True
class NewWeights(BaseModel):
login: str = None
term: str = None
subject: str = None
quiz_weight: float = None
assign_weight: float = None
lab_weight: float = None
project_weight: float = None
midterm_weight: float = None
finals_weight: float = None
class Config:
orm_mode = True
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_methods=['*'],
)
dbHandler = DBSetup('my_db.db')
support_func = SupportFunctions()
# This is the function that will be called when the user clicks on the "Register" button
def add_user(input_data: Validation):
if not all(input_data.dict().values()):
return
# Verify credentials from Qalam
valid, name = support_func.auth(input_data.login, input_data.password)
if not valid:
return
# Add user to database
dbHandler.add_user(input_data.login, input_data.password, name, input_data.section)
print('[+] User Added')
# fetch all details from qalam to fill grades table
previous_terms, cur_sub_details, teachers = support_func.fetch_all_details(input_data.login, input_data.password)
print('[+] Fetched all details from Qalam')
# add all details to relevant db tables
# add to db tables all fields
# find current term
temp = list(previous_terms.keys())[-1]
year = int(temp[-4:])
term = temp[0:-5]
term = term == 'spring' and 'fall' or 'spring'
year = term == 'spring' and year + 1 or year
cur_term = term + ' ' + str(year)
# Find subject codes and teacher name for cur_term
temp = [(sub['name'].replace('&', 'and'), sub['credits']) for sub in cur_sub_details]
sub_codes = dbHandler.fetch_subject_codes(*temp)
teacher_names = [teachers[subject['name']] for subject in cur_sub_details]
# add to subject_term table
records = []
for sub, teach in zip(sub_codes, teacher_names):
records.append({'subject': sub, 'term': cur_term, 'teacher': teach if teach else 'visiting', 'batch': input_data.login.split('.')[-1]})
dbHandler.add_subject_term(*records)
print('[+] Added to subject_term table')
# {term: [{course: [credits, aggregate, grade]}, ...], ...}
old_sub = []
old_sub_details = []
for key, val in previous_terms.items():
for rec in val:
for course_name, detail in rec.items():
old_sub.append((course_name.lower().replace('&', 'and'), detail[0]))
old_sub_details.append({'term': key, 'aggregate': detail[1], 'grade': detail[2], 'teacher': 'visiting',
'id': input_data.login, 'batch': input_data.login.split('.')[-1]})
old_sub_codes = dbHandler.fetch_subject_codes(*old_sub)
old_sub_details = [{**details, 'subject': code} for details, code in zip(old_sub_details, old_sub_codes)]
dbHandler.add_subject_term(*old_sub_details)
print('[+] Added to subject_term table')
# add to weightage table
records = [{**record, 'id': input_data.login} for record in records]
dbHandler.add_weightage(*records)
print('[+] Added to weightage table')
dbHandler.add_weightage(*old_sub_details)
print('[+] Added to weightage table')
# add to grades and grade_avg table
records = [{**record, **grade, 'grade': 'P'} for record, grade in zip(records, cur_sub_details)]
dbHandler.add_grade_details(*records)
dbHandler.add_grade_avg(*records)
print('[+] Added to grades and grade_avg table')
# add to old_term_record table
dbHandler.add_old_term_record(*old_sub_details)
print('[+] Added to old_term_record table')
@app.post("/validate")
async def starter(input_data: Validation):
result = dbHandler.exists(input_data.login, input_data.password)
if result:
return {'result': "success", 'name': result[0]}
return {'result': False}
@app.post("/addUser")
async def starter(input_data: Validation):
threading.Thread(target=lambda: add_user(input_data)).start()
return {'result': 'pending'}
@app.post("/fillForms")
async def starter(input_data: Validation):
threading.Thread(target=lambda: form_filler(input_data.login, input_data.password)).start()
return {'result': 'pending'}
@app.post("/getGrades")
async def starter(input_data: SubjectRequest):
return {'grades': dbHandler.fetch_term_result(input_data.login, input_data.term)}
@app.post("/getImage")
async def starter(input_data: ImageRequest):
return {'image': dbHandler.fetch_image(input_data.login, input_data.term, input_data.subject)}
@app.post("/getTerms")
async def starter(input_data: Validation):
return {'terms': dbHandler.fetch_terms(input_data.login)}
@app.post("/editWeightage")
async def starter(input_data: NewWeights):
return {'result': dbHandler.update_and_fetch_new_record(**input_data.dict())}
@app.post("/prepareNextTerm")
async def starter(input_data: Cred):
threading.Thread(target=lambda: dbHandler.send_features(input_data.password)).start()
return {'result': 'pending'}