Coverage for app/logic/userManagement.py: 96%

76 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-03-11 22:25 +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, flash 

7from app.logic.createLogs import createAdminLog 

8from playhouse.shortcuts import model_to_dict 

9from typing import List 

10 

11def addCeltsAdmin(user: str) -> None: 

12 if user.isCeltsAdmin: 

13 raise Exception(f"{user.fullName} is already a CELTS Admin.") 

14 elif user.isStudent and not user.isCeltsStudentStaff: 

15 raise Exception(f"{user.fullName} cannot be added as CELTS Admin.") 

16 else: 

17 user: User = User.get_by_id(user) 

18 user.isCeltsAdmin = True 

19 user.save() 

20 createAdminLog(f'Made f"{user.fullName} a CELTS admin member.') 

21 

22def addCeltsStudentStaff(user: str) -> None: 

23 if user.isCeltsStudentStaff: 

24 raise Exception(f"{user.fullName} is already a CELTS Student Staff.") 

25 elif user.isStudent: 

26 user: User = User.get_by_id(user) 

27 user.isCeltsStudentStaff = True 

28 user.save() 

29 createAdminLog(f'Made f"{user.fullName} a CELTS Student Staff.') 

30 else: 

31 raise Exception(f"{user.fullName} cannot be added as CELTS Student Staff.") 

32 

33 

34def addCeltsStudentAdmin(user: str) -> None: 

35 if user.isCeltsStudentAdmin: 

36 raise Exception(f"{user.fullName} is already a CELTS Student Admin.") 

37 elif user.isStudent: 

38 user: User = User.get_by_id(user) 

39 user.isCeltsStudentAdmin = True 

40 user.save() 

41 createAdminLog(f'Made {user.fullName} a CELTS Student Admin.') 

42 else: 

43 raise Exception(f"{user.fullName} cannot be added as CELTS Student Admin.") 

44 

45 

46def removeCeltsAdmin(user: str) -> None: 

47 user: User = User.get_by_id(user) 

48 user.isCeltsAdmin = False 

49 user.save() 

50 createAdminLog(f'Removed f"{user.fullName} from CELTS admins.') 

51 

52def removeCeltsStudentStaff(user: str) -> None: 

53 user: User = User.get_by_id(user) 

54 programManagerRoles: List[str] = list([obj.program.programName for obj in ProgramManager.select(Program).join(Program).where(ProgramManager.user == user)]) 

55 programManagerRoles: str = ", ".join(programManagerRoles) 

56 ProgramManager.delete().where(ProgramManager.user_id == user).execute() 

57 user.isCeltsStudentStaff = False 

58 user.save() 

59 createAdminLog(f'Removed {user.firstName} {user.lastName} from a CELTS student staff member'+ 

60 (f', and as a manager of {programManagerRoles}.' if programManagerRoles else ".")) 

61 

62def removeCeltsStudentAdmin(user: str) -> None: 

63 user: User = User.get_by_id(user) 

64 user.isCeltsStudentAdmin = False 

65 user.save() 

66 createAdminLog(f'Removed f"{user.fullName} from a CELTS student admins.') 

67 

68def changeProgramInfo(newProgramName: str, newContactEmail: str, newContactName: str, newLocation: str, programId: int) -> str: 

69 """Updates the program info with a new sender and email.""" 

70 program: Program = Program.get_by_id(programId) 

71 Program.update({Program.programName:newProgramName, Program.contactEmail: newContactEmail, Program.contactName:newContactName, Program.defaultLocation:newLocation}).where(Program.id == programId).execute() 

72 if newProgramName != program.programName: 

73 createAdminLog(f"{program.programName} Program Name was changed to: {newProgramName}") 

74 if newContactEmail != program.contactEmail: 

75 createAdminLog(f"{program.programName} Contact Email was changed to: {newContactEmail}") 

76 if newContactName != program.contactName: 

77 createAdminLog(f"{program.programName} Contact Name was changed to: {newContactName}") 

78 if newLocation != program.defaultLocation: 

79 createAdminLog(f"{program.programName} Location was changed to: {newLocation}") 

80 

81 return (f'Program email info updated') 

82 

83def getAllowedPrograms(currentUser: User) -> List[Program]: 

84 """Returns a list of all visible programs depending on who the current user is.""" 

85 if currentUser.isCeltsAdmin: 

86 return list(Program.select().order_by(Program.programName)) 

87 elif currentUser.isCeltsStudentAdmin: 

88 return list(Program.select().where(Program.programName != "Bonner Scholars").order_by(Program.programName)) 

89 else: 

90 return list(Program.select().join(ProgramManager).where(ProgramManager.user==currentUser).order_by(Program.programName)) 

91 

92 

93def getAllowedTemplates(currentUser: User) -> List[EventTemplate]: 

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

95 if currentUser.isCeltsAdmin or currentUser.isCeltsStudentAdmin: 

96 return EventTemplate.select().where(EventTemplate.isVisible==True).order_by(EventTemplate.name) 

97 else: 

98 return []