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

35 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2025-01-29 15:39 +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 Program query object containing all the programs for , 

17 the current user. 

18 """ 

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

20 

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

22 .join(EventParticipant) 

23 .where(EventParticipant.user == username) 

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

25 .order_by(Event.term) 

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

27 

28 # Fetch all ProgramBan objects for the user 

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

30 

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

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

33 transcriptData = defaultdict(list) 

34 

35 # Iterate through EventData and populate transcriptData 

36 for event in EventData: 

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

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

39 

40 return dict(transcriptData) 

41 

42def getSlCourseTranscript(username): 

43 """ 

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

45 current user. 

46 """ 

47 

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

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

50 .where(CourseParticipant.user == username) 

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

52 

53 return slCourses 

54 

55def getTotalHours(username): 

56 """ 

57 Get the toal hours from events and courses combined. 

58 """ 

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

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

61 

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

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

64 .where((EventParticipant.user == username) & (Event.program_id.not_in(transcriptsRemovedIdList)))).scalar() 

65 

66 

67 

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

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

70 

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

72 "totalCourseHours": (courseHours or 0), 

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

74 return allHours 

75 

76def getStartYear(username): 

77 """ 

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

79 """ 

80 

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

82 .join(Event) 

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

84 + CourseParticipant.select(Term.year) 

85 .join(Course) 

86 .join(Term) 

87 .where(CourseParticipant.user == username) 

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

89 

90 if startDate: 

91 return startDate.event.term.year 

92 

93 return "N/A"