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

40 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-01-29 16:34 +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 

11 

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" 

22 

23 user.save() 

24 

25def getCourseInformation(id): 

26 """ 

27 Given a course ID, return an object containing the course information and  

28 its instructors full names. 

29 """ 

30 

31 # retrieve the course and the course instructors 

32 course = model_to_dict(Course.get_by_id(id)) 

33 

34 courseInstructors = (CourseInstructor.select(CourseInstructor, User) 

35 .join(Course).switch() 

36 .join(User) 

37 .where(Course.id == id)) 

38 

39 courseInformation = {"instructors": [(instructor.user.firstName + " " + instructor.user.lastName) for instructor in courseInstructors], "course": course} 

40 

41 return courseInformation 

42 

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 """ 

48 

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 ) 

58 

59 program = Program.get_by_id(program_id) 

60 

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 

66 

67 participatedEvents = {"program":program.programName, "events": [event for event in eventsInProgramAndTerm.dicts()], "totalHours": totalHours} 

68 

69 return participatedEvents 

70 

71 

72 

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)) 

81 

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}) 

87 

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)) 

93 

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}) 

96 

97 # sorting the terms by the term id 

98 return dict(sorted(terms.items(), key=lambda x: x[0][1])) 

99 

100 

101