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

57 statements  

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

17 

18def isEligibleForProgram(program, user): 

19 """ 

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

21 banned from a program. 

22 

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

24 :param user: accepts a User object or userid 

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

26 """ 

27 now = datetime.datetime.now() 

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

29 return False 

30 return True 

31 

32def addUserInterest(program_id, username): 

33 """ 

34 This function is used to add an interest to . 

35 Parameters: 

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

37 username: username of the user showing interest 

38 """ 

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

40 return True 

41 

42def removeUserInterest(program_id, username): 

43 """ 

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

45 Parameters: 

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

47 username: username of the user showing disinterest 

48 

49 """ 

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

51 if interestToDelete: 

52 interestToDelete.delete_instance() 

53 return True 

54 

55def getBannedUsers(program): 

56 """ 

57 This function returns users banned from a program. 

58 """ 

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

60 

61def isBannedFromEvent(username, eventId): 

62 """ 

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

64 """ 

65 program = Event.get_by_id(eventId).program 

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

67 return not isEligibleForProgram(program, user) 

68 

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

70 """ 

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

72 to ban the selected user. 

73 Parameters: 

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

75 username: username of the user to be banned 

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

77 banEndDate: date when the ban will end 

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

79 """ 

80 

81 noteForDb = Note.create(createdBy = creator, 

82 createdOn = datetime.datetime.now(), 

83 noteContent = note, 

84 isPrivate = 0, 

85 noteType = "ban") 

86 

87 ProgramBan.create(program = program_id, 

88 user = username, 

89 endDate = banEndDate, 

90 banNote = noteForDb) 

91 

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

93 """ 

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

95 to unban the selected user. 

96 Parameters: 

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

98 username: username of the user to be unbanned 

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

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

101 """ 

102 noteForDb = Note.create(createdBy = creator, 

103 createdOn = datetime.datetime.now(), 

104 noteContent = note, 

105 isPrivate = 0, 

106 noteType = "unban") 

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

108 unbanNote = noteForDb, 

109 removeFromTranscript = 0) 

110 .where(ProgramBan.program == program_id, 

111 ProgramBan.user == username, 

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

113 

114def getUserBGCheckHistory(username): 

115 """ 

116 Get a users background check history 

117 """ 

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

119 

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

121 .join(BackgroundCheckType) 

122 .where(BackgroundCheck.user == username) 

123 .order_by(BackgroundCheck.dateCompleted.desc())) 

124 for row in allBackgroundChecks: 

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

126 return bgHistory 

127 

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

129 if bonner: 

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

131 

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

133 createdOn = datetime.datetime.now(), 

134 noteContent = noteTextbox, 

135 noteType = "profile") 

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

137 note = noteForDb, 

138 isBonnerNote = bonner, 

139 viewTier = visibility) 

140 return createProfileNote 

141 

142def deleteProfileNote(noteId): 

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

144 

145def updateDietInfo(username, dietContent): 

146 """ 

147 Creates or update a user's diet information 

148 """ 

149 

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

151 

152 return ""