Coverage for app/logic/transcript.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-12-18 19:28 +0000

1from peewee import fn 

2from collections import defaultdict 

3 

4from app.models.course import Course 

5from app.models.courseParticipant import CourseParticipant 

6from app.models.program import Program 

7from app.models.programBan import ProgramBan 

8from app.models.courseInstructor import CourseInstructor 

9from app.models.user import User 

10from app.models.term import Term 

11from app.models.eventParticipant import EventParticipant 

12from app.models.event import Event 

13 

14def getProgramTranscript(username): 

15 """ 

16 Returns a dictionary with programs as keys and a list of service or bonner events and hours earned as values for the given user. 

17 """ 

18 # Add up hours earned in a term for each program they've participated in 

19 

20 EventData = (Event.select(Event, fn.SUM(EventParticipant.hoursEarned).alias("hoursEarned")) 

21 .join(EventParticipant).switch() 

22 .join(Program) 

23 .where(EventParticipant.user == username, 

24 Event.isService | Program.isBonnerScholars, 

25 Event.deletionDate == None, 

26 Event.isCanceled == False) 

27 .group_by(Event.program, Event.term) 

28 .order_by(Event.term) 

29 .having(fn.SUM(EventParticipant.hoursEarned > 0))) 

30 

31 # Fetch all ProgramBan objects for the user 

32 bannedProgramsForParticipant = ProgramBan.select().where(ProgramBan.user == username) 

33 

34 # Create a set of program IDs to remove from transcript 

35 programsToRemoveFromTranscript = {bannedProgram.program_id for bannedProgram in bannedProgramsForParticipant if bannedProgram.removeFromTranscript} 

36 transcriptData = defaultdict(list) 

37 

38 # Iterate through EventData and populate transcriptData 

39 for event in EventData: 

40 if event.program.id not in programsToRemoveFromTranscript: # Check if program is not in programs to be removed from transcript 

41 transcriptData[event.program].append([event.term.description, event.hoursEarned]) 

42 

43 return dict(transcriptData) 

44 

45def getSlCourseTranscript(username): 

46 """ 

47 Returns a SLCourse query object containing all the training events for 

48 current user. 

49 """ 

50 

51 slCourses = (Course.select(Course, fn.SUM(CourseParticipant.hoursEarned).alias("hoursEarned")) 

52 .join(CourseParticipant, on=(Course.id == CourseParticipant.course)) 

53 .where(CourseParticipant.user == username) 

54 .group_by(Course.courseName, Course.term)) 

55 

56 return slCourses 

57 

58def getTotalHours(username): 

59 """ 

60 Get the toal hours from events and courses combined. 

61 """ 

62 bannedAndTranscriptsRemoved = ProgramBan.select().where((ProgramBan.user == username) and (ProgramBan.unbanNote.is_null(True)) and (ProgramBan.removeFromTranscript == 1)) 

63 transcriptsRemovedIdList = [program.program_id for program in bannedAndTranscriptsRemoved] 

64 

65 eventHours = (EventParticipant.select(fn.SUM(EventParticipant.hoursEarned)) 

66 .join(Event, on=(EventParticipant.event == Event.id)) 

67 .where((EventParticipant.user == username) & (Event.program_id.not_in(transcriptsRemovedIdList)) & (Event.deletionDate == None))).scalar() 

68 

69 

70 

71 courseHours = (CourseParticipant.select(fn.SUM(CourseParticipant.hoursEarned)) 

72 .where(CourseParticipant.user == username)).scalar() 

73 

74 allHours = {"totalEventHours": (eventHours or 0), 

75 "totalCourseHours": (courseHours or 0), 

76 "totalHours": (eventHours or 0) + (courseHours or 0)} 

77 return allHours 

78 

79def getStartYear(username): 

80 """ 

81 Returns the users start term for participation in the CELTS organization 

82 """ 

83 

84 startDate = (EventParticipant.select(Term.year) 

85 .join(Event) 

86 .join(Term).where(EventParticipant.user == username) 

87 + CourseParticipant.select(Term.year) 

88 .join(Course) 

89 .join(Term) 

90 .where(CourseParticipant.user == username) 

91 ).order_by(Event.term.year).first() 

92 

93 if startDate: 

94 return startDate.event.term.year 

95 

96 return "N/A"