Coverage for app/controllers/admin/userManagement.py: 25%
92 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-25 18:59 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-25 18:59 +0000
1from flask import render_template,request, flash, g, 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 changeProgramInfo
8from app.logic.utils import selectSurroundingTerms
9from app.logic.term import addNextTerm, changeCurrentTerm
11@admin_bp.route('/admin/manageUsers', methods = ['POST'])
12def manageUsers():
13 eventData = request.form
14 user = eventData['user']
15 method = eventData['method']
16 username = re.sub("[()]","", (user.split())[-1])
18 try:
19 user = User.get_by_id(username)
20 except Exception as e:
21 print(e)
22 flash(username + " is an invalid user.", "danger")
23 return ("danger", 500)
25 if method == "addCeltsAdmin":
26 if user.isStudent and not user.isCeltsStudentStaff:
27 flash(user.firstName + " " + user.lastName + " cannot be added as a CELTS-Link admin", 'danger')
28 else:
29 if user.isCeltsAdmin:
30 flash(user.firstName + " " + user.lastName + " is already a CELTS-Link Admin", 'danger')
31 else:
32 addCeltsAdmin(user)
33 flash(user.firstName + " " + user.lastName + " has been added as a CELTS-Link Admin", 'success')
34 elif method == "addCeltsStudentStaff":
35 if not user.isStudent:
36 flash(username + " cannot be added as CELTS Student Staff", 'danger')
37 else:
38 if user.isCeltsStudentStaff:
39 flash(user.firstName + " " + user.lastName + " is already a CELTS Student Staff", 'danger')
40 else:
41 addCeltsStudentStaff(user)
42 flash(user.firstName + " " + user.lastName + " has been added as a CELTS Student Staff", 'success')
43 elif method == "removeCeltsAdmin":
44 removeCeltsAdmin(user)
45 flash(user.firstName + " " + user.lastName + " is no longer a CELTS Admin ", 'success')
46 elif method == "removeCeltsStudentStaff":
47 removeCeltsStudentStaff(user)
48 flash(user.firstName + " " + user.lastName + " is no longer a CELTS Student Staff", 'success')
49 return ("success")
51@admin_bp.route('/addProgramManagers', methods=['POST'])
52def addProgramManagers():
53 eventData = request.form
54 try:
55 return addProgramManager(eventData['username'],int(eventData['programID']))
56 except Exception as e:
57 print(e)
58 flash('Error while trying to add a manager.','warning')
59 abort(500,"'Error while trying to add a manager.'")
61@admin_bp.route('/removeProgramManagers', methods=['POST'])
62def removeProgramManagers():
63 eventData = request.form
64 try:
65 return removeProgramManager(eventData['username'],int(eventData['programID']))
66 except Exception as e:
67 print(e)
68 flash('Error while removing a manager.','warning')
69 abort(500,"Error while trying to remove a manager.")
71@admin_bp.route('/admin/updateProgramInfo/<programID>', methods=['POST'])
72def updateProgramInfo(programID):
73 """Grabs info and then outputs it to logic function"""
74 programInfo = request.form # grabs user inputs
75 if g.current_user.isCeltsAdmin:
76 try:
77 changeProgramInfo(programInfo["programName"], #calls logic function to add data to database
78 programInfo["contactEmail"],
79 programInfo["contactName"],
80 programInfo["location"],
81 programID)
83 flash("Program updated", "success")
84 return redirect(url_for("admin.userManagement", accordion="program"))
85 except Exception as e:
86 print(e)
87 flash('Error while updating program info.','warning')
88 abort(500,'Error while updating program.')
89 abort(403)
91@admin_bp.route('/admin', methods = ['GET'])
92def userManagement():
93 terms = selectSurroundingTerms(g.current_term)
94 current_programs = Program.select()
95 currentAdmins = list(User.select().where(User.isCeltsAdmin))
96 currentStudentStaff = list(User.select().where(User.isCeltsStudentStaff))
97 if g.current_user.isCeltsAdmin:
98 return render_template('admin/userManagement.html',
99 terms = terms,
100 programs = list(current_programs),
101 currentAdmins = currentAdmins,
102 currentStudentStaff = currentStudentStaff,
103 )
104 abort(403)
106@admin_bp.route('/admin/changeTerm', methods=['POST'])
107def changeTerm():
108 termData = request.form
109 term = int(termData["id"])
110 changeCurrentTerm(term)
111 return ""
113@admin_bp.route('/admin/addNewTerm', methods = ['POST'])
114def addNewTerm():
115 addNextTerm()
116 return ""