-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
executable file
·1048 lines (888 loc) · 30.2 KB
/
database.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# ---------------------------------------------------------------------
# database.py
# Author: Caroline di Vittorio '22
# ---------------------------------------------------------------------
from sqlalchemy import *
from student import Student
from alert import Alert
from course import Course
from group_assignment import GroupAssignment
from study_groups import StudyGroup
from scraper import scrape
from datetime import date
from cycle import Cycle
import os
GROUP_NO_STUDENTS_MIN = 3
GROUP_NO_STUDENTS_MAX = 6
# ------ DATABASE CONFIGURATION -------
# ---------------------------------------------------------------------
db_string = os.environ.get("DATABASE_URL")
db = create_engine(db_string)
meta = MetaData()
# ---------------------------------------------------------------------
student = Table(
"student",
meta,
Column("netid", String, primary_key=True),
Column("first_name", String),
Column("last_name", String),
Column("phone", String),
Column("availability", String),
Column("honor_code", String),
)
group_info = Table(
"group_info",
meta,
Column("groupid", Integer, primary_key=True),
Column("dept", String),
Column("classnum", String),
)
group_assignment = Table(
"group_assignment",
meta,
Column("groupid", Integer, primary_key=True),
Column("netid", String),
)
classes = Table(
"classes",
meta,
Column("dept", String),
Column("classnum", String),
Column("endorsed", Integer),
Column("title", String),
Column("notes", String),
)
admin = Table("admin", meta, Column("netid", String), Column("email_list", Boolean))
faculty_access = Table(
"faculty_access",
meta,
Column("netid", String),
)
cycle = Table(
"cycle",
meta,
Column("netid", String),
Column("start", Date),
Column("term", String),
)
emails = Table(
"emails",
meta,
Column("type", String),
Column("subject", String),
Column("body", String),
)
# ---------------------------------------------------------------------
# --------------------- DATABASE INTERFACE ----------------------------
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# --- CYCLE ----
def getCycleInfo():
conn = db.connect()
stmt = cycle.select()
result = conn.execute(stmt)
conn.close()
return Cycle(result.fetchone())
# ---------------------------------------------------------------------
# --- ADMIN ----
# returns true if the relevant netid is granted admin access
def isAdmin(netid):
conn = db.connect()
stmt = admin.select().where(admin.c.netid == netid)
result = conn.execute(stmt)
conn.close()
return result.fetchone() is not None
# adds relevant netid to authorized admin access
def addAdmin(netid, email_list):
if netid is None or netid == "":
return Alert(["danger", "Enter an admin netid."])
if isAdmin(netid):
return Alert(["danger", str(netid) + " is already an admin."])
conn = db.connect()
stmt = admin.insert().values(netid=netid, email_list=email_list)
conn.execute(stmt)
conn.close()
return Alert(["success", str(netid) + " added successfully!"])
# removes relevant netid from authorized admin access
def deleteAdmin(netid):
if netid == "":
return Alert(["danger", "Enter an admin netid."])
if isAdmin(netid):
conn = db.connect()
stmt = admin.delete().where(admin.c.netid == netid)
conn.execute(stmt)
conn.close()
return Alert(["success", str(netid) + " removed successfully!"])
return Alert(["danger", str(netid) + " not an admin."])
# returns a list of netids of all authorized admin
def getAdmin():
conn = db.connect()
stmt = admin.select()
result = conn.execute(stmt)
conn.close()
admins = []
for row in result:
netid, email_list = row[0], row[1]
admins.append(netid)
if email_list:
admins[-1] += "*"
return admins
# ---------------------------------------------------------------------
# --- METRICS ----
def getMetrics():
all_classes, all_group_info, all_group_assignment = _getGroupData()
all_classes = list(all_classes)
# precompute whether or not all courses in a dept are approved
entire_dept_approved = {}
for row in all_classes:
dept, approval_status = row[0], row[2]
approved = entire_dept_approved.get(dept, True)
entire_dept_approved[dept] = approved and approval_status == 2
groups_by_id = {} # key is groupid, value is list of netids
for row in all_group_assignment:
assignment_row = GroupAssignment(row)
group_id = str(assignment_row.getGroupId())
net_id = assignment_row.getNetid()
if group_id not in groups_by_id:
groups_by_id[group_id] = []
groups_by_id[group_id].append(net_id)
"""
dept_course_data
{
dept: {
'num_unique_students': #,
'num_groups': #,
'num_courses_with_groups': #,
'num_courses_total': #,
'courses': {
classnum: {
'title': "ABC",
'num_unique_students': #,
'num_groups': #,
}
...
},
...
}
}
"""
dept_course_data = {}
"""
groups_by_dept
{
dept: {
classnum: {
{
groupid: [netid1, netid2, ...],
}
},
...
},
...
}
"""
groups_by_dept = {}
# every class should have an entry, even if it has no groups
for row in all_classes:
course_row = Course(row)
dept = course_row.getDept()
num = course_row.getNum()
title = course_row.getTitle()
if dept not in groups_by_dept:
groups_by_dept[dept] = {}
dept_course_data[dept] = {
"courses": {},
"num_unique_students": 0,
"num_groups": 0,
"num_courses_total": 0,
"num_courses_with_groups": 0,
}
if num not in groups_by_dept[dept]:
groups_by_dept[dept][num] = {}
dept_course_data[dept]["courses"][num] = {
"title": title,
"num_unique_students": 0,
"num_groups": 0,
}
# increment total number of courses per dept
dept_course_data[dept]["num_courses_total"] += 1
for row in all_group_info:
group_row = StudyGroup(row)
dept = group_row.getClassDept()
num = str(group_row.getClassNum())
group_id = str(group_row.getGroupId())
# append netids to this group
if group_id not in groups_by_dept[dept][num]:
groups_by_dept[dept][num][group_id] = []
group_netids = groups_by_id[group_id]
groups_by_dept[dept][num][group_id].extend(group_netids)
for dept in groups_by_dept:
dept_students_set = set()
dept_num_groups = 0
for num in groups_by_dept[dept]:
# number of groups in this course
course_num_groups = len(groups_by_dept[dept][num])
dept_course_data[dept]["courses"][num]["num_groups"] = course_num_groups
dept_num_groups += course_num_groups
# increment number of courses with groups
if course_num_groups > 0:
dept_course_data[dept]["num_courses_with_groups"] += 1
course_students_set = set()
for group_id in groups_by_dept[dept][num]:
group_set = set(groups_by_dept[dept][num][group_id])
course_students_set.update(group_set)
dept_students_set.update(group_set)
# number of unique students in this course
dept_course_data[dept]["courses"][num]["num_unique_students"] = len(
course_students_set
)
# number of unique students in this dept
dept_course_data[dept]["num_unique_students"] = len(dept_students_set)
# number of groups in this dept
dept_course_data[dept]["num_groups"] = dept_num_groups
# sort by increasing course number
for dept in groups_by_dept:
groups_by_dept[dept] = dict(sorted(groups_by_dept[dept].items()))
# sort by depts in alphabetical order
groups_by_dept = dict(sorted(groups_by_dept.items()))
return groups_by_dept, dept_course_data, entire_dept_approved
def getAllDeptCourses(dept):
conn = db.connect()
stmt = classes.select().where(classes.c.dept == dept)
dept_courses = conn.execute(stmt)
return dept_courses
def _getGroupData():
conn = db.connect()
stmt = classes.select()
all_classes = conn.execute(stmt)
stmt = group_info.select()
all_group_info = conn.execute(stmt)
stmt = group_assignment.select()
all_group_assignment = conn.execute(stmt)
conn.close()
return all_classes, all_group_info, all_group_assignment
def getEmailListAdmins():
conn = db.connect()
stmt = admin.select().where(admin.c.email_list == True)
result = conn.execute(stmt)
conn.close()
return [f"{row[0]}@princeton.edu" for row in result]
def getEmailTemplates():
conn = db.connect()
stmt = emails.select()
result = conn.execute(stmt)
conn.close()
return {e[0]: {"subject": e[1], "body": e[2]} for e in result}
def updateEmailTemplate(type_, subject, body):
conn = db.connect()
stmt = (
emails.update().where(emails.c.type == type_).values(subject=subject, body=body)
)
conn.execute(stmt)
conn.close()
# ---------------------------------------------------------------------
# --- FACULTY ----
# return true if the relevant netid has faculty access
def isFaculty(netid):
conn = db.connect()
stmt = faculty_access.select().where(faculty_access.c.netid == netid)
result = conn.execute(stmt)
conn.close()
return result.fetchone() is not None
# adds relevant netid to authorized faculty list
def addFaculty(netid):
if netid is None or netid == "":
return Alert(["danger", "Enter a faculty netid."])
if isFaculty(netid):
return Alert(["danger", str(netid) + " is already an approved faculty."])
conn = db.connect()
stmt = faculty_access.insert().values(netid=netid)
conn.execute(stmt)
conn.close()
return Alert(["success", str(netid) + " added successfully!"])
# removes relevant netid from authorized faculty access
def deleteFaculty(netid):
if netid == "":
return Alert(["danger", "Enter a faculty netid."])
if isFaculty(netid):
conn = db.connect()
stmt = faculty_access.delete().where(faculty_access.c.netid == netid)
conn.execute(stmt)
conn.close()
return Alert(["success", str(netid) + " removed successfully!"])
return Alert(["danger", str(netid) + " not an approved faculty."])
# returns a list of netids of all authorized faculty
def getFaculty():
conn = db.connect()
stmt = faculty_access.select()
result = conn.execute(stmt)
conn.close()
faculty = []
for row in result:
faculty.append(row[0])
return faculty
# ---------------------------------------------------------------------
# --- STUDENT ----
# creates a new student in database
def createNewStudent(netid):
conn = db.connect()
stmt = student.insert().values(
netid=netid,
availability="{True, True, True, True, True, True, True}",
honor_code="not accepted",
)
conn.execute(stmt)
conn.close()
# returns a student object with the relevant student information
def getStudentInformation(netid):
conn = db.connect()
stmt = student.select().where(student.c.netid == netid)
result = conn.execute(stmt)
info = result.fetchone()
conn.close()
return None if info is None else Student(info)
# student_info is a Student object
# updates the relevant student with the information in student_info
def updateStudent(student_info):
if student_info is None or student_info.getNetid == "":
return Alert(["failure", "Please Enter Contact Information"])
conn = db.connect()
stmt = student.delete().where(student.c.netid == student_info.getNetid())
conn.execute(stmt)
stmt = student.insert().values(
netid=student_info.getNetid(),
first_name=student_info.getFirstName(),
last_name=student_info.getLastName(),
phone=student_info.getPhone(),
availability=student_info.getAvailability(),
honor_code=student_info.getHonorCode(),
)
conn.execute(stmt)
conn.close()
return Alert(["success", "Your contact information has been successfully saved."])
# returns true if this is the first login of a student, and false otherwise
def firstLogin(netid):
return getStudentInformation(netid) is None
# ---------------------------------------------------------------------
# --- COURSES ----
# returns the title of the class
def getCourseTitle(dept, num):
conn = db.connect()
stmt = (
classes.select()
.where(classes.c.dept == dept)
.where(classes.c.classnum == str(num))
)
result = conn.execute(stmt)
title = Course(result.fetchone()).getTitle()
conn.close()
return title
# returns the number of groups in that class
def numberGroupsInClass(dept, num):
conn = db.connect()
stmt = (
group_info.select()
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
)
result = conn.execute(stmt)
conn.close()
return len(result.fetchall())
# returns a list of netids of the students in the relevant group
def getStudentsInGroup(groupid):
conn = db.connect()
stmt = group_assignment.select().where(group_assignment.c.groupid == groupid)
result = conn.execute(stmt)
netids = []
for row in result:
netids.append(GroupAssignment(row).getNetid())
conn.close()
return netids
# returns a list of netids of the students in the relevant class
def getStudentsInClass(dept, num):
conn = db.connect()
stmt = (
group_assignment.select()
.where(group_assignment.c.groupid == group_info.c.groupid)
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
)
result = conn.execute(stmt)
netids = []
for row in result:
netids.append(GroupAssignment(row).getNetid())
conn.close()
return netids
# returns true if the student is already joined in the class
def isStudentInClass(netid, dept, num):
global group_assignment
global group_info
conn = db.connect()
stmt = (
group_assignment.select()
.where(group_assignment.c.netid == netid)
.where(group_assignment.c.groupid == group_info.c.groupid)
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
)
result = conn.execute(stmt)
conn.close()
return result.fetchone() is not None
# gets the relevant class from database
def getCourse(dept, num):
conn = db.connect()
stmt = (
classes.select().where(classes.c.dept == dept).where(classes.c.classnum == num)
)
result = conn.execute(stmt)
for row in result:
return Course(row)
return None
# returns the endorsement status of the relevant class
def getClassEndorsement(dept, coursenum):
conn = db.connect()
stmt = (
classes.select()
.where(classes.c.classnum == coursenum)
.where(classes.c.dept == dept)
)
result = conn.execute(stmt)
return Course(result.fetchone()).isEndorsed()
# returns the groupids that the relevant student has joined (both approved and unapproved)
def getJoinedGroups(netid):
conn = db.connect()
stmt = (
group_assignment.select()
.where(group_assignment.c.netid == netid)
.where(group_assignment.c.groupid == group_info.c.groupid)
.order_by(group_info.c.dept, group_info.c.classnum)
)
result = conn.execute(stmt)
groups = []
for row in result:
groups.append(row[0])
conn.close()
return groups
# returns the classes that the relevant student has joined (both approved and unapproved)
def getJoinedClasses(netid):
conn = db.connect()
stmt = (
group_info.select()
.where(group_assignment.c.netid == netid)
.where(group_assignment.c.groupid == group_info.c.groupid)
.order_by(group_info.c.dept, group_info.c.classnum)
)
result = conn.execute(stmt)
courses = []
for row in result:
courses.append([row[1], row[2]])
conn.close()
return courses
# returns the group that the relevant student has been assigned to in that class
def getGroupOfStudentInClass(netid, dept, num):
conn = db.connect()
stmt = (
group_info.select()
.where(group_assignment.c.netid == netid)
.where(group_assignment.c.groupid == group_info.c.groupid)
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
)
result = conn.execute(stmt)
return result.fetchone()[0]
# creates a new group within the relevant class; returns the groupid
def createNewGroup(dept, classnum):
global group_info
global classes
conn = db.connect()
stmt = group_info.insert().values(dept=dept, classnum=classnum)
result = conn.execute(stmt)
key = result.inserted_primary_key[0]
conn.close()
return key
# returns the number of students in the relevant group
def getNumStudentsInGroup(groupid):
conn = db.connect()
stmt = group_assignment.select().where(group_assignment.c.groupid == groupid)
result = conn.execute(stmt)
conn.close()
return len(result.fetchall())
# returns the number of groups in the relevant class
def getNumGroupsInClass(dept, num):
conn = db.connect()
stmt = (
group_info.select()
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
)
result = conn.execute(stmt)
conn.close()
return len(result.fetchall())
# returns the number of students in the class
def getNumStudentsInClass(dept, num):
conn = db.connect()
stmt = (
group_assignment.select()
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
.where(group_info.c.groupid == group_assignment.c.groupid)
)
result = conn.execute(stmt)
conn.close()
return len(result.fetchall())
# returns the groupid of all the approved course groups that have been joined by the student
def getPublicJoinedGroups(netid):
conn = db.connect()
stmt = (
group_assignment.select()
.where(group_assignment.c.netid == netid)
.where(group_assignment.c.groupid == group_info.c.groupid)
.where(group_info.c.dept == classes.c.dept)
.where(group_info.c.classnum == classes.c.classnum)
.where(classes.c.endorsed == 2)
.order_by(group_info.c.dept, group_info.c.classnum)
)
result = conn.execute(stmt)
groups = []
for row in result:
groups.append(row[0])
conn.close()
return groups
# returns the study group information for the relevant groupid
def getGroupInformation(groupid):
conn = db.connect()
stmt = group_info.select().where(group_info.c.groupid == groupid)
result = conn.execute(stmt).fetchone()
conn.close()
if result is not None:
return StudyGroup(result)
else:
return None
# returns a list of groups in class
def getGroupsInClass(dept, num):
groups = []
conn = db.connect()
stmt = (
group_info.select()
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
)
result = conn.execute(stmt)
for row in result:
group = StudyGroup(row)
groups.append(group)
conn.close()
return groups
# ---------------------------------------------------------------------
# APPLICATION FUNCTIONS
# ---------------------------------------------------------------------
# search the database for classes
# class_dept or class_num == "" means search all of the classes
def search(class_dept, class_num):
conn = db.connect()
stmt = (
classes.select()
.where(classes.c.classnum.like("%" + str(class_num) + "%"))
.where(classes.c.dept.like("%" + str(class_dept.upper()) + "%"))
)
result = conn.execute(stmt.order_by(classes.c.dept, classes.c.classnum))
returned_classes = []
if result is not None:
for row in result:
returned_classes.append(Course(row))
return returned_classes
def searchStudents(netid):
conn = db.connect()
stmt = student.select().where(student.c.netid.like("%" + str(netid) + "%"))
result = conn.execute(stmt.order_by(student.c.netid))
returned_students = []
if result is not None:
for row in result:
returned_students.append(Student(row))
return returned_students
# -------------------------------------------------------------
# have student with netid join the class with dept, num
def addStudentToClass(netid, dept, num):
if isStudentInClass(netid, dept, num):
return Alert(["failed", "student already in class"])
endorsement_status = getClassEndorsement(dept, num)
if endorsement_status == 0:
return Alert(["failed", "class has been denied"])
# check if there exists a group to add to
groups = getGroupsInClass(dept, num)
for group in groups:
if getNumStudentsInGroup(group.getGroupId()) < GROUP_NO_STUDENTS_MAX:
addStudentToGroup(netid, group.getGroupId())
return Alert(["success", group.getGroupId()])
# create new group if necessary
new_groupid = createNewGroup(dept, num)
addStudentToGroup(netid, new_groupid)
return Alert(["success", new_groupid])
# adds the student defined by netid to the group with groupid
def addStudentToGroup(netid, groupid):
if netid in getStudentsInGroup(groupid):
return
conn = db.connect()
stmt = group_assignment.insert().values(groupid=groupid, netid=netid)
conn.execute(stmt)
conn.close()
# -----------------------------------------------------------------------
# removes the student with netid from the relevant group (and therefore class)
def removeStudentFromGroup(netid, groupid, dept, num):
if netid is None:
return
if groupid is None:
return
if isStudentInClass(netid, dept, num):
conn = db.connect()
stmt = (
group_assignment.delete()
.where(group_assignment.c.groupid == groupid)
.where(group_assignment.c.netid == netid)
)
conn.execute(stmt)
if getNumStudentsInGroup(groupid) <= 0:
stmt = group_info.delete().where(group_info.c.groupid == groupid)
conn.execute(stmt)
conn.close()
# -----------------------------------------------------------------------
# switches the student with netid into a new group for the class defined
# by dept/num
def switchGroup(netid, dept, num):
if not isStudentInClass(netid, dept, num):
return Alert(["failed", "student not in class"])
endorsement_status = getClassEndorsement(dept, num)
if endorsement_status == 0:
return Alert(["failed", "class has been denied"])
# get current groupid
conn = db.connect()
stmt = (
group_assignment.select()
.where(group_info.c.dept == dept)
.where(group_info.c.classnum == num)
.where(group_assignment.c.groupid == group_info.c.groupid)
.where(group_assignment.c.netid == netid)
)
result = conn.execute(stmt)
curr_groupid = -1
for row in result:
curr_groupid = row[0]
removeStudentFromGroup(netid, curr_groupid, dept, num)
# check if there exists a group to add to
groups = getGroupsInClass(dept, num)
for group in groups:
if (
getNumStudentsInGroup(group.getGroupId()) < GROUP_NO_STUDENTS_MAX
and group.getGroupId() != curr_groupid
):
addStudentToGroup(netid, group.getGroupId())
return Alert(["success", group.getGroupId()])
# create new group if necessary
new_groupid = createNewGroup(dept, num)
addStudentToGroup(netid, new_groupid)
return Alert(["success", new_groupid])
# -----------------------------------------------------------------------
def approveCourse(dept, num, approval, msg):
conn = db.connect()
stmt = (
classes.select().where(classes.c.dept == dept).where(classes.c.classnum == num)
)
result = conn.execute(stmt)
course = Course(result.fetchone())
stmt = (
classes.update()
.values(endorsed=approval, notes=msg)
.where(classes.c.dept == dept)
.where(classes.c.classnum == num)
)
conn.execute(stmt)
conn.close()
if approval == 0:
if course.isEndorsed() == 0:
return None
if getNumGroupsInClass(dept, num) > 0:
netids = getStudentsInClass(dept, num)
return [0, netids]
if approval == 2:
if course.isEndorsed() == 2:
return None
if getNumGroupsInClass(dept, num) > 0:
groupids = getGroupsInClass(dept, num)
all_groups = []
for id in groupids:
all_groups.append(getStudentsInGroup(id.getGroupId()))
return [2, all_groups]
return None
# -----------------------------------------------------------------------
# OTHER ADMIN
# -----------------------------------------------------------------------
# gathers the breakdwon information for the admin page
def getAdminBreakdown():
conn = db.connect()
stmt = student.select()
result = conn.execute(stmt).fetchall()
num_student_visisted = 0 if result is None else len(result)
stmt = group_info.select()
result = conn.execute(stmt).fetchall()
num_groups = 0 if result is None else len(result)
stmt = group_assignment.select()
result = conn.execute(stmt).fetchall()
netids = set()
for row in result:
netids.add(GroupAssignment(row).getNetid())
num_participants = len(netids)
conn.close()
return [num_student_visisted, num_groups, num_participants]
# -----------------------------------------------------------------------
# ADMIN RESET
# -----------------------------------------------------------------------
# inserts new class into the database. Used when the database is being reset.
def instantiateClass(dept, num, title):
conn = db.connect()
stmt = classes.insert().values(
dept=dept, classnum=num, title=title, endorsed=1, notes=""
)
conn.execute(stmt)
conn.close()
# -----------------------------------------------------------------------
# Resets the database for new use. Deletes all information within it and reloads
# all current class information.
# USE WITH TREMENDOUS CAUTION
def reset_classes(netid):
conn = db.connect()
stmt = classes.delete()
conn.execute(stmt)
stmt = group_assignment.delete()
conn.execute(stmt)
stmt = group_info.delete()
conn.execute(stmt)
stmt = student.delete()
conn.execute(stmt)
stmt = cycle.delete()
conn.execute(stmt)
# start a new cycle
# term argument isn't used anymore
stmt = cycle.insert().values(netid=netid, start=date.today(), term=str("1223"))
conn.execute(stmt)
conn.close()
# set the classes
DEPTS = [
"AAS",
"AFS",
"AMS",
"ANT",
"AOS",
"APC",
"ARA",
"ARC",
"ART",
"ASA",
"AST",
"ATL",
"BCS",
"CBE",
"CEE",
"CGS",
"CHI",
"CHM",
"CHV",
"CLA",
"CLG",
"COM",
"COS",
"CWR",
"CZE",
"DAN",
"EAS",
"ECO",
"ECS",
"EEB",
"EGR",
"ELE",
"ENE",
"ENG",
"ENT",
"ENV",
"EPS",
"FIN",
"FRE",
"FRS",
"GEO",
"GER",
"GHP",
"GSS",
"HEB",
"HIN",
"HIS",
"HLS",
"HOS",
"HUM",
"ISC",
"ITA",
"JDS",
"JPN",
"JRN",
"KOR",
"LAO",
"LAS",
"LAT",
"LIN",
"MAE",
"MAT",
"MED",
"MOD",
"MOG",
"MOL",
"MPP",
"MSE",
"MTD",
"MUS",
"NES",
"NEU",