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

94 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-24 14:13 +0000

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

2import re 

3from app.controllers.admin import admin_bp 

4from app.models.user import User 

5from app.models.program import Program 

6from app.logic.userManagement import addCeltsAdmin,addCeltsStudentStaff,removeCeltsAdmin,removeCeltsStudentStaff 

7from app.logic.userManagement import changeCurrentTerm 

8from app.logic.userManagement import changeProgramInfo 

9from app.logic.utils import selectSurroundingTerms 

10from app.logic.userManagement import addNextTerm 

11from app.models.term import Term 

12 

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

14def manageUsers(): 

15 eventData = request.form 

16 user = eventData['user'] 

17 method = eventData['method'] 

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

19 

20 try: 

21 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 if user.isStudent: 

29 flash(user.firstName + " " + user.lastName + " cannot be added as a Celts admin", 'danger') 

30 else: 

31 if user.isCeltsAdmin: 

32 flash(user.firstName + " " + user.lastName + " is already a Celts Admin", 'danger') 

33 else: 

34 addCeltsAdmin(user) 

35 flash(user.firstName + " " + user.lastName + " has been added as a Celts Admin", 'success') 

36 elif method == "addCeltsStudentStaff": 

37 if not user.isStudent: 

38 flash(username + " cannot be added as Celts Student Staff", 'danger') 

39 else: 

40 if user.isCeltsStudentStaff: 

41 flash(user.firstName + " " + user.lastName + " is already a Celts Student Staff", 'danger') 

42 else: 

43 addCeltsStudentStaff(user) 

44 flash(user.firstName + " " + user.lastName + " has been added as a Celts Student Staff", 'success') 

45 elif method == "removeCeltsAdmin": 

46 removeCeltsAdmin(user) 

47 flash(user.firstName + " " + user.lastName + " is no longer a Celts Admin ", 'success') 

48 elif method == "removeCeltsStudentStaff": 

49 removeCeltsStudentStaff(user) 

50 flash(user.firstName + " " + user.lastName + " is no longer a Celts Student Staff", 'success') 

51 return ("success") 

52 

53@admin_bp.route('/addProgramManagers', methods=['POST']) 

54def addProgramManagers(): 

55 eventData = request.form 

56 try: 

57 return addProgramManager(eventData['username'],int(eventData['programID'])) 

58 except Exception as e: 

59 print(e) 

60 flash('Error while trying to add a manager.','warning') 

61 abort(500,"'Error while trying to add a manager.'") 

62 

63@admin_bp.route('/removeProgramManagers', methods=['POST']) 

64def removeProgramManagers(): 

65 eventData = request.form 

66 try: 

67 return removeProgramManager(eventData['username'],int(eventData['programID'])) 

68 except Exception as e: 

69 print(e) 

70 flash('Error while removing a manager.','warning') 

71 abort(500,"Error while trying to remove a manager.") 

72 

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

74def updateProgramInfo(programID): 

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

76 programInfo = request.form # grabs user inputs 

77 if g.current_user.isCeltsAdmin: 

78 try: 

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

80 programInfo["contactEmail"], 

81 programInfo["contactName"], 

82 programInfo["location"], 

83 programID) 

84 

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

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

87 except Exception as e: 

88 print(e) 

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

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

91 abort(403) 

92 

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

94def userManagement(): 

95 terms = selectSurroundingTerms(g.current_term) 

96 current_programs = Program.select() 

97 currentAdmins = list(User.select().where(User.isCeltsAdmin)) 

98 currentStudentStaff = list(User.select().where(User.isCeltsStudentStaff)) 

99 if g.current_user.isCeltsAdmin: 

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

101 terms = terms, 

102 programs = list(current_programs), 

103 currentAdmins = currentAdmins, 

104 currentStudentStaff = currentStudentStaff, 

105 ) 

106 abort(403) 

107 

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

109def changeTerm(): 

110 termData = request.form 

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

112 changeCurrentTerm(term) 

113 return "" 

114 

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

116def addNewTerm(): 

117 addNextTerm() 

118 return ""