Coverage for app/logic/transcript.py: 86%
42 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-24 14:13 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-24 14:13 +0000
1from app.models.course import Course
2from app.models.courseParticipant import CourseParticipant
3from app.models.program import Program
4from app.models.programEvent import ProgramEvent
5from app.models.courseInstructor import CourseInstructor
6from app.models.user import User
7from app.models.term import Term
8from app.models.eventParticipant import EventParticipant
9from app.models.event import Event
10from peewee import DoesNotExist, fn, JOIN
13def getOtherEventsTranscript(username):
14 """
15 Returns a Other Events query object containing all the non-program
16 events for the current user.
17 """
19 otherEventsData = list(EventParticipant.select(Event, Term, fn.SUM(EventParticipant.hoursEarned).alias("hoursEarned"))
20 .join(Event)
21 .join(ProgramEvent, JOIN.LEFT_OUTER)
22 .join(Program, JOIN.LEFT_OUTER).switch(Event)
23 .join(Term)
24 .where(EventParticipant.user==username,
25 ProgramEvent.program == None)
26 .group_by(Event.term)
27 )
29 return [[row.event.term.description, row.hoursEarned] for row in otherEventsData]
31def getProgramTranscript(username):
32 """
33 Returns a Program query object containing all the programs for
34 current user.
35 """
36 # Add up hours earned in a term for each program they've participated in
37 programData = (ProgramEvent
38 .select(Program, Event, fn.SUM(EventParticipant.hoursEarned).alias("hoursEarned"))
39 .join(Program)
40 .switch(ProgramEvent)
41 .join(Event)
42 .join(EventParticipant)
43 .where(EventParticipant.user == username)
44 .group_by(Program, Event.term)
45 .order_by(Event.term)
46 .having(fn.SUM(EventParticipant.hoursEarned > 0)))
47 transcriptData = {}
48 for program in programData:
49 if program.program in transcriptData:
50 transcriptData[program.program].append([program.event.term.description, program.hoursEarned])
51 else:
52 transcriptData[program.program] = [[program.event.term.description, program.hoursEarned]]
53 return transcriptData
55def getAllEventTranscript(username):
56 """
57 Combines the program transcript and other events transcript into one dict
58 for easier display.
59 """
60 programDict = getProgramTranscript(username)
61 allEventsDict = programDict
62 otherList = getOtherEventsTranscript(username)
63 if otherList:
64 allEventsDict["CELTS Sponsored Events"] = otherList
65 return allEventsDict
68def getSlCourseTranscript(username):
69 """
70 Returns a SLCourse query object containing all the training events for
71 current user.
72 """
74 slCourses = (Course
75 .select(Course, fn.SUM(CourseParticipant.hoursEarned).alias("hoursEarned"))
76 .join(CourseParticipant, on=(Course.id == CourseParticipant.course))
77 .where(CourseParticipant.user == username)
78 .group_by(Course.courseName, Course.term))
80 return slCourses
82def getTotalHours(username):
83 """
84 Get the toal hours from events and courses combined.
85 """
86 eventHours = EventParticipant.select(fn.SUM(EventParticipant.hoursEarned)).where(EventParticipant.user == username).scalar()
87 courseHours = CourseParticipant.select(fn.SUM(CourseParticipant.hoursEarned)).where(CourseParticipant.user == username).scalar()
89 allHours = {"totalEventHours": (eventHours or 0),
90 "totalCourseHours": (courseHours or 0),
91 "totalHours": (eventHours or 0) + (courseHours or 0)}
92 return allHours
94def getStartYear(username):
95 """
96 Returns the users start term for participation in the CELTS organization
97 """
99 startDate = (EventParticipant.select(Term.year)
100 .join(Event)
101 .join(Term).where(EventParticipant.user == username)
102 + CourseParticipant.select(Term.year)
103 .join(Course)
104 .join(Term).where(CourseParticipant.user == username)).order_by(Event.term.year)
106 startDate = startDate.first()
107 if startDate:
108 return startDate.event.term.year
109 return "N/A"