Coverage for app/logic/users.py: 100%
56 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-09-13 18:43 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-09-13 18:43 +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.backgroundCheck import BackgroundCheck
9from app.models.backgroundCheckType import BackgroundCheckType
10from app.logic.volunteers import addUserBackgroundCheck
11import datetime
12from peewee import JOIN
13from dateutil import parser
14from flask import g
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.
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
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
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
48 """
49 interestToDelete = Interest.get_or_none(Interest.program == program_id, Interest.user == username)
50 if interestToDelete:
51 interestToDelete.delete_instance()
52 return True
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)
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)
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 noteForDb = Note.create(createdBy = creator,
80 createdOn = datetime.datetime.now(),
81 noteContent = note,
82 isPrivate = 0,
83 noteType = "ban")
85 ProgramBan.create(program = program_id,
86 user = username,
87 endDate = banEndDate,
88 banNote = noteForDb)
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 .where(ProgramBan.program == program_id,
108 ProgramBan.user == username,
109 ProgramBan.endDate > datetime.datetime.now())).execute()
111def getUserBGCheckHistory(username):
112 """
113 Get a users background check history
114 """
115 bgHistory = {'CAN': [], 'FBI': [], 'SHS': [], 'BSL': [],'DDC':[]}
117 allBackgroundChecks = (BackgroundCheck.select(BackgroundCheck, BackgroundCheckType)
118 .join(BackgroundCheckType)
119 .where(BackgroundCheck.user == username)
120 .order_by(BackgroundCheck.dateCompleted.desc()))
121 for row in allBackgroundChecks:
122 bgHistory[row.type_id].append(row)
123 return bgHistory
125def addProfileNote(visibility, bonner, noteTextbox, username):
126 if bonner:
127 visibility = 1 # bonner notes are always admins and the student
129 noteForDb = Note.create(createdBy = g.current_user,
130 createdOn = datetime.datetime.now(),
131 noteContent = noteTextbox,
132 noteType = "profile")
133 createProfileNote = ProfileNote.create(user = User.get(User.username == username),
134 note = noteForDb,
135 isBonnerNote = bonner,
136 viewTier = visibility)
137 return createProfileNote
139def deleteProfileNote(noteId):
140 return ProfileNote.delete().where(ProfileNote.id == noteId).execute()
142def updateDietInfo(username, dietContent):
143 """
144 Creates or update a user's diet information
145 """
147 User.update(dietRestriction = dietContent).where(User.username == username).execute()
149 return ""