-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalterLSF.py
More file actions
180 lines (170 loc) · 9.87 KB
/
alterLSF.py
File metadata and controls
180 lines (170 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from datetime import date, datetime
from flask import json, jsonify, request, flash
from app.controllers.main_routes import *
from app.controllers.main_routes.main_routes import *
from app.controllers.main_routes.laborHistory import *
from app.models.formHistory import FormHistory
from app.models.user import User
from app.models.supervisor import Supervisor
from app.models.activePosition import ActivePosition
from app.logic.emailHandler import *
from app.login_manager import require_login
from app.logic.tracy import Tracy, InvalidQueryException
from app.models.notes import Notes
from app.models.supervisor import Supervisor
from app.login_manager import require_login
from app.logic.alterLSF import modifyLSF, adjustLSF
@main_bp.route("/alterLSF/<laborStatusKey>", methods=["GET"])
def alterLSF(laborStatusKey):
"""
This function gets all the form's data and populates the front end
"""
currentUser = require_login()
if not currentUser: # Not logged in
return render_template("errors/403.html")
if not currentUser.isLaborAdmin: # Not an admin
if currentUser.student and not currentUser.supervisor: # If a student is logged in and trying to get to this URL then send them back to their own page.
return redirect("/laborHistory/" + currentUser.student.ID)
currentDate = date.today()
#If logged in....
#Step 1: get form attached to the student (via labor history modal)
form = LaborStatusForm.get(LaborStatusForm.laborStatusFormID == laborStatusKey)
# If todays date is greater than the adjustment cut off date on the term and the form is an adjustment LSF,
# then we do not want to give users access to the adjustment page
# Query the status of the form to determine if correction or adjust LSF
formStatus = (FormHistory.get(FormHistory.formID == laborStatusKey).status_id)
if currentDate > form.termCode.adjustmentCutOff and formStatus == "Approved" and not currentUser.isLaborAdmin:
return render_template("errors/403.html")
#Step 2: get prefill data from said form, then the data that populates dropdowns for supervisors and position
prefillstudent = form.studentSupervisee.FIRST_NAME + " "+ form.studentSupervisee.LAST_NAME+" ("+form.studentSupervisee.ID+")"
prefillsupervisor = form.supervisor.FIRST_NAME +" "+ form.supervisor.LAST_NAME
prefillsupervisorPIDM = form.supervisor.PIDM
superviser_id = form.supervisor.ID
prefilldepartment = form.department.ORG
prefillposition = form
prefilljobtype = form.jobType
prefillterm = form.termCode
prefillstartdate = form.startDate
prefillenddate = form.endDate
totalHours = 0
if form.weeklyHours != None:
prefillhours = form.weeklyHours
allTermForms = LaborStatusForm.select().join_from(LaborStatusForm, Student).where((LaborStatusForm.termCode == form.termCode) & (LaborStatusForm.laborStatusFormID != laborStatusKey) & (LaborStatusForm.studentSupervisee.ID == form.studentSupervisee.ID))
if allTermForms:
for i in allTermForms:
totalHours += i.weeklyHours
else:
prefillhours = form.contractHours
#These are the data fields to populate our dropdowns(Supervisor. Position)
supervisors = Supervisor.select() #.where(Supervisor.isActive) if we want supervisors to be active one
positions = (
ActivePosition
.select(ActivePosition, Department)
.join(Department, on=(ActivePosition.department == Department.departmentID))
.where((Department.ACCOUNT == form.department.ACCOUNT) & (Department.ORG == form.department.ORG))
)
departments = Department.select()
# supervisors from the old system WILL have a Supervisor record, but might not have a Tracy record
oldSupervisor = Supervisor.get_or_none(ID = form.supervisor.ID)
if not oldSupervisor:
try:
oldSupervisor = Tracy().getSupervisorFromID(form.supervisor.ID)
except InvalidQueryException:
print("The bnumber {} was not found in Supervisor or Tracy", form.supervisor.ID)
oldSupervisor = {'ID': form.supervisor.ID}
notes = Notes.select().where(Notes.formID == laborStatusKey, Notes.noteType == "Supervisor Note") # Gets labor department notes from the laborofficenotes table
return render_template( "main/alterLSF.html",
title=("Adjust Labor Status Form" if formStatus == "Approved" else "Labor Status Correction Form"),
username = currentUser,
superviser_id = superviser_id,
prefillstudent = prefillstudent,
prefillsupervisor = prefillsupervisor,
prefillsupervisorPIDM = prefillsupervisorPIDM,
prefilldepartment = prefilldepartment,
prefillposition = prefillposition,
prefilljobtype = prefilljobtype,
prefillterm = prefillterm,
prefillstartdate = prefillstartdate,
prefillenddate = prefillenddate,
prefillhours = prefillhours,
supervisors = supervisors,
positions = positions,
departments=departments,
form = form,
oldSupervisor = oldSupervisor,
totalHours = totalHours,
currentUser = currentUser,
notes = notes
)
@main_bp.route("/alterLSF/getDate/<termcode>", methods=['GET'])
def getDate(termcode):
""" Get the start and end dates of the selected term. """
dates = Term.select().where(Term.termCode == termcode)
datesDict = {}
for date in dates:
start = date.termStart
end = date.termEnd
primaryCutOff = date.primaryCutOff
if primaryCutOff is None:
datesDict[date.termCode] = {"Start Date":datetime.strftime(start, "%m/%d/%Y") , "End Date": datetime.strftime(end, "%m/%d/%Y")}
else:
datesDict[date.termCode] = {"Start Date":datetime.strftime(start, "%m/%d/%Y") , "End Date": datetime.strftime(end, "%m/%d/%Y"), "Primary Cut Off": datetime.strftime(primaryCutOff, "%m/%d/%Y"), "isBreak": date.isBreak, "isSummer": date.isSummer}
return json.dumps(datesDict)
@main_bp.route("/alterLSF/fetchPositions/<departmentOrg>/<departmentAccount>", methods=['GET'])
def fetchPositions(departmentOrg, departmentAccount):
currentUser = require_login()
positions = (
ActivePosition
.select(ActivePosition, Department)
.join(Department, on=(ActivePosition.department == Department.departmentID))
.where((Department.ACCOUNT == departmentAccount) & (Department.ORG == departmentOrg))
)
for position in positions:
if position.POSN_CODE != "S12345" or currentUser.isLaborAdmin:
positionDict[position.POSN_CODE] = {"POSN_TITLE": position.POSN_TITLE, "WLS": position.WLS, "POSN_CODE": position.POSN_CODE}
return json.dumps(positionDict)
@main_bp.route("/alterLSF/submitAlteredLSF/<laborStatusKey>", methods=["POST"])
def submitAlteredLSF(laborStatusKey):
"""
Submits an altered LSF form and creates a formHistory entry if appropriate
"""
try:
currentUser = require_login()
if not currentUser: # Not logged in
return render_template("errors/403.html")
currentDate = datetime.now().strftime("%Y-%m-%d")
fieldsChanged = eval(request.data.decode("utf-8")) # This fixes byte indices must be intergers or slices error
fieldsChanged = dict(fieldsChanged)
student = LaborStatusForm.get(LaborStatusForm.laborStatusFormID == laborStatusKey)
formStatus = (FormHistory.get(FormHistory.formID == laborStatusKey).status_id)
formHistoryIDs = []
lsf = LaborStatusForm.get(LaborStatusForm.laborStatusFormID == laborStatusKey)
for fieldName in fieldsChanged:
if formStatus =="Pending" or formStatus == "Pre-Student Approval":
modifyLSF(fieldsChanged, fieldName, lsf, currentUser, host=request.host)
elif formStatus =="Approved":
changedForm = adjustLSF(fieldsChanged, fieldName, lsf, currentUser, host=request.host)
if changedForm:
formHistoryIDs.append(changedForm)
if formStatus == "Approved":
for formHistory in formHistoryIDs:
try:
email = emailHandler(formHistory)
if "supervisor" in fieldsChanged:
email.laborStatusFormAdjusted(fieldsChanged["supervisor"]["newValue"])
else:
email.laborStatusFormAdjusted()
except Exception as e:
print("An error occured while attempting to send adjustment form emails: ", e)
message = "Your labor adjustment form(s) for {0} {1} have been submitted.".format(student.studentSupervisee.FIRST_NAME, student.studentSupervisee.LAST_NAME)
else:
message = "Your labor status form for {0} {1} has been modified.".format(student.studentSupervisee.FIRST_NAME, student.studentSupervisee.LAST_NAME)
flash(message, "success")
return jsonify({"Success": True})
except Exception as e:
message = "An error occured. Your labor {0} form(s) for {1} {2} were not submitted.".format("status" if formStatus == "Pending" else "adjustment",
student.studentSupervisee.FIRST_NAME,
student.studentSupervisee.LAST_NAME)
flash(message, "danger")
print("An error occured during form submission:", e)
return jsonify({"Success": False}), 500