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

33 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-09-13 19:44 +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 eventHours = (EventParticipant.select(fn.SUM(EventParticipant.hoursEarned)) 

60 .where(EventParticipant.user == username)).scalar() 

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

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

63 

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

65 "totalCourseHours": (courseHours or 0), 

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

67 return allHours 

68 

69def getStartYear(username): 

70 """ 

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

72 """ 

73 

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

75 .join(Event) 

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

77 + CourseParticipant.select(Term.year) 

78 .join(Course) 

79 .join(Term) 

80 .where(CourseParticipant.user == username) 

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

82 

83 if startDate: 

84 return startDate.event.term.year 

85 

86 return "N/A"