Coverage for app/logic/userManagement.py: 100%
52 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-07-31 16:31 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-07-31 16:31 +0000
1from app.models.user import User
2from app.models.term import Term
3from app.models.programManager import ProgramManager
4from app.models.program import Program
5from app.models.eventTemplate import EventTemplate
6from flask import g, session
7from app.logic.createLogs import createActivityLog
8from playhouse.shortcuts import model_to_dict
10def addCeltsAdmin(user):
11 user = User.get_by_id(user)
12 user.isCeltsAdmin = True
13 user.save()
14 createActivityLog(f'Made {user.firstName} {user.lastName} a CELTS admin member.')
17def addCeltsStudentStaff(user):
18 user = User.get_by_id(user)
19 user.isCeltsStudentStaff = True
20 user.save()
21 createActivityLog(f'Made {user.firstName} {user.lastName} a CELTS student staff member.')
24def removeCeltsAdmin(user):
25 user = User.get_by_id(user)
26 user.isCeltsAdmin = False
27 user.save()
28 createActivityLog(f'Removed {user.firstName} {user.lastName} from CELTS admins.')
31def removeCeltsStudentStaff(user):
32 user = User.get_by_id(user)
33 programManagerRoles = list([obj.program.programName for obj in ProgramManager.select(Program).join(Program).where(ProgramManager.user == user)])
34 programManagerRoles = ", ".join(programManagerRoles)
35 ProgramManager.delete().where(ProgramManager.user_id == user).execute()
36 user.isCeltsStudentStaff = False
37 user.save()
38 createActivityLog(f'Removed {user.firstName} {user.lastName} from a CELTS student staff member'+
39 (f', and as a manager of {programManagerRoles}.' if programManagerRoles else "."))
42def changeProgramInfo(newProgramName, newContactEmail, newContactName, newLocation, programId):
43 """Updates the program info with a new sender and email."""
44 program = Program.get_by_id(programId)
45 updatedProgram = Program.update({Program.programName:newProgramName, Program.contactEmail: newContactEmail, Program.contactName:newContactName, Program.defaultLocation:newLocation}).where(Program.id==programId)
46 updatedProgram.execute()
47 if newProgramName != program.programName:
48 createActivityLog(f"{program.programName} Program Name was changed to: {newProgramName}")
49 if newContactEmail != program.contactEmail:
50 createActivityLog(f"{program.programName} Contact Email was changed to: {newContactEmail}")
51 if newContactName != program.contactName:
52 createActivityLog(f"{program.programName} Contact Name was changed to: {newContactName}")
53 if newLocation != program.defaultLocation:
54 createActivityLog(f"{program.programName} Location was changed to: {newLocation}")
56 return (f'Program email info updated')
58def getAllowedPrograms(currentUser):
59 """Returns a list of all visible programs depending on who the current user is."""
60 if currentUser.isCeltsAdmin:
61 return Program.select().order_by(Program.programName)
62 else:
63 return Program.select().join(ProgramManager).where(ProgramManager.user==currentUser).order_by(Program.programName)
67def getAllowedTemplates(currentUser):
68 """Returns a list of all visible templates depending on who the current user is. If they are not an admin it should always be none."""
69 if currentUser.isCeltsAdmin:
70 return EventTemplate.select().where(EventTemplate.isVisible==True).order_by(EventTemplate.name)
71 else:
72 return []