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

56 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-09-02 19:10 +0000

1from app.models.user import User 

2from app.models.event import Event 

3from app.models.programBan import ProgramBan 

4from app.models.interest import Interest 

5from app.models.note import Note 

6from app.models.user import User 

7from app.models.profileNote import ProfileNote 

8from app.models.programBan import ProgramBan 

9from app.models.backgroundCheck import BackgroundCheck 

10from app.models.backgroundCheckType import BackgroundCheckType 

11import datetime 

12from peewee import JOIN 

13from dateutil import parser 

14from flask import g 

15 

16def isEligibleForProgram(program, user): 

17 """ 

18 Verifies if a given user is eligible for a program by checking if they are 

19 banned from a program. 

20 

21 :param program: accepts a Program object or a valid programid 

22 :param user: accepts a User object or userid 

23 :return: True if the user is not banned and meets the requirements, and False otherwise 

24 """ 

25 now = datetime.datetime.now() 

26 if (ProgramBan.select().where(ProgramBan.user == user, ProgramBan.program == program, ProgramBan.endDate > now, ProgramBan.unbanNote == None).exists()): 

27 return False 

28 return True 

29 

30def addUserInterest(program_id, username): 

31 """ 

32 This function is used to add an interest to . 

33 Parameters: 

34 program_id: id of the program the user is interested in 

35 username: username of the user showing interest 

36 """ 

37 Interest.get_or_create(program = program_id, user = username) 

38 return True 

39 

40def removeUserInterest(program_id, username): 

41 """ 

42 This function is used to add or remove interest from the interest table. 

43 Parameters: 

44 program_id: id of the program the user is interested in 

45 username: username of the user showing disinterest 

46 

47 """ 

48 interestToDelete = Interest.get_or_none(Interest.program == program_id, Interest.user == username) 

49 if interestToDelete: 

50 interestToDelete.delete_instance() 

51 return True 

52 

53def getBannedUsers(program): 

54 """ 

55 This function returns users banned from a program. 

56 """ 

57 return ProgramBan.select().where(ProgramBan.program == program, ProgramBan.unbanNote == None) 

58 

59def isBannedFromEvent(username, eventId): 

60 """ 

61 This function returns whether the user is banned from the program associated with an event. 

62 """ 

63 program = Event.get_by_id(eventId).program 

64 user = User.get(User.username == username) 

65 return not isEligibleForProgram(program, user) 

66 

67def banUser(program_id, username, note, banEndDate, creator): 

68 """ 

69 This function creates an entry in the note table and programBan table in order 

70 to ban the selected user. 

71 Parameters: 

72 program_id: primary id of the program the user has been banned from 

73 username: username of the user to be banned 

74 note: note left about the ban, expected to be a reason why the change is needed 

75 banEndDate: date when the ban will end 

76 creator: the admin or person with authority who created the ban 

77 """ 

78 

79 noteForDb = Note.create(createdBy = creator, 

80 createdOn = datetime.datetime.now(), 

81 noteContent = note, 

82 isPrivate = 0, 

83 noteType = "ban") 

84 

85 ProgramBan.create(program = program_id, 

86 user = username, 

87 endDate = banEndDate, 

88 banNote = noteForDb) 

89 

90def unbanUser(program_id, username, note, creator): 

91 """ 

92 This function creates an entry in the note table and programBan table in order 

93 to unban the selected user. 

94 Parameters: 

95 program_id: primary id of the program the user has been unbanned from 

96 username: username of the user to be unbanned 

97 note: note left about the ban, expected to be a reason why the change is needed 

98 creator: the admin or person with authority who removed the ban 

99 """ 

100 noteForDb = Note.create(createdBy = creator, 

101 createdOn = datetime.datetime.now(), 

102 noteContent = note, 

103 isPrivate = 0, 

104 noteType = "unban") 

105 (ProgramBan.update(endDate = datetime.datetime.now(), 

106 unbanNote = noteForDb, 

107 removeFromTranscript = 0) 

108 .where(ProgramBan.program == program_id, 

109 ProgramBan.user == username, 

110 ProgramBan.endDate > datetime.datetime.now())).execute() 

111 

112def getUserBGCheckHistory(username): 

113 """ 

114 Get a users background check history 

115 """ 

116 bgHistory = {'CAN': [], 'FBI': [], 'SHS': [], 'BSL': [],'DDC':[]} 

117 

118 allBackgroundChecks = (BackgroundCheck.select(BackgroundCheck, BackgroundCheckType) 

119 .join(BackgroundCheckType) 

120 .where(BackgroundCheck.user == username, BackgroundCheck.deletionDate == None) 

121 .order_by(BackgroundCheck.dateCompleted.desc())) 

122 for row in allBackgroundChecks: 

123 bgHistory[row.type_id].append(row) 

124 return bgHistory 

125 

126def addProfileNote(visibility, bonner, noteTextbox, username): 

127 if bonner: 

128 visibility = 1 # bonner notes are always admins and the student 

129 

130 noteForDb = Note.create(createdBy = g.current_user, 

131 createdOn = datetime.datetime.now(), 

132 noteContent = noteTextbox, 

133 noteType = "profile") 

134 createProfileNote = ProfileNote.create(user = User.get(User.username == username), 

135 note = noteForDb, 

136 isBonnerNote = bonner, 

137 viewTier = visibility) 

138 return createProfileNote 

139 

140def deleteProfileNote(noteId): 

141 return ProfileNote.delete().where(ProfileNote.id == noteId).execute() 

142 

143def updateDietInfo(username, dietContent): 

144 """ 

145 Creates or update a user's diet information 

146 """ 

147 

148 User.update(dietRestriction = dietContent).where(User.username == username).execute() 

149 

150 return ""