forked from BitsByBrandon/Comp3613Assignment1
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwsgi.py
More file actions
363 lines (264 loc) · 11 KB
/
Copy pathwsgi.py
File metadata and controls
363 lines (264 loc) · 11 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import click, pytest, sys
from flask.cli import with_appcontext, AppGroup
from App.database import db, get_migrate
from App.models import User
from App.models import Student
from App.models import Staff
from App.models import Request
from App.main import create_app
from App.controllers.student_controller import *
from App.controllers.staff_controller import *
from App.controllers.app_controller import *
from App.controllers import ( create_user, get_all_users_json, get_all_users, initialize )
'''APP COMMANDS(TESTING PURPOSES)'''
# This commands file allow you to create convenient CLI commands for testing controllers
app = create_app()
migrate = get_migrate(app)
# This command creates and initializes the database
@app.cli.command("init", help="Creates and initializes the database")
def init():
initialize()
print('database intialized')
@app.cli.command("listUsers", help="Lists all users in the database")
def listUsers():
listAllUsers()
#Comamand to list all staff in the database
@app.cli.command ("listStaff", help="Lists all staff in the database")
def listStaff():
printAllStaff()
#Comamand to list all students in the database
@app.cli.command ("listStudents", help="Lists all students in the database")
def listStudents():
printAllStudents()
#Comamand to list all requests in the database
@app.cli.command ("listRequests", help="Lists all requests in the database")
def listRequests():
listAllRequests()
#Comamand to list all approved requests in the database
@app.cli.command ("listApprovedRequests", help="Lists all approved requests in the database")
def listApprovedRequests():
listAllApprovedRequests()
#Comamand to list all pending requests in the database
@app.cli.command ("listPendingRequests", help="Lists all pending requests in the database")
def listPendingRequests():
listAllPendingRequests()
#Comamand to list all denied requests in the database
@app.cli.command ("listDeniedRequests", help="Lists all denied requests in the database")
def listDeniedRequests():
listAllDeniedRequests()
#Comamand to list all logged hours in the database
@app.cli.command ("listloggedHours", help="Lists all logged hours in the database")
def listloggedHours():
listAllloggedHours()
'''STUDENT COMMANDS'''
student_cli = AppGroup('student', help='Student object commands')
#Command to view total approved hours for a student (student_id)
@student_cli.command("hours", help="View total approved hours for a student")
def hours ():
print("\n")
try:
student_id = int(input("Enter your student ID: "))
student = get_approved_hours(student_id)
name,total_hours = student
print(f"Total approved hours for {name}: {total_hours}")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#Command to create a new student (name, email)
@student_cli.command("create", help="Create a new student")
def create_student():
print("\n")
try:
name = input("Enter student name: ")
email = input("Enter student email: ")
password = input("Enter student password: ")
student = register_student(name, email, password)
print(f"Created student: {student}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#Command for student to request hour confirmation (student_id, hours)
@student_cli.command("requestHours", help="Student requests hour confirmation (interactive)")
def requestHours():
print("\n")
try:
student_id = int(input("Enter your student ID: "))
hours = float(input("Enter the number of hours to request: "))
req = create_hours_request(student_id,hours)
print(f"Requested {hours} hours for confirmation.\n")
print(f"Request ID: {req.id}, Status: {req.status}")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#command to list all requests made by a specific student(student_id)
@student_cli.command("viewmyRequests", help="List all requests for a student")
def viewmyRequests():
print("\n")
try:
student_id = int(input("Enter your student ID: "))
requests = fetch_requests(student_id)
if not requests:
print(f"No requests found for student {student_id}.")
return
else:
print(f"Requests for student {student_id}:")
for req in requests:
print(req)
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#command to list all accolades for a specific student (student_id)
@student_cli.command("viewmyAccolades", help="List all accolades for a student")
def viewmyAccolades():
print("\n")
try:
student_id = int(input("Enter your student ID: "))
accolades = fetch_accolades(student_id)
if not accolades:
print(f"No accolades found for student {student_id}.")
return
else:
print(f"Accolades for student {student_id}:")
for accolade in accolades:
print(f"- {accolade}")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#Student command to view leaderboard of students by approved hours
@student_cli.command("viewLeaderboard", help="View leaderboard of students by approved hours")
def viewLeaderboard():
print("\n")
try:
leaderboard = generate_leaderboard()
print("Leaderboard (by approved hours):")
if not leaderboard:
print("No students found or hour data found.")
return
for rank, data in enumerate(leaderboard, 1):
print(f"{rank:<6}. {data['name']:<10} ------ \t{data['hours']} hours")
except Exception as e:
print(f"An error occurred while generating the leaderboard: {e}")
print("\n")
app.cli.add_command(student_cli) # add the group to the cli
'''STAFF COMMANDS'''
staff_cli = AppGroup('staff', help='Staff object commands')
#Command to create a new staff member (name, email)
@staff_cli.command("create", help="Create a new staff member")
def create_staff():
print("\n")
try:
name = input("Enter staff name: ")
email = input("Enter staff email: ")
password = input("Enter staff password: ")
staff = register_staff(name, email, password)
print(f"Created staff member: {staff}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#Command for staff to view all pending requests
@staff_cli.command("requests", help="View all pending hour requests")
def requests():
try:
pending_requests = fetch_all_requests()
if not pending_requests:
print("No pending requests found.")
return
print("\n\nPending Requests:")
for req in pending_requests:
print(f"Request ID: {req['id']:<4} "
f"Student: {req['student_name']:<10} "
f"Hours: {req['hours']:<7} \t"
f" Status: {req['status']}")
print("\n")
except Exception as e:
print(f"An error occurred while fetching requests: {e}")
#Command for staff to approve a student's request (staff_id, request_id)
#Once approved it is added to logged hours database
@staff_cli.command("approveRequest", help="Staff approves a student's request")
def approveRequest():
print("\n")
try:
staff_id = int(input("Enter your staff ID: "))
request_id = int(input("Enter the request ID to approve: "))
results = process_request_approval(staff_id,request_id)
req=results['request']
student_name=results['student_name']
staff_name=results['staff_name']
logged=results['logged_hours']
if logged:
print(f"Request {request_id} for {req.hours} hours made by {student_name} approved by Staff {staff_name} (ID: {staff_id}). Logged Hours ID: {logged.id}")
else:
print(f"Request {request_id} for {req.hours} hours made by {student_name} could not be approved (Already Processed).")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
# Command for staff to deny a student's request (staff_id, request_id)
#change request status to denied, no logged hours created
@staff_cli.command("denyRequest", help="Staff denies a student's request")
def denyRequest():
print("\n")
try:
staff_id = int(input("Enter your staff ID: "))
request_id = int(input("Enter the request ID to deny: "))
results = process_request_denial(staff_id,request_id)
req=results['request']
student_name=results['student_name']
staff_name=results['staff_name']
success=results['denial_successful']
if success:
print(f"Request {request_id} for {req.hours} hours made by {student_name} denied by Staff {staff_name} (ID: {staff_id}).")
else:
print(f"Request {request_id} for {req.hours} hours made by {student_name} could not be denied (Already Processed).")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print("\n")
#staff command to view leaderboard of students by approved hours
@staff_cli.command("viewLeaderboard", help="View leaderboard of students by approved hours")
def viewLeaderboard():
print("\n")
try:
leaderboard = generate_leaderboard()
print("Leaderboard (by approved hours):")
if not leaderboard:
print("No students found or hour data found.")
return
for rank, data in enumerate(leaderboard, 1):
print(f"{rank:<6}. {data['name']:<10} ------ \t{data['hours']} hours")
except Exception as e:
print(f"An error occurred while generating the leaderboard: {e}")
print("\n")
app.cli.add_command(staff_cli) # add the group to the cli
# '''
# Test Commands
# '''
# test = AppGroup('test', help='Testing commands')
# @test.command("unit", help="Run User tests")
# def unit_tests_command():
# sys.exit(pytest.main(["-k", "UserUnitTests or StudentUnitTests or StaffUnitTests or RequestUnitTests or LoggedHoursUnitTests"]))
# app.cli.add_command(test)
'''
Test Commands
'''
test = AppGroup('test', help='Testing commands')
@test.command("user", help="Run User tests")
@click.argument("type", default="all")
def user_tests_command(type):
if type == "unit":
sys.exit(pytest.main(["-k", "UserUnitTests or StudentUnitTests or StaffUnitTests or RequestUnitTests or LoggedHoursUnitTests"]))
elif type == "int":
sys.exit(pytest.main(["-k", "UserIntegrationTests or StudentIntegrationTests or StaffIntegrationTests "]))
else:
sys.exit(pytest.main(["-k", "App"]))
app.cli.add_command(test)