Coverage for app/logic/volunteers.py: 96%
49 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-07-29 13:28 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-07-29 13:28 +0000
1from app.models.eventParticipant import EventParticipant
2from app.models.user import User
3from app.models.event import Event
4from app.models.eventRsvp import EventRsvp
5from app.models.program import Program
6from app.models.backgroundCheck import BackgroundCheck
7from app.models.programManager import ProgramManager
8from datetime import datetime, date
9from app.logic.createLogs import createActivityLog
11def getEventLengthInHours(startTime, endTime, eventDate):
12 """
13 Converts the event length hours into decimal
14 parameters: startTime- start time event (type: time)
15 endTime- end time event (type: time)
16 eventDate- date of the event (type: datetime)
17 """
18 #can only subtract datetime objects, not time objects. So convert time into datetime
19 eventLength = datetime.combine(eventDate, endTime) - datetime.combine(eventDate, startTime)
20 eventLengthInHours = round(eventLength.seconds/3600, 2)
21 return eventLengthInHours
24def updateEventParticipants(participantData):
25 """
26 Create new entry in event participant table if user does not exist. Otherwise, updates the record.
28 param: participantData- an ImmutableMultiDict that contains data from every row of the page along with the associated username.
29 """
30 event = Event.get_or_none(Event.id==participantData['event'])
31 if not event:
32 raise Exception("Event does not exist.") # ???
33 return False
36 for username in participantData.getlist("username"):
37 userObject = User.get_or_none(User.username==username)
38 eventParticipant = EventParticipant.get_or_none(user=userObject, event=participantData['event'])
39 if userObject:
40 if participantData.get(f'checkbox_{username}'): #if the user is marked as present
41 inputHours = participantData.get(f'inputHours_{username}')
42 hoursEarned = float(inputHours) if inputHours else 0
43 if eventParticipant:
44 ((EventParticipant.update({EventParticipant.hoursEarned: hoursEarned})
45 .where(EventParticipant.event==event.id, EventParticipant.user==userObject.username))
46 .execute())
47 else:
48 EventParticipant.create(user=userObject, event=event, hoursEarned=hoursEarned)
49 else:
50 ((EventParticipant.delete()
51 .where(EventParticipant.user==userObject.username, EventParticipant.event==event.id))
52 .execute())
53 else:
54 return False
55 return True
57def addUserBackgroundCheck(user, bgType, bgStatus, dateCompleted):
58 """
59 Changes the status of a users background check depending on what was marked
60 on their volunteer profile.
61 """
62 today = date.today()
63 user = User.get_by_id(user)
64 if bgStatus == '' and dateCompleted == '':
65 createActivityLog(f"Marked {user.firstName} {user.lastName}'s background check for {bgType} as 'in progress'.")
66 else:
67 if not dateCompleted:
68 dateCompleted = None
69 update = BackgroundCheck.create(user=user, type=bgType, backgroundCheckStatus=bgStatus, dateCompleted=dateCompleted)
70 if bgStatus == 'Submitted':
71 createActivityLog(f"Marked {user.firstName} {user.lastName}'s background check for {bgType} as submitted.")
72 elif bgStatus == 'Passed':
73 createActivityLog(f"Marked {user.firstName} {user.lastName}'s background check for {bgType} as passed.")
74 else:
75 createActivityLog(f"Marked {user.firstName} {user.lastName}'s background check for {bgType} as failed.")
77def setProgramManager(username, program_id, action):
78 '''
79 adds and removes the studentstaff from program that makes them student manager.
81 param: uername - a string
82 program_id - id
83 action: add, remove
85 '''
86 studentstaff=User.get(User.username==username)
87 if action == "add" and studentstaff.isCeltsStudentStaff==True:
88 studentstaff.addProgramManager(program_id)
89 elif action == "remove":
90 studentstaff.removeProgramManager(program_id)