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

57 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-09-13 19:44 +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 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) 

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