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

57 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2025-07-22 20:03 +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 

11from app.logic.volunteers import addUserBackgroundCheck 

12import datetime 

13from peewee import JOIN 

14from dateutil import parser 

15from flask import g 

16 

17def isEligibleForProgram(program, user): 

18 """ 

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

20 banned from a program. 

21 

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

23 :param user: accepts a User object or userid 

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

25 """ 

26 now = datetime.datetime.now() 

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

28 return False 

29 return True 

30 

31def addUserInterest(program_id, username): 

32 """ 

33 This function is used to add an interest to . 

34 Parameters: 

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

36 username: username of the user showing interest 

37 """ 

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

39 return True 

40 

41def removeUserInterest(program_id, username): 

42 """ 

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

44 Parameters: 

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

46 username: username of the user showing disinterest 

47 

48 """ 

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

50 if interestToDelete: 

51 interestToDelete.delete_instance() 

52 return True 

53 

54def getBannedUsers(program): 

55 """ 

56 This function returns users banned from a program. 

57 """ 

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

59 

60def isBannedFromEvent(username, eventId): 

61 """ 

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

63 """ 

64 program = Event.get_by_id(eventId).program 

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

66 return not isEligibleForProgram(program, user) 

67 

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

69 """ 

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

71 to ban the selected user. 

72 Parameters: 

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

74 username: username of the user to be banned 

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

76 banEndDate: date when the ban will end 

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

78 """ 

79 

80 noteForDb = Note.create(createdBy = creator, 

81 createdOn = datetime.datetime.now(), 

82 noteContent = note, 

83 isPrivate = 0, 

84 noteType = "ban") 

85 

86 ProgramBan.create(program = program_id, 

87 user = username, 

88 endDate = banEndDate, 

89 banNote = noteForDb) 

90 

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

92 """ 

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

94 to unban the selected user. 

95 Parameters: 

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

97 username: username of the user to be unbanned 

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

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

100 """ 

101 noteForDb = Note.create(createdBy = creator, 

102 createdOn = datetime.datetime.now(), 

103 noteContent = note, 

104 isPrivate = 0, 

105 noteType = "unban") 

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

107 unbanNote = noteForDb, 

108 removeFromTranscript = 0) 

109 .where(ProgramBan.program == program_id, 

110 ProgramBan.user == username, 

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

112 

113def getUserBGCheckHistory(username): 

114 """ 

115 Get a users background check history 

116 """ 

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

118 

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

120 .join(BackgroundCheckType) 

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

122 .order_by(BackgroundCheck.dateCompleted.desc())) 

123 for row in allBackgroundChecks: 

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

125 return bgHistory 

126 

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

128 if bonner: 

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

130 

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

132 createdOn = datetime.datetime.now(), 

133 noteContent = noteTextbox, 

134 noteType = "profile") 

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

136 note = noteForDb, 

137 isBonnerNote = bonner, 

138 viewTier = visibility) 

139 return createProfileNote 

140 

141def deleteProfileNote(noteId): 

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

143 

144def updateDietInfo(username, dietContent): 

145 """ 

146 Creates or update a user's diet information 

147 """ 

148 

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

150 

151 return ""