-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_controller.py
658 lines (586 loc) · 23.4 KB
/
db_controller.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import sqlite3
class DB:
@classmethod
def connect(cls):
"""Connect with database"""
return sqlite3.connect('data/ccms.db')
@classmethod
def execute_query(cls, query, args):
"""Execute query based on provided parameters"""
conn = cls.connect()
cur = conn.cursor()
if type(args) is tuple:
args = [args]
cur.executemany(query, args)
conn.commit()
conn.close()
@classmethod
def execute_insert_query(cls, query, args):
"""Execute query and return new record id"""
conn = cls.connect()
cur = conn.cursor()
cur.execute(query, args)
last_id = cur.lastrowid
conn.commit()
conn.close()
return last_id
@classmethod
def execute_select_query(cls, query, args):
"""Execute select query and return fetchall"""
conn = cls.connect()
cur = conn.cursor()
cur.execute(query, args)
return cur.fetchall()
@classmethod
def create_user_record(cls, values):
"""Add new user record to database"""
query = 'INSERT INTO users (`name`, `mail`, `password`, `role`) VALUES (?, ?, ?, ?);'
return cls.execute_insert_query(query, values)
@classmethod
def create_assignment_record(cls, values):
"""Add new assignment record to database"""
query = 'INSERT INTO assignments (`title`, `is_team`, `content`, `due_date`, `max_points`) VALUES (?, ?, ?, ?, ?);'
return cls.execute_insert_query(query, values)
@classmethod
def create_submission_record(cls, values):
"""Add new submission record to database"""
query = 'INSERT INTO submissions (`assignment_id`, `user_id`, `content`, `date`) VALUES (?, ?, ?, ?);'
return cls.execute_insert_query(query, values)
@classmethod
def create_attendance_record(cls, values):
"""Add attendance record to database"""
query = 'INSERT INTO attendances (`user_id`, `date`, `status`) VALUES (?, ?, ?);'
return cls.execute_insert_query(query, values)
@classmethod
def create_checkpoint_record(cls, values):
"""Add checkpoint record to database"""
query = 'INSERT INTO checkpoints (`student_id`, `date`, `title`, `card`) VALUES (?, ?, ?, ?);'
return cls.execute_insert_query(query, values)
@classmethod
def create_team(cls, name):
"""Add new team to database"""
query = "INSERT INTO `teams` (`name`) VALUES (?)"
args = name
return cls.execute_insert_query(query, (args,))
@classmethod
def create_member_record(cls, team_id, student_id):
"""Add new member record to database"""
query = 'INSERT INTO `members` VALUES (?, ?);'
args = (team_id, student_id)
return cls.execute_insert_query(query, args)
@classmethod
def read_user_record_by_user_id(cls, user_id):
"""Read user record by provided id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `users` WHERE `user_id` = ?;"
cursor.execute(query, (user_id,))
user = cursor.fetchall()
conn.close()
return user
@classmethod
def read_user_record_list_by_user_id(cls, user_id):
"""Read user record list by provided id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `users` WHERE `user_id` = ?;"
cursor.execute(query, (user_id,))
user = cursor.fetchall()
conn.close()
return user
@classmethod
def read_user_record_list(cls):
"""Read all user records list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `users`;"
cursor.execute(query)
user_list = cursor.fetchall()
conn.close()
return user_list
@classmethod
def read_user_record_list_by_role(cls, role):
"""Read user record list by provided role"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `users` WHERE `role` = ?;"
cursor.execute(query, (role,))
user_list = cursor.fetchall()
conn.close()
return user_list
@classmethod
def read_user_record_list_by_id(cls, id_list):
"""Read user record list by provided id"""
conn = cls.connect()
cursor = conn.cursor()
placeholder = '?'
placeholders = ', '.join(placeholder for unused in id_list)
query = 'SELECT * FROM `users` WHERE `user_id` IN (%s);' % placeholders
cursor.execute(query, id_list)
user_list = cursor.fetchall()
conn.close()
return user_list
@classmethod
def read_user_id_list_by_team_id(cls, team_id):
"""Read user id list by provided team id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT `student_id` FROM `members` WHERE `team_id` = ?;"
cursor.execute(query, (team_id,))
temp_list = cursor.fetchall()
user_list = [elem[0] for elem in temp_list]
conn.close()
return user_list
@classmethod
def read_assignment_record_by_id(cls, assignment_id):
"""Read assignment record by provided assignment id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `assignments` WHERE `assignment_id` = ?;"
cursor.execute(query, (assignment_id,))
assignment = cursor.fetchall()
conn.close()
return assignment
@classmethod
def read_assignment_record_list(cls):
"""Read assignment record list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `assignments`;"
cursor.execute(query)
assignment_list = cursor.fetchall()
conn.close()
return assignment_list
@classmethod
def read_team_assignment_record_list(cls):
"""Read assignment record list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `assignments` WHERE `is_team` = 1;"
cursor.execute(query)
assignment_list = cursor.fetchall()
conn.close()
return assignment_list
@classmethod
def read_submission_record_by_id(cls, submission_id):
"""Read submission reacord by provided submission id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `submissions` WHERE `submission_id` = ?;"
cursor.execute(query, (submission_id,))
submission = cursor.fetchall()
conn.close()
return submission
@classmethod
def read_submission_record_list_by_user_id(cls, user_id):
"""Read submission record list by provided user id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `submissions` WHERE `user_id` = ?;"
cursor.execute(query, (user_id,))
user_submission_list = cursor.fetchall()
conn.close()
return user_submission_list
@classmethod
def read_team_record_by_id(cls, team_id):
"""Read team record by provided team id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `teams` WHERE `id` = ?;"
cursor.execute(query, (team_id,))
team_list = cursor.fetchall()
conn.close()
return team_list
@classmethod
def read_team_record_by_name(cls, team_name):
"""Read team record by provided team name"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `teams` WHERE `name` = ?;"
cursor.execute(query, (team_name,))
team_list = cursor.fetchall()
conn.close()
return team_list
@classmethod
def read_submission_record_list_by_assignment_id(cls, assignment_id):
"""Read submission record list by provided assignment id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `submissions` WHERE `assignment_id` = ?;"
cursor.execute(query, (assignment_id,))
user_submission_list = cursor.fetchall()
conn.close()
return user_submission_list
@classmethod
def read_active_user_record_list_by_assignment_id(cls, assignment_id):
"""Read submission record list by provided assignment id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT user_id FROM `submissions` WHERE assignment_id = ?;"
cursor.execute(query, (assignment_id,))
user_submission_list = cursor.fetchall()
conn.close()
return user_submission_list
@classmethod
def read_menu_record_list_by_upper_menu_id(cls, upper_menu_id):
""""""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT id, `name`, url_for, url_for_args, `position`, upper_menu_id FROM menus WHERE upper_menu_id = ?"
cursor.execute(query, (upper_menu_id,))
submenu_list = cursor.fetchall()
conn.close()
return submenu_list
@classmethod
def read_menu_by_name(cls, menu_name):
""""""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT id, `name`, url_for, url_for_args, `position`, upper_menu_id FROM menus WHERE url_for = ?"
cursor.execute(query, (menu_name,))
menu_data = cursor.fetchall()[0]
conn.close()
return menu_data
@classmethod
def read_menu_permission(cls, menu_id):
""""""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT mentor_perm, student_perm, staff_perm, boss_perm FROM menus WHERE id = ?"
cursor.execute(query, (menu_id,))
perm_list = list(cursor.fetchall()[0])
conn.close()
return perm_list
@classmethod
def read_submission_record_list(cls):
"""Read submission record list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `submissions`;"
cursor.execute(query)
submission_list = cursor.fetchall()
conn.close()
return submission_list
@classmethod
def read_attendance_record_by_id(cls, attendance_id):
"""Read attendance record by provided attendance id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `attendances` WHERE `attendance_id` = ?;"
cursor.execute(query, (attendance_id,))
attendance = cursor.fetchall()
conn.close()
return attendance
@classmethod
def read_checkpoint_record_by_id(cls, checkpoint_id):
"""Read checkpoint record by provided checkpoint id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `checkpoints` WHERE `checkpoint_id` = ?;"
cursor.execute(query, (checkpoint_id,))
checkpoint = cursor.fetchall()
conn.close()
return checkpoint
@classmethod
def read_checkpoint_record_list_by_student_id(cls, student_id):
"""Read checkpoint record list by provided studen id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `checkpoints` WHERE `student_id` = ?;"
cursor.execute(query, (student_id,))
checkpoint = cursor.fetchall()
conn.close()
return checkpoint
@classmethod
def read_checkpoint_record_list_by_title(cls, title):
"""Read checkpoint record list by provided title"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `checkpoints` WHERE `title` = ?;"
cursor.execute(query, (title,))
checkpoint = cursor.fetchall()
conn.close()
return checkpoint
@classmethod
def read_attendance_record_list_by_student_id(cls, student_id):
"""Read attendance record list by provided studen id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `attendances` WHERE `user_id` = ?;"
cursor.execute(query, (student_id,))
attendance = cursor.fetchall()
conn.close()
return attendance
@classmethod
def read_attendance_record_list_by_date(cls, date):
"""Read attendance record list by provided date"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `attendances` WHERE `date` = ?;"
cursor.execute(query, (date,))
attendance = cursor.fetchall()
conn.close()
return attendance
@classmethod
def read_checkpoint_record_list_by_date(cls, date):
"""Read checkpoint record list by provided date"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `checkpoints` WHERE `date` = ?;"
cursor.execute(query, (date,))
checkpoint = cursor.fetchall()
conn.close()
return checkpoint
@classmethod
def read_attendance_record_list(cls):
"""Read attendance record list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `attendances`;"
cursor.execute(query)
attendance_list = cursor.fetchall()
conn.close()
return attendance_list
@classmethod
def read_checkpoint_record_list(cls):
"""Read checkpoint record list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM checkpoints;"
cursor.execute(query)
checkpoint_list = cursor.fetchall()
conn.close()
return checkpoint_list
@classmethod
def read_overall_grade(cls, student_id):
"""Read overall grade by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*submissions.points/assignments.max_points),2) FROM submissions " \
"INNER JOIN assignments ON submissions.assignment_id=assignments.assignment_id " \
"WHERE `user_id`=?;"
cursor.execute(query, (student_id,))
overall_grade = cursor.fetchall()[0][0]
conn.close()
return overall_grade
@classmethod
def read_overall_attendance(cls, student_id):
"""Read overall attendance by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*status), 2) from attendances WHERE `user_id`=?;"
cursor.execute(query, (student_id,))
overall_attendance = cursor.fetchall()[0][0]
conn.close()
return overall_attendance
@classmethod
def read_overall_checkpoint(cls, student_id):
"""Read overall checkpoint by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*card), 2) from checkpoints WHERE `student_id`=?;"
cursor.execute(query, (student_id,))
overall_checkpoint = cursor.fetchall()[0][0]
conn.close()
return overall_checkpoint
@classmethod
def read_all_overall_attendance(cls):
"""Read overall attendance by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*status), 2) from attendances;"
cursor.execute(query)
overall_attendance = cursor.fetchall()[0][0]
conn.close()
return overall_attendance
@classmethod
def read_all_overall_checkpoint(cls):
"""Read overall checkpoint by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*card), 2) from checkpoints;"
cursor.execute(query)
overall_checkpoint = cursor.fetchall()[0][0]
conn.close()
return overall_checkpoint
@classmethod
def read_overall_attendance_by_date(cls, date):
"""Read overall attendance by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*status), 2) from attendances WHERE `date` = ?;"
args = (date,)
cursor.execute(query, args)
overall_attendance = cursor.fetchall()[0][0]
conn.close()
return overall_attendance
@classmethod
def read_overall_checkpoint_by_date(cls, date):
"""Read overall checkpoint by provided student id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT round(avg(100.0*card), 2) from checkpoints WHERE `date` = ?;"
args = (date,)
cursor.execute(query, args)
overall_checkpoint = cursor.fetchall()[0][0]
conn.close()
return overall_checkpoint
@classmethod
def read_team_list(cls):
"""Read team record list"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT * FROM `teams`;"
cursor.execute(query)
team_list = cursor.fetchall()
conn.close()
return team_list
@classmethod
def read_team_membership(cls, student_id):
"""Read student team id"""
conn = cls.connect()
cursor = conn.cursor()
query = "SELECT `team_id` FROM `members` WHERE `student_id` = ?;"
cursor.execute(query, (student_id,))
try:
team_id = cursor.fetchall()[0][0]
except IndexError:
team_id = None
conn.close()
return team_id
@classmethod
def update_name(cls, user_id, name):
"""Update name in user record by provided user id"""
query = "UPDATE `users` SET `name` = ? WHERE `user_id` = ?;"
args = (name, user_id)
cls.execute_query(query, args)
@classmethod
def update_mail(cls, user_id, new_mail):
"""Update mail in user record by provided user id"""
query = "UPDATE `users` SET `mail` = ? WHERE `user_id` = ?;"
args = (new_mail, user_id)
cls.execute_query(query, args)
@classmethod
def update_password(cls, user_id, new_password):
"""Update password in user record by provided user id"""
query = "UPDATE `users` SET `password` = ? WHERE `user_id` = ?;"
args = (new_password, user_id)
cls.execute_query(query, args)
@classmethod
def update_attendance(cls, user_id, date, status):
"""Update status in attendance record by provided user id and date"""
query = "UPDATE `attendances` SET `status` = ? WHERE `user_id` = ? AND `date` = ?;"
args = (status, user_id, date)
cls.execute_query(query, args)
@classmethod
def update_checkpoint(cls, user_id, title, status):
"""Update status in checkpoint record by provided user id and date"""
query = "UPDATE `checkpoints` SET `card` = ? WHERE `student_id` = ? AND `title` = ?;"
args = (status, user_id, title)
cls.execute_query(query, args)
@classmethod
def update_grade(cls, user_id, assignment_id, points):
"""Update points in submission record by provided user id and assignment id"""
query = "UPDATE `submissions` SET `points` = ? WHERE `assignment_id` = ? AND `user_id` = ?;"
args = (points, assignment_id, user_id)
cls.execute_query(query, args)
@classmethod
def update_user(cls, user_id, name, mail, password):
query = "UPDATE `users` SET `name` = ?, `mail` = ?, `password` = ? WHERE `user_id` = ?;"
args = (name, mail, password, user_id)
cls.execute_query(query, args)
@classmethod
def update_title(cls, assignment_id, title):
"""Update title of assignment by provided assignment id"""
query = "UPDATE `assignments` SET `title` = ? WHERE `assignment_id` = ?;"
args = (title, assignment_id)
cls.execute_query(query, args)
@classmethod
def update_content(cls, assignment_id, content):
"""Update content of assignment by provided assignment id"""
query = "UPDATE `assignments` SET `content` = ? WHERE `assignment_id` = ?;"
args = (content, assignment_id)
cls.execute_query(query, args)
@classmethod
def update_due_date(cls, assignment_id, due_date):
"""Update due date of assignment by provided assignment id"""
query = "UPDATE `assignments` SET `due_date` = ? WHERE `assignment_id` = ?;"
args = (due_date, assignment_id)
cls.execute_query(query, args)
@classmethod
def update_max_points(cls, assignment_id, max_points):
"""Update max points of assignment by provided assignment id"""
query = "UPDATE `assignments` SET `max_points` = ? WHERE `assignment_id` = ?;"
args = (max_points, assignment_id)
cls.execute_query(query, args)
@classmethod
def update_assignment(cls, assignment_id, title, is_team, content, due_date, max_points):
query = "UPDATE `assignments` SET `title` = ?, `is_team` = ?, `content` = ?, `due_date` = ?, `max_points` = ? WHERE `assignment_id` = ?;"
args = (title, is_team, content, due_date, max_points, assignment_id)
cls.execute_query(query, args)
@classmethod
def update_team_name(cls, team_id, new_name):
query = "UPDATE `teams` SET `name` = ? WHERE `id` = ?;"
args = (new_name, team_id)
cls.execute_query(query, args)
@classmethod
def update_student_team_id(cls, team_id, student_id):
query = "UPDATE `members` SET `team_id` = ? WHERE `student_id` = ?;"
args = (team_id, student_id)
cls.execute_query(query, args)
@classmethod
def delete_assignment_record(cls, assignment_id):
"""Delete assignment record by provided assignment id"""
query = "DELETE FROM assignments WHERE assignment_id = ?"
args = assignment_id
cls.execute_query(query, (args,))
@classmethod
def delete_attendance_record(cls, attendance_id):
"""Delete attendance record by provided attendance id"""
query = "DELETE FROM attendances WHERE attendance_id = ?"
args = attendance_id
cls.execute_query(query, (args,))
@classmethod
def delete_submission_record(cls, submission_id):
"""Delete submission record by provided submission id"""
query = "DELETE FROM submissions WHERE submission_id = ?"
args = submission_id
cls.execute_query(query, (args,))
@classmethod
def delete_user_record(cls, user_id):
"""Delete user record by provided user id"""
query = "DELETE FROM users WHERE user_id = ?"
args = user_id
cls.execute_query(query, (args,))
@classmethod
def delete_member_record(cls, student_id):
"""Delete member record from database"""
query = 'DELETE FROM `members` WHERE student_id = ?;'
args = student_id
cls.execute_query(query, (args,))
@classmethod
def delete_team_record(cls, team_id):
"""Delete team record from database"""
query = 'DELETE FROM `members` WHERE team_id = ?;'
args = team_id
cls.execute_query(query, (args,))
query = 'DELETE FROM `teams` WHERE id = ?;'
cls.execute_query(query, (args,))
@classmethod
def delete_user_attendance_record(cls, user_id):
"""Delete attendance record by provided user id"""
query = "DELETE FROM attendances WHERE user_id = ?"
args = user_id
cls.execute_query(query, (args,))
@classmethod
def delete_user_submission_record(cls, user_id):
"""Delete submission record by provided user id"""
query = "DELETE FROM submissions WHERE user_id = ?"
args = user_id
cls.execute_query(query, (args,))
@classmethod
def delete_assignment_submission_record(cls, assignment_id):
"""Delete submission record by provided assignment id"""
query = "DELETE FROM submissions WHERE assignment_id = ?"
args = assignment_id
cls.execute_query(query, (args,))