Coverage for app/logic/transcript.py: 97%
30 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-10-24 20:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-10-24 20:53 +0000
1from peewee import fn
3from app.models.course import Course
4from app.models.courseParticipant import CourseParticipant
5from app.models.program import Program
6from app.models.courseInstructor import CourseInstructor
7from app.models.user import User
8from app.models.term import Term
9from app.models.eventParticipant import EventParticipant
10from app.models.event import Event
12def getProgramTranscript(username):
13 """
14 Returns a Program query object containing all the programs for
15 current user.
16 """
17 # Add up hours earned in a term for each program they've participated in
19 EventData = (Event.select(Event, fn.SUM(EventParticipant.hoursEarned).alias("hoursEarned"))
20 .join(EventParticipant)
21 .where(EventParticipant.user == username)
22 .group_by(Event.program, Event.term)
23 .order_by(Event.term)
24 .having(fn.SUM(EventParticipant.hoursEarned > 0)))
25 transcriptData = {}
26 for event in EventData:
27 if event.program in transcriptData:
28 transcriptData[event.program].append([event.term.description, event.hoursEarned])
29 else:
30 transcriptData[event.program] = [[event.term.description, event.hoursEarned]]
31 return transcriptData
33def getSlCourseTranscript(username):
34 """
35 Returns a SLCourse query object containing all the training events for
36 current user.
37 """
39 slCourses = (Course.select(Course, fn.SUM(CourseParticipant.hoursEarned).alias("hoursEarned"))
40 .join(CourseParticipant, on=(Course.id == CourseParticipant.course))
41 .where(CourseParticipant.user == username)
42 .group_by(Course.courseName, Course.term))
44 return slCourses
46def getTotalHours(username):
47 """
48 Get the toal hours from events and courses combined.
49 """
50 eventHours = (EventParticipant.select(fn.SUM(EventParticipant.hoursEarned))
51 .where(EventParticipant.user == username)).scalar()
52 courseHours = (CourseParticipant.select(fn.SUM(CourseParticipant.hoursEarned))
53 .where(CourseParticipant.user == username)).scalar()
55 allHours = {"totalEventHours": (eventHours or 0),
56 "totalCourseHours": (courseHours or 0),
57 "totalHours": (eventHours or 0) + (courseHours or 0)}
58 return allHours
60def getStartYear(username):
61 """
62 Returns the users start term for participation in the CELTS organization
63 """
65 startDate = (EventParticipant.select(Term.year)
66 .join(Event)
67 .join(Term).where(EventParticipant.user == username)
68 + CourseParticipant.select(Term.year)
69 .join(Course)
70 .join(Term)
71 .where(CourseParticipant.user == username)
72 ).order_by(Event.term.year).first()
74 if startDate:
75 return startDate.event.term.year
77 return "N/A"