Coverage for app/controllers/admin/userManagement.py: 25%

84 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-03-11 22:25 +0000

1from flask import render_template,request, flash, g, abort, redirect, url_for 

2import re 

3from typing import Dict, Any, List 

4from app.controllers.admin import admin_bp 

5from app.models.user import User 

6from app.models.program import Program 

7from app.models.term import Term 

8from app.logic.userManagement import addCeltsAdmin,addCeltsStudentStaff,addCeltsStudentAdmin ,removeCeltsAdmin,removeCeltsStudentStaff, removeCeltsStudentAdmin 

9from app.logic.userManagement import changeProgramInfo 

10from app.logic.utils import selectSurroundingTerms 

11from app.logic.term import addNextTerm, changeCurrentTerm 

12 

13@admin_bp.route('/admin/manageUsers', methods = ['POST']) 

14def manageUsers() -> str: 

15 eventData: Dict[str, str] = request.form 

16 user: str = eventData['user'] 

17 method: str = eventData['method'] 

18 username: str = re.sub("[()]","", (user.split())[-1]) 

19 

20 try: 

21 user: User = User.get_by_id(username) 

22 except Exception as e: 

23 print(e) 

24 flash(username + " is an invalid user.", "danger") 

25 return ("danger", 500) 

26 

27 if method == "addCeltsAdmin": 

28 try: 

29 addCeltsAdmin(user) 

30 flash(f"{user.fullName} has been added as CELTS Admin.", 'success') 

31 except Exception as errorMessage: 

32 flash(str(errorMessage), 'danger') 

33 

34 elif method == "addCeltsStudentStaff": 

35 try: 

36 addCeltsStudentStaff(user) 

37 flash(f"{user.fullName} has been added as CELTS Student Staff.", 'success') 

38 except Exception as errorMessage: 

39 flash(str(errorMessage), "danger") 

40 

41 elif method == "addCeltsStudentAdmin": 

42 try: 

43 addCeltsStudentAdmin(user) 

44 flash(f"{user.fullName} has been added as CELTS Student Admin.", 'success') 

45 except Exception as errorMessage: 

46 flash(str(errorMessage), "danger") 

47 

48 elif method == "removeCeltsAdmin": 

49 removeCeltsAdmin(user) 

50 flash(f"{user.fullName} is no longer a CELTS Admin.", 'success') 

51 

52 elif method == "removeCeltsStudentStaff": 

53 removeCeltsStudentStaff(user) 

54 flash(f"{user.fullName} is no longer a CELTS Student Staff.", 'success') 

55 

56 elif method == "removeCeltsStudentAdmin": 

57 removeCeltsStudentAdmin(user) 

58 flash(f"{user.fullName} is no longer a CELTS Student Admin.", 'success') 

59 

60 return ("success") 

61 

62@admin_bp.route('/admin/updateProgramInfo/<programID>', methods=['POST']) 

63def updateProgramInfo(programID: int) -> Any: 

64 """Grabs info and then outputs it to logic function""" 

65 programInfo: Dict[str, str] = request.form # grabs user inputs 

66 if g.current_user.isCeltsAdmin or g.current_user.isCeltsStudentAdmin: 

67 try: 

68 changeProgramInfo(programInfo["programName"], #calls logic function to add data to database 

69 programInfo["contactEmail"], 

70 programInfo["contactName"], 

71 programInfo["location"], 

72 programID) 

73 

74 flash("Program updated", "success") 

75 return redirect(url_for("admin.userManagement", accordion="program")) 

76 except Exception as e: 

77 print(e) 

78 flash('Error while updating program info.','warning') 

79 abort(500,'Error while updating program.') 

80 abort(403) 

81 

82@admin_bp.route('/admin', methods = ['GET']) 

83def userManagement() -> str: 

84 terms: List[Term] = selectSurroundingTerms(g.current_term) 

85 currentPrograms: List[Program] = list(Program.select()) 

86 currentAdmins: List[User] = list(User.select().where(User.isCeltsAdmin)) 

87 currentStudentStaff: List[User] = list(User.select().where(User.isCeltsStudentStaff)) 

88 currentStudentAdmin: List[User] = list(User.select().where(User.isCeltsStudentAdmin)) 

89 if g.current_user.isCeltsAdmin or g.current_user.isCeltsStudentAdmin: 

90 return render_template('admin/userManagement.html', 

91 terms = terms, 

92 programs = currentPrograms, 

93 currentAdmins = currentAdmins, 

94 currentStudentStaff = currentStudentStaff, 

95 currentStudentAdmin = currentStudentAdmin, 

96 ) 

97 abort(403) 

98 

99@admin_bp.route('/admin/changeTerm', methods=['POST']) 

100def changeTerm(): 

101 termData = request.form 

102 term = int(termData["id"]) 

103 changeCurrentTerm(term) 

104 return "" 

105 

106@admin_bp.route('/admin/addNewTerm', methods = ['POST']) 

107def addNewTerm(): 

108 addNextTerm() 

109 return ""