-
Notifications
You must be signed in to change notification settings - Fork 0
Adding the Manage Members Page #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: department-portal-base
Are you sure you want to change the base?
Changes from 53 commits
dad0a49
0684323
8bcd89b
24d7ca1
194ff1c
52b7efc
c2052a7
a649439
6a8185a
a4648dd
42e0e7c
01baa28
01ee63a
162400e
f04fbce
4f72b3f
c089049
7950862
6979e82
647ba11
28bee64
322f1fd
c9e7871
436198f
97d0460
28490eb
0ee4970
a982310
2fa25e9
c10d0fe
37b715f
08ef1dd
f5a6924
fd37b48
6fd3441
3aaee60
39df63d
e00b702
76b2320
1a923d3
e824434
baa19f1
5e02f2c
ea2e6eb
10bc99e
300e18f
f648de8
7e4d743
fd16541
5de6421
e3ddeb4
39537e4
21dacc7
8bfaf02
1ae75a2
a869602
5d58f3f
f90d754
9a0112a
c6b1424
09346c3
a083395
13b50ce
d5f461c
393d9de
4723ec7
cd508c4
eff7a57
0971c55
661ff17
bc46dae
1e234fe
d2bda86
9dc49ae
813362e
386decf
e805d6f
33a891f
1be130c
17919f5
f6a5a09
09d5b6e
efa9b90
a0fe036
e4436c8
faef000
6d68125
2c4425e
c506a25
2eb341f
5dc2048
3c9c643
6ef0131
075f554
51ee08a
ab9f802
705cda1
35dbd4d
ea760b5
fa6536b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,161 @@ | ||
| from flask import render_template | ||
| from flask import g, jsonify, redirect, render_template, request, url_for | ||
|
|
||
| from app.controllers.main_routes import main_bp | ||
| from app.models.supervisor import Supervisor | ||
| from app.models.supervisorDepartment import SupervisorDepartment | ||
| from app.logic.manageMembers import * | ||
| from app.logic.search import searchPerson | ||
|
|
||
|
|
||
| @main_bp.route('/department/<org>/<account>/members', methods=['GET']) | ||
| def manageMembers(org=None, account=None): | ||
| """Generates the Manage Members page.""" | ||
| currentUser = g.currentUser | ||
|
|
||
| if not currentUser.supervisor: | ||
|
MImran2002 marked this conversation as resolved.
Outdated
|
||
| return redirect(url_for('main.laborhistory', id=currentUser.student.ID)) | ||
|
|
||
| if not (currentUser.isLaborAdmin or currentUser.isLaborDepartmentStudent): | ||
| return render_template('errors/403.html'), 403 | ||
|
|
||
| dept, members = getCurrentDeptMembers(org, account) | ||
| counts = getStudentCounts(dept) | ||
|
BhushanSah marked this conversation as resolved.
Outdated
|
||
| members = attachPositionCounts(members, counts) | ||
|
|
||
| return render_template( | ||
| 'main/manageMembers.html', | ||
| members=members, | ||
| department=dept, | ||
| ) | ||
|
|
||
|
|
||
|
|
||
| @main_bp.route('/members/search/<query>', methods=['GET']) | ||
| def searchMember(query=None): | ||
|
MImran2002 marked this conversation as resolved.
|
||
| """ | ||
| Search supervisors by name or B-number. | ||
| """ | ||
| currentUser = g.currentUser | ||
|
|
||
| if not (currentUser.isLaborAdmin or currentUser.isLaborDepartmentStudent): | ||
| return render_template('errors/403.html'), 403 | ||
|
|
||
| supervisors = ( | ||
| searchPerson(Supervisor, query) | ||
| .order_by(Supervisor.LAST_NAME.asc()) | ||
| .limit(10) | ||
| ) | ||
|
|
||
| supervisors = list(map(supervisorsDbToDict, supervisors)) | ||
|
|
||
| return jsonify(supervisors) | ||
|
|
||
| @main_bp.route('/members/update_coordinator', methods=['POST']) | ||
| def updateCoordinator(): | ||
| """ | ||
| Assigns or unassignes a supervisor as a Labor Coordinator. | ||
| """ | ||
| currentUser = g.currentUser | ||
|
|
||
| if not (currentUser.isLaborAdmin or currentUser.isLaborDepartmentStudent): | ||
|
BhushanSah marked this conversation as resolved.
Outdated
|
||
| return render_template('errors/403.html'), 403 | ||
|
|
||
| supervisorID = request.form.get("supervisorID") | ||
| departmentID = request.form.get("departmentID") | ||
| isCoordinator = request.form.get("isCoordinator") == "true" | ||
|
|
||
| if not supervisorID or not departmentID: | ||
| return "", 400 | ||
|
|
||
| member = SupervisorDepartment.get( | ||
| (SupervisorDepartment.supervisor == supervisorID) & | ||
| (SupervisorDepartment.department == departmentID) | ||
| ) | ||
|
|
||
| member.isCoordinator = isCoordinator | ||
| member.save() | ||
|
|
||
| return "", 200 | ||
|
|
||
|
|
||
| @main_bp.route('/members/update_eligibility', methods=['POST']) | ||
| def updateEligibility(): | ||
| """ | ||
| Updates a supervisor's eligibility status. | ||
| """ | ||
| currentUser = g.currentUser | ||
|
|
||
| if not (currentUser.isLaborAdmin or currentUser.isLaborDepartmentStudent): | ||
| return render_template('errors/403.html'), 403 | ||
|
|
||
| supervisorID = request.form.get("supervisorID") | ||
|
|
||
| if not supervisorID: | ||
| return "", 400 | ||
|
|
||
| member = Supervisor.get(Supervisor.ID == supervisorID) | ||
| member.isBanned = not member.isBanned | ||
| member.save() | ||
|
BhushanSah marked this conversation as resolved.
|
||
|
|
||
| return "", 200 | ||
|
|
||
|
|
||
| @main_bp.route('/members/remove', methods=['DELETE']) | ||
| def removeMember(): | ||
| """ | ||
| Removes a staff member from a department. | ||
| """ | ||
| currentUser = g.currentUser | ||
|
|
||
| if not (currentUser.isLaborAdmin or currentUser.isLaborDepartmentStudent): | ||
| return render_template('errors/403.html'), 403 | ||
|
|
||
| supervisorID = request.form.get("supervisorID") | ||
| departmentID = request.form.get("departmentID") | ||
|
|
||
| if not supervisorID or not departmentID: | ||
| return "", 400 | ||
|
|
||
| member = SupervisorDepartment.get( | ||
| (SupervisorDepartment.supervisor == supervisorID) & | ||
| (SupervisorDepartment.department == departmentID) | ||
| ) | ||
|
|
||
| member.delete_instance() | ||
|
|
||
| return "", 200 | ||
|
|
||
|
|
||
|
|
||
| @main_bp.route('/members/add', methods=['POST']) | ||
| def addUserToDept(): | ||
| """ | ||
| Adds a user to a department. | ||
| """ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs authentication. Who can add a supervisor to a department? |
||
| currentUser = g.currentUser | ||
|
|
||
| if not (currentUser.isLaborAdmin or currentUser.isLaborDepartmentStudent): | ||
| return render_template('errors/403.html'), 403 | ||
|
|
||
| supervisorID = request.form.get("supervisorID") | ||
| departmentID = request.form.get("departmentID") | ||
|
|
||
| if not supervisorID or not departmentID: | ||
| return "", 400 | ||
|
|
||
| try: | ||
| supervisorDeptRecord = SupervisorDepartment.get_or_none(supervisor=supervisorID, department=departmentID) | ||
|
|
||
| if supervisorDeptRecord: | ||
| return "False" | ||
|
|
||
| if not Supervisor.get_or_none(Supervisor.ID == supervisorID): | ||
|
BhushanSah marked this conversation as resolved.
Outdated
|
||
| return "", 400 | ||
|
|
||
| SupervisorDepartment.create(supervisor=supervisorID, department=departmentID) | ||
|
|
||
| return "True" | ||
|
|
||
| except Exception as e: | ||
| print(f'Could not add user to department: {e}') | ||
| return "", 500 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| from datetime import date | ||
|
|
||
| from flask import abort | ||
| from peewee import Case, DoesNotExist, fn | ||
|
|
||
| from app.logic.search import usernameFromEmail | ||
| from app.models.department import Department | ||
| from app.models.formHistory import FormHistory | ||
| from app.models.laborReleaseForm import LaborReleaseForm | ||
| from app.models.laborStatusForm import LaborStatusForm | ||
| from app.models.supervisor import Supervisor | ||
| from app.models.supervisorDepartment import SupervisorDepartment | ||
|
|
||
|
|
||
| def supervisorsDbToDict(supervisor): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what you should do is remove this function totally in app>logic>getsupervisors.py there is a function call buildSupervisorDisplay that is technically what you are doing and that function is there but it is smaller go ahead and make changes to it so that it reflects what you want to but at the same time there is a route that uses the buildsupervisordisplay function so make the test for the logic and controller are not broken and are returning right |
||
| """ | ||
| Given a supervisor object it will return a mapped Dict with supervisor data. | ||
| """ | ||
| dbToDict = { | ||
| 'username': usernameFromEmail(supervisor.EMAIL.strip()), | ||
| 'firstName': supervisor.FIRST_NAME.strip(), | ||
| 'lastName': supervisor.LAST_NAME.strip(), | ||
| 'bnumber': supervisor.ID.strip(), | ||
| 'department': supervisor.DEPT_NAME.strip(), | ||
| 'type': 'Supervisor' | ||
| } | ||
| return dbToDict | ||
|
|
||
|
|
||
| def getCurrentDeptMembers(org, account): | ||
|
brightfietsop-ux marked this conversation as resolved.
Outdated
|
||
| """Return the current department and its supervisor-department rows.""" | ||
| try: | ||
| dept = Department.get(Department.ORG == org, Department.ACCOUNT == account) | ||
| except (NameError, DoesNotExist): | ||
| abort(404) | ||
|
|
||
| members = list( | ||
| SupervisorDepartment | ||
| .select(SupervisorDepartment, Supervisor) | ||
| .join(Supervisor) | ||
| .where(SupervisorDepartment.department == dept) | ||
| ) | ||
|
BhushanSah marked this conversation as resolved.
Outdated
|
||
|
|
||
| return dept, members | ||
|
|
||
|
|
||
| def getStudentCounts(dept): | ||
|
BhushanSah marked this conversation as resolved.
Outdated
|
||
| """Active/pending primary/secondary position counts, keyed by (dept, supervisor).""" | ||
| today = date.today() | ||
|
|
||
| releasedFormIds = ( | ||
| FormHistory | ||
| .select(FormHistory.formID) | ||
| .join(LaborReleaseForm) | ||
| .where( | ||
| (FormHistory.historyType == "Labor Release Form") & | ||
| (FormHistory.status == "Approved") & | ||
| (LaborReleaseForm.releaseDate <= today) | ||
| ) | ||
| ) | ||
|
|
||
| activePrimaries = ( | ||
| (LaborStatusForm.jobType == 'Primary') & | ||
| (LaborStatusForm.studentConfirmation == True) | ||
| ) | ||
| pendingPrimaries = ( | ||
| (LaborStatusForm.jobType == 'Primary') & | ||
| (LaborStatusForm.studentConfirmation.is_null(True)) | ||
| ) | ||
| activeSecondaries = ( | ||
| (LaborStatusForm.jobType == 'Secondary') & | ||
| (LaborStatusForm.studentConfirmation == True) | ||
| ) | ||
| pendingSecondaries = ( | ||
| (LaborStatusForm.jobType == 'Secondary') & | ||
| (LaborStatusForm.studentConfirmation.is_null(True)) | ||
| ) | ||
|
|
||
| rows = list( | ||
| LaborStatusForm | ||
| .select( | ||
| fn.SUM(Case(None, ((activePrimaries, 1),), 0)).alias("active_primary_positions"), | ||
| fn.SUM(Case(None, ((pendingPrimaries, 1),), 0)).alias("pending_primary_positions"), | ||
| fn.SUM(Case(None, ((activeSecondaries, 1),), 0)).alias("active_secondary_positions"), | ||
| fn.SUM(Case(None, ((pendingSecondaries, 1),), 0)).alias("pending_secondary_positions"), | ||
| LaborStatusForm.department, | ||
| LaborStatusForm.supervisor | ||
| ) | ||
| .where( | ||
| (LaborStatusForm.department == dept) & | ||
| (LaborStatusForm.laborStatusFormID.not_in(releasedFormIds)) | ||
| ) | ||
| .group_by(LaborStatusForm.department, LaborStatusForm.supervisor) | ||
| .dicts() | ||
| ) | ||
|
|
||
| return {(row["department"], row["supervisor"]): row for row in rows} | ||
|
|
||
|
|
||
| def attachPositionCounts(members, counts): | ||
| """Attach position counts to each supervisor-department row.""" | ||
| fields = [ | ||
| "active_primary_positions", | ||
| "pending_primary_positions", | ||
| "active_secondary_positions", | ||
| "pending_secondary_positions", | ||
| ] | ||
|
|
||
| for member in members: | ||
| row = counts.get((member.department_id, member.supervisor_id), {}) | ||
|
|
||
| for field in fields: | ||
| setattr(member, field, row.get(field, 0)) | ||
|
|
||
| return members | ||
Uh oh!
There was an error while loading. Please reload this page.