Coverage for app/logic/users.py: 100%
54 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-24 14:13 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-24 14:13 +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).singleProgram
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).where(ProgramBan.program == program_id,
107 ProgramBan.user == username,
108 ProgramBan.endDate > datetime.datetime.now()).execute()
110def getUserBGCheckHistory(username):
111 """
112 Get a users background check history
113 """
114 bgHistory = {'CAN': [], 'FBI': [], 'SHS': [], 'BSL': []}
116 allBackgroundChecks = (BackgroundCheck.select(BackgroundCheck, BackgroundCheckType)
117 .join(BackgroundCheckType)
118 .where(BackgroundCheck.user == username)
119 .order_by(BackgroundCheck.dateCompleted.desc()))
120 for row in allBackgroundChecks:
121 bgHistory[row.type_id].append(row)
123 return bgHistory
125def addProfileNote(visibility, bonner, noteTextbox, username):
126 noteForDb = Note.create(createdBy = g.current_user,
127 createdOn = datetime.datetime.now(),
128 noteContent = noteTextbox,
129 noteType = "profile")
130 createProfileNote = ProfileNote.create(user = User.get(User.username == username),
131 note = noteForDb,
132 isBonnerNote = bonner,
133 viewTier = visibility)
134 return createProfileNote
136def deleteProfileNote(noteId):
137 return ProfileNote.delete().where(ProfileNote.id == noteId).execute()
139def updateDietInfo(username, dietContent):
140 """
141 Creates or update a user's diet information
142 """
144 User.update(dietRestriction = dietContent).where(User.username == username).execute()
146 return ""