Coverage for app/logic/minor.py: 100%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-02-15 21:27 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-02-15 21:27 +0000
1from app.models.user import User
2from app.models.course import Course
3from app.models.courseParticipant import CourseParticipant
4from app.models.program import Program
5from app.models.event import Event
6from app.models.courseInstructor import CourseInstructor
7from app.models.eventParticipant import EventParticipant
8from collections import defaultdict
9from playhouse.shortcuts import model_to_dict
10from peewee import JOIN, fn
12def updateMinorInterest(username):
13 """
14 Given a username, update their minor interest and minor status.
15 """
16 user = User.get(username=username)
17 user.minorInterest = not user.minorInterest
18 if user.minorInterest == True:
19 user.minorStatus = "Interested"
20 else:
21 user.minorStatus = "No interest"
23 user.save()
25def getCourseInformation(id):
26 """
27 Given a course ID, return an object containing the course information and
28 its instructors full names.
29 """
31 # retrieve the course and the course instructors
32 course = model_to_dict(Course.get_by_id(id))
34 courseInstructors = (CourseInstructor.select(CourseInstructor, User)
35 .join(Course).switch()
36 .join(User)
37 .where(Course.id == id))
39 courseInformation = {"instructors": [(instructor.user.firstName + " " + instructor.user.lastName) for instructor in courseInstructors], "course": course}
41 return courseInformation
43def getProgramEngagementHistory(program_id, username, term_id):
44 """
45 Given a program_id, username, and term_id, return an object containing all events in the provided program
46 and in the given term along with the program name.
47 """
49 # execute a query that will retrieve all events in which the user has participated
50 # that fall under the provided term and programs.
51 eventsInProgramAndTerm = (Event.select(Event.id, Event.name, fn.SUM(EventParticipant.hoursEarned).alias("hoursEarned"))
52 .join(Program).switch()
53 .join(EventParticipant)
54 .where(EventParticipant.user == username,
55 Event.term == term_id,
56 Program.id == program_id)
57 )
59 program = Program.get_by_id(program_id)
61 # calculate total amount of hours for the whole program that term
62 totalHours = 0
63 for event in eventsInProgramAndTerm:
64 if event.hoursEarned:
65 totalHours += event.hoursEarned
67 participatedEvents = {"program":program.programName, "events": [event for event in eventsInProgramAndTerm.dicts()], "totalHours": totalHours}
69 return participatedEvents
73def getCommunityEngagementByTerm(username):
74 """
75 Given a username, return all of their community engagements (service learning courses and event participations.)
76 """
77 courses = (Course.select(Course)
78 .join(CourseParticipant, on=(Course.id == CourseParticipant.course))
79 .where(CourseParticipant.user == username)
80 .group_by(Course.courseName, Course.term))
82 # initialize default dict to store term descriptions as keys mapping to each
83 # engagement's respective type, name, id, and term.
84 terms = defaultdict(list)
85 for course in courses:
86 terms[(course.term.description, course.term.id)].append({"name":course.courseName, "id":course.id, "type":"course", "term":course.term})
88 events = (Event.select(Event, Program)
89 .join(EventParticipant, on=(Event.id == EventParticipant.event)).switch()
90 .join(Program)
91 .where(EventParticipant.user == username)
92 .group_by(Event.program, Event.term))
94 for event in events:
95 terms[(event.term.description, event.term.id)].append({"name":event.program.programName, "id":event.program.id, "type":"program", "term":event.term})
97 # sorting the terms by the term id
98 return dict(sorted(terms.items(), key=lambda x: x[0][1]))