-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_from_mongo.py
395 lines (326 loc) · 13.6 KB
/
fetch_from_mongo.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from pymongo import MongoClient
from datetime import datetime
from bson import ObjectId
uri = 'mongodb://127.0.0.1:27017'
alp = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# GET FACULTY DETAILS FROM THE DATABASE
def faculty_details(faculty_id):
client = MongoClient(uri)
db = client['FACULTY']
collection = db['FACULTY INFORMATION']
details = list(collection.find({'faculty_id':faculty_id}))
details[0]['_id'] = str(details[0]['_id'])
return details[0]
# GET THE LIST OF ALL STUDENT COLLECTIONS (BATCHES ex: 2021_AIE_A)
def get_batches():
client = MongoClient(uri)
list_db = client['STUDENTS']
lis = {}
lis['STUDENTS'] = list(list_db.list_collection_names())
return lis
# GET BATCH COURSES
def get_batch_courses(student_id):
client = MongoClient(uri)
year = '20' + student_id[11:13]
department = student_id[8:11]
section = alp[int(student_id[13])]
batch = year + '_' + department + '_' + section
batch_database = client['COURSES']
courses = list(batch_database[batch].find({}))
for course in courses:
fid = course['faculty_id']
fdetails = faculty_details(fid)
course['faculty_email'] = fdetails['college_email']
course['faculty_name'] = fdetails['faculty_name']
course['_id'] = str(course['_id'])
return courses
# GET THE LIST OF ALL DATABASES AND COLLECTIONS (TOPICS AND SUBTOPICS)
def get_topics_and_subtopics():
client = MongoClient(uri)
list_db = client.list_database_names()
lis = {}
s_k = ['FACULTY','STUDENTS','COURSES','PRE-TEST','POST-TEST','TEST-HISTORY','EVALUATION-HISTORY','POST-EVALUATION','admin','config','local']
for i in list_db:
if i in s_k:
continue
lis[i] = client[i].list_collection_names()
return lis
# CREATE TEST AND UPLOAD RANDOMLY PICKED QUESTIONS FOR EACH STUDENT
def upload_test(que):
all_questions = []
client = MongoClient(uri)
try:
skip_keys = {'faculty_id', 'course_id', 'batch', 'test_name', 'test_start_time', 'test_end_time', 'test_date'}
for database_name, questions in que.items():
if database_name in skip_keys:
continue
db = client[database_name]
for question in questions:
collection_name = question['sub_topic']
difficulty_tag = question['difficulty_tag']
num_questions = int(question['no_of_questions'])
collection = db[collection_name]
random_questions = list(collection.aggregate([
{ '$match': { 'difficulty_tag': difficulty_tag } },
{ '$sample': { 'size': num_questions } }
]))
for q in random_questions:
q['_id'] = str(q['_id'])
all_questions.append(q)
st_db = client['STUDENTS']
student_data = st_db[que['batch']]
existing_db = client['PRE-TEST']
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
student_questions_collection = existing_db[f"{que['faculty_id']}_{timestamp}"]
details = list(student_data.find({}))
for student_id in details:
student_questions = all_questions
student_record = {
'student_id': student_id['Roll Number'],
'questions': student_questions
}
student_questions_collection.insert_one(student_record)
test_record = {
'faculty_id':que['faculty_id'],
'test_id':f"{que['faculty_id']}_{timestamp}",
'course_id':que['course_id'],
'batch':que['batch'],
'test_date':que['test_date'],
'test_start_time':que['test_start_time'],
'test_end_time':que['test_end_time'],
'test_name':que['test_name']
}
test_history = client['TEST-HISTORY']
test_history_collection = test_history['ALL TESTS']
test_history_collection.insert_one(test_record)
return f"Test {que['test_name']} (ID : {que['faculty_id']}_{timestamp}) has been created succesfully."
finally:
client.close()
# GET STUDENT NAME
def fetch_student_details(student_id):
client = MongoClient(uri)
year = '20' + student_id[11:13]
department = student_id[8:11]
section = alp[int(student_id[13])]
batch = year + '_' + department + '_' + section
batch_database = client['STUDENTS']
student_list = batch_database[batch]
student_info = list(student_list.find({'Roll Number': student_id}))
student_info[0]['_id'] = str(student_info[0]['_id'])
return student_info[0]
# GET STUDENT TESTS
def fetch_tests(inp):
client = MongoClient(uri)
pre_data_base = client['TEST-HISTORY']
test_id = pre_data_base['ALL TESTS']
branch = inp[8:11]
batch = "20"+inp[11:13]
section = alp[int(inp[13])]
tests = list(test_id.find({'batch':f"{batch}_{branch}_{section}"}))
for test in tests:
test['_id'] = str(test['_id'])
return tests
# GET TEST INFO BY TEST ID
def get_times(test_id):
client = MongoClient(uri)
pre_data_base = client['TEST-HISTORY']
collection = pre_data_base['ALL TESTS']
info = list(collection.find({'test_id':test_id}))
info[0]['_id'] = str(info[0]['_id'])
return info
# GET QUESTIONS FOR THE STUDENT TO TAKE THE TEST
def get_test(inp):
client = MongoClient(uri)
pre_data_base = client['PRE-TEST']
test_id = pre_data_base[inp['test_id']]
questions = list(test_id.find({'student_id':inp['student_id']}))
questions[0]['_id'] = str(questions[0]['_id'])
return questions
# SUBMIT STUDENT'S TEST AND UPLOAD IT TO DATABASE
def submit_test(inp):
client = MongoClient(uri)
post_data_base = client['POST-TEST']
test_collection = post_data_base[inp['test_id']]
query = {'student_id': inp['student_id']}
update = {'$set': {'exam_data': inp['answers']}}
test_collection.update_one(query, update, upsert=True)
# FUNCTION TO ADD COURSE (ADDS COURSE IN BOTH COURSES DB AND FACULTY[faculty_id][courses] attribute.)
def add_courses(inp):
client = MongoClient(uri)
db = client['COURSES']
co = db[inp['batch']]
st_db = client['STUDENTS']
no_of_students = st_db[inp['batch']].count_documents({})
record = {
'faculty_id':inp['faculty_id'],
'course_id':inp['course_id'],
'course_name':inp['course_name'],
'batch':inp['batch'],
"active_status":"Active",
"no_of_students": no_of_students
}
co.insert_one(record)
tup = f"({inp['course_id']}, {inp['course_name']}, {no_of_students}, {inp['batch']}, 'Active')"
fac_db = client['FACULTY']
fac_col = fac_db['FACULTY INFORMATION']
rec = list(fac_col.find({
'faculty_id':inp['faculty_id']
}))
if 'courses' in rec[0] :
if rec[0]['courses'] == "" :
updated_courses = rec[0]['courses'] + f"{tup}"
else :
updated_courses = rec[0]['courses'] + f"; {tup}"
fac_col.update_one({'_id': rec[0]['_id']}, {'$set': {'courses': updated_courses}})
# ADD A PINNED COURSE FOR A FACULTY
def add_pinned_courses(inp):
client = MongoClient(uri)
tup = f"({inp['course_id']}, {inp['course_name']}, {inp['students']}, {inp['batch']})"
fac_db = client['FACULTY']
fac_col = fac_db['FACULTY INFORMATION']
rec = list(fac_col.find({
'faculty_id':inp['faculty_id']
}))
if 'pinned_courses' in rec[0] :
if rec[0]['pinned_courses'] == "" :
updated_pinned_courses = rec[0]['pinned_courses'] + f"{tup}"
else :
updated_pinned_courses = rec[0]['pinned_courses'] + f"; {tup}"
fac_col.update_one({'_id': rec[0]['_id']}, {'$set': {'pinned_courses': updated_pinned_courses}})
# HELPER FUNCTION TO CHECK THE STATUS OF A TEST
def check_time_period(start_time_str, end_time_str, time_format='%Y-%m-%d %H:%M'):
start_time = datetime.strptime(start_time_str, time_format)
end_time = datetime.strptime(end_time_str, time_format)
current_time = datetime.now()
if current_time < start_time:
return "upcoming"
elif current_time > end_time:
return "expired"
else:
return "active"
# GET ALL THE TESTS ADDED BY A FACULTY WITH ADDED STATUS ATTRIBUTE
def faculty_test_history(faculty_id):
statuses = {
'expired':[],
'upcoming':[],
'active':[]
}
client = MongoClient(uri)
db = client['TEST-HISTORY']
collection = db['ALL TESTS']
tests = list(collection.find({'faculty_id': faculty_id}))
for test in tests:
start_time = test['test_date']+" "+test['test_start_time']
end_time = test['test_date']+" "+test['test_end_time']
status = check_time_period(start_time,end_time)
statuses[status].append(test['test_id'])
return statuses
# GET ALL THE TESTS SUBMITTED BY A STUDENT
def student_test_history(student_id):
statuses = {
'expired':[],
'upcoming':[],
'active':[]
}
client = MongoClient(uri)
db = client['TEST-HISTORY']
col = db['ALL TESTS']
branch = student_id[8:11]
batch = "20" + student_id[11:13]
section = alp[int(student_id[13])]
tests = list(col.find({'batch':f"{batch}_{branch}_{section}"}))
for test in tests:
s_t = test['test_date'] + " " + test['test_start_time']
e_t = test['test_date'] + " " + test['test_end_time']
status = check_time_period(s_t,e_t)
if status == "expired":
statuses['expired'].append(test['test_id'])
if status == 'active' or status=='upcoming':
statuses[status].append(test['test_id'])
return statuses
# GET A STUDENT'S TEST PAPER AND ALL THE EVALUATION INFO ABOUT THE TEST
def get_student_submission(test_id, student_id):
client = MongoClient(uri)
tests_db = client['POST-EVALUATION']
collection = tests_db[test_id]
student_paper = list(collection.find({'student_id': student_id}))
if len(student_paper) == 0:
student_paper = [None]
else:
student_paper[0]['_id'] = str(student_paper[0]['_id'])
eval_db = client['EVALUATION-HISTORY']
collection = eval_db[test_id]
test_info = list(collection.find({'test_id': test_id}))
test_info[0]['_id'] = str(test_info[0]['_id'])
return student_paper[0], test_info[0]
# GET THE POSITION OF A STUDENT IN A TEST
def get_student_position(student_id, test_id):
client = MongoClient(uri)
tests_db = client['POST-EVALUATION']
collection = tests_db[test_id]
test_data = list(collection.find({}))
try :
index = next((i for i, submission in enumerate(test_data) if submission['student_id'] == student_id), None)
sorted_test_data = sorted(test_data, key=lambda x: x['total_marks'], reverse=True)
position = next((i for i, submission in enumerate(sorted_test_data) if submission['student_id'] == student_id), None)
return position+1
except Exception as e:
return 'Absent'
# GET BASIC INFORMATION ABOUT A TEST
def get_test_details(test_id) :
client = MongoClient(uri)
db = client['TEST-HISTORY']
collection = db['ALL TESTS']
test = list(collection.find({'test_id': test_id}))
test[0]['_id'] = str(test[0]['_id'])
return test[0]
# GET ALL THE SUBMISSIONS RECIEVED FOR A PARTICULAR TEST
def get_test_submissions(test_id) :
client = MongoClient(uri)
db = client['POST-TEST']
collection = db[test_id]
submissions = list(collection.find({}))
for submission in submissions :
submission['_id'] = str(submission['_id'])
return submissions
# EVALUATE A TEST AND STORE THE INFORMATION IN TWO DIFFERENT DATABASES
def mark_test_evaluated(test_id, evaluation_data, student_test_data):
client = MongoClient(uri)
db = client['EVALUATION-HISTORY']
test_history_collections = client['TEST-HISTORY']['ALL TESTS']
basic_test_info = test_history_collections.find_one({'test_id': test_id})
basic_test_info['no_of_attendees'] = evaluation_data['no_of_attendees']
basic_test_info['highest_mark'] = evaluation_data['highest_mark']
basic_test_info['average_mark'] = evaluation_data['average_mark']
basic_test_info['least_mark'] = evaluation_data['least_mark']
basic_test_info['no_of_easy'] = evaluation_data['no_of_easy']
basic_test_info['no_of_medium'] = evaluation_data['no_of_medium']
basic_test_info['no_of_hard'] = evaluation_data['no_of_hard']
db[test_id].insert_one(basic_test_info)
post_evaluation_collection = client['POST-EVALUATION'][test_id]
for student_data in student_test_data:
post_evaluation_collection.insert_one(student_data)
# GET ALL THE EVALUATED TESTS (THOSE THAT ARE PRESENT IN THE 'EVALUATION-HISTORY' DATABASE)
def get_all_evaluated_tests():
client = MongoClient(uri)
db = client['EVALUATION-HISTORY']
all_evaluated_tests = list(db.list_collection_names())
if 'DELETE' in all_evaluated_tests:
all_evaluated_tests.remove('DELETE')
return all_evaluated_tests
# GET THE INFORMATION ABOUT AN EVALUATED TEST
def get_evaluated_test_info(test_id):
client = MongoClient(uri)
db = client['EVALUATION-HISTORY']
col = db[test_id]
test_info = list(col.find({'test_id': test_id}))
test_info[0]['_id'] = str(test_info[0]['_id'])
return test_info
# GET ALL THE EVALUATED PAPERS OF A TEST
def get_evaluated_test_papers(test_id):
client = MongoClient(uri)
db = client['POST-EVALUATION']
submissions = list(db[test_id].find({}))
for submission in submissions:
submission['_id'] = str(submission['_id'])
return submissions