forked from Quantizate/DBMS_frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
1363 lines (1194 loc) · 44.9 KB
/
app.py
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, redirect, session, url_for, jsonify
from flask_mysqldb import MySQL
import MySQLdb.cursors
from flask import redirect
import os
from flask_mail import Mail, Message
from authlib.integrations.flask_client import OAuth
from dotenv import load_dotenv
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
load_dotenv()
app = Flask(__name__)
oauth = OAuth(app)
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["200 per day", "50 per hour"],
storage_uri="memory://",
)
# Configure MySQL
app.secret_key = "abcd2123445"
app.config["MYSQL_HOST"] = os.getenv("MYSQL_HOST")
app.config["MYSQL_PORT"] = os.getenv("MYSQL_PORT")
app.config["MYSQL_USER"] = os.getenv("MYSQL_USER")
app.config["MYSQL_PASSWORD"] = os.getenv("MYSQL_PASSWORD")
app.config["MYSQL_DB"] = "lab_bookings"
app.config["MAIL_SERVER"] = os.getenv("MAIL_SERVER")
app.config["MAIL_PORT"] = os.getenv("MAIL_PORT")
app.config["MAIL_USERNAME"] = os.getenv("MAIL_USERNAME")
app.config["MAIL_PASSWORD"] = os.getenv("MAIL_PASSWORD")
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = True
mail = Mail(app)
mysql = MySQL(app)
@app.route("/")
def index():
session.clear()
return render_template("index.html")
@app.route("/submit", methods=["POST"])
def submit():
if "loggedin" not in session:
return redirect(url_for("login"))
if request.method == "POST":
details = request.form
email = session.get("email") # Retrieve email from session
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM students WHERE Email_ID = %s", (email,))
user = cur.fetchone()
if user is None:
cur.execute("SELECT * FROM professor WHERE Email_ID = %s", (email,))
user = cur.fetchone()
if user is None:
cur.execute("SELECT * FROM staff WHERE Email_ID = %s", (email,))
user = cur.fetchone()
if user:
name = user[1]
# Check if the form is for lab booking or equipment issuing
if "time_slot" in details:
# Lab booking form data
lab_name = details["Lab_Name"]
time_slot = details["time_slot"]
date = details["date"]
cur = mysql.connection.cursor()
cur.execute(
"SELECT * FROM bookings WHERE lab_name = %s AND date = %s AND time_slot = %s",
(lab_name, date, time_slot),
)
existing_booking = cur.fetchone()
if existing_booking:
return redirect(url_for("booking_lab"))
else:
cur.execute(
"INSERT INTO bookings (user_email, name, lab_name, time_slot, date) VALUES (%s, %s, %s, %s, %s)",
(email, name, lab_name, time_slot, date),
)
mysql.connection.commit()
cur.close()
msg = Message(
subject="Booking Confirmed!",
sender="nitishkarnik@iitgn.ac.in",
recipients=[email],
)
msg.body = f"Hello, Your Booking has been confirmed . Here are the details: \n\nDate: {date} \nLab: {lab_name} \nTime Slot: {time_slot} \nThank you!"
mail.send(msg)
elif "Equipment_Name" in details:
# Equipment issuing form data
# print(details)
equipment_name = details["Equipment_Name"]
equipment_id = details["ID"]
price = details["Price"]
vendor_address = details["Vendor_Address"]
vendor_phone_number = details["Vendor_Phone_Number"]
manufacturer_name = details["Manufacturer_Name"]
status = details["isAvailable"]
if status == "Available":
status = 1
else:
status = 0
lab_name = details["Lab_Name"]
cur = mysql.connection.cursor()
cur.execute(
"INSERT INTO inventory ( ID,Equipment_Name, Price, Vendor_Address, Vendor_Phone_Number, Manufacturer_Name, isAvailable , Lab_Name ) VALUES (%s,%s, %s, %s, %s, %s, %s,%s)",
(
equipment_id,
equipment_name,
price,
vendor_address,
vendor_phone_number,
manufacturer_name,
status,
lab_name,
),
)
mysql.connection.commit()
cur.close()
elif "Enrolled_Course_ID" in details:
# request.form.getlist('Enrolled_Course_ID')
enrolled_course_ids = request.form.getlist("Enrolled_Course_ID")
cur = mysql.connection.cursor()
cur.execute(
"SELECT Roll_Number FROM students WHERE Email_ID = %s", (email,)
)
roll_no = cur.fetchone()[0]
for enrolled_course in enrolled_course_ids:
print(enrolled_course[2:-3])
cur.execute(
"SELECT * FROM student_enrolled WHERE Course_Id = %s AND Roll_Number = %s",
(enrolled_course[2:-3], roll_no),
)
user = cur.fetchone()
if user:
continue
cur.execute(
"INSERT INTO student_enrolled (Course_Id, Roll_Number) VALUES (%s, %s)",
(enrolled_course[2:-3], roll_no),
)
mysql.connection.commit()
cur.close()
elif "Course_Name" in details:
# print("Hello")
course_name = details["Course_Name"]
course_id = details["Course_ID"]
credits = details["Credits"]
cur = mysql.connection.cursor()
cur.execute("select * from professor where Email_ID = %s", (email,))
user = cur.fetchone()
employee_id = user[0]
cur.execute(
"INSERT INTO course (Course_ID, Course_Name, Credits) VALUES (%s, %s, %s)",
(course_id, course_name, credits),
)
mysql.connection.commit()
cur.execute(
"INSERT INTO instructor(Course_ID, Employee_ID) VALUES (%s, %s)",
(course_id, employee_id),
)
# cur.execute("INSERT INTO course (email,Course_ID, Course_Name, Credits) VALUES (%s,%s, %s, %s)", (email,course_id,course_name, credits))
mysql.connection.commit()
cur.close()
elif "Donating_Organization" in details:
id = details["ID"]
lab_name = details["Lab_Name"]
donor = details["Donor"]
donating_organization = details["Donating_Organization"]
amount = details["Amount"]
receiving_date = details["Receiving_Date"]
cur = mysql.connection.cursor()
cur.execute(
"INSERT INTO grants (ID, Lab_Name, Donor, Donating_Organization, Amount, Receiving_Date) VALUES (%s, %s, %s, %s, %s, %s)",
(id, lab_name, donor, donating_organization, amount, receiving_date),
)
mysql.connection.commit()
cur.close()
else:
# print(details)
# Equipment issuing form data
if "issueDate" in details:
equipmentID = details["ID"]
# number_of_equipment = details['numberOfEquipment']
issue_date = details["issueDate"]
return_date = details["returnDate"]
# return_date = details['returnDate']
cur = mysql.connection.cursor()
cur.execute("select * from students where Email_ID = %s", (email,))
user = cur.fetchone()
if user is not None:
roll_no = user[0]
else:
redirect(url_for("404"))
cur.execute(
"SELECT ID,isAvailable FROM inventory WHERE ID = %s", (equipmentID,)
)
existing_id = cur.fetchone()
if not existing_id:
return redirect(url_for("booking_lab"))
elif existing_id[1] == 0:
return redirect(url_for("booking_lab"))
cur.execute(
"INSERT INTO accessed_tool (Roll_Number, ID, Issued_date,Return_Date) VALUES (%s, %s, %s,%s)",
(roll_no, equipmentID, issue_date, return_date),
)
mysql.connection.commit()
cur.execute(
"UPDATE inventory SET isAvailable = 0 WHERE ID = %s", (equipmentID,)
)
mysql.connection.commit()
cur.close()
msg = Message(
subject="Equipment Issuing Confirmed!",
sender="nitishkarnik@iitgn.ac.in",
recipients=[email],
)
msg.body = f"Hello, Your equipment has been booked. Here are the details: \n\n Roll Number:{roll_no} \nID: {equipmentID} \nIssue Date : {issue_date} \nReturn Date: {return_date} \nThank you!"
mail.send(msg)
else:
equipmentID = details["equipment_id"]
cur = mysql.connection.cursor()
cur.execute("select * from students where Email_ID = %s", (email,))
user = cur.fetchone()
if user is not None:
roll_no = user[0]
else:
redirect(url_for("404"))
cur.execute(
"DELETE from accessed_tool where Roll_Number = %s AND ID = %s",
(roll_no, equipmentID),
)
mysql.connection.commit()
cur.execute(
"UPDATE inventory SET isAvailable = 1 WHERE ID = %s", (equipmentID,)
)
mysql.connection.commit()
cur.close()
lab_bookings = fetch_lab_bookings(email)
equipment_issued = fetch_equipment_issued(email)
if type(equipment_issued) == type(None):
equipment_issued = []
courses = fetch_courses()
role = fetch_role(email)
equipment_details_list = fetch_equip_details_list(equipment_issued)
return render_template(
"submit.html",
equipment_issued=equipment_issued,
lab_bookings=lab_bookings,
courses=courses,
role=role,
equipment_details_list=equipment_details_list,
)
@app.route("/profile")
def profile():
if "loggedin" not in session:
return redirect(url_for("login"))
email = session.get("email")
lab_bookings = fetch_lab_bookings(email)
equipment_issued = fetch_equipment_issued(email)
if type(equipment_issued) == type(None):
equipment_issued = []
role = fetch_role(email)
if role != "lab":
name = fetch_name(email)
if role == "student":
courses = fetch_student_courses(email)
equipment_details_list = fetch_equip_details_list(equipment_issued)
return render_template(
"profile.html",
equipment_issued=equipment_issued,
lab_bookings=lab_bookings,
courses=courses,
name=name,
role=role,
equipment_details_list=equipment_details_list,
profile_pic=(
session.get("profile_pic") if session.get("profile_pic") else None
),
)
elif role == "professor":
courses = fetch_prof_course(email)
return render_template(
"profile.html",
lab_bookings=lab_bookings,
courses=courses,
name=name,
equipment_issued=[],
role=role,
profile_pic=(
session.get("profile_pic") if session.get("profile_pic") else None
),
)
else:
return render_template(
"profile.html",
name=name,
role=role,
profile_pic=(
session.get("profile_pic") if session.get("profile_pic") else None
),
)
else:
grants, inventory = fetch_grant_inventory(email)
return render_template(
"profile.html", role=role, grants=grants, inventory=inventory
)
@app.route("/login", methods=["GET", "POST"])
def login():
session.clear()
if request.method == "POST":
email = request.form["email"]
password = request.form["password"]
if (email == "admin@gmail.com") and (password == "admin"):
session["loggedin"] = True
session["email"] = email
session["password"] = password
return redirect(url_for("table"))
# session_email=email
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
# Fetch user from database
# cur = mysql.connection.cursor()
cursor.execute(
"SELECT * FROM students WHERE Email_ID = %s AND password = %s",
(email, password),
)
user = cursor.fetchone()
if user is None:
cursor.execute(
"SELECT * FROM professor WHERE Email_ID = %s AND password = %s",
(email, password),
)
user = cursor.fetchone()
if user is None:
cursor.execute(
"SELECT * FROM staff WHERE Email_ID = %s AND password = %s",
(email, password),
)
user = cursor.fetchone()
if user:
session["loggedin"] = True
session["email"] = user["Email_ID"]
return redirect(url_for("booking_lab"))
# cursor.close()
# if user:
# # Redirect to booking page if login is successful
# return render_template('/bookinglab.html')
else:
# Redirect to login page with error message if login fails
print(email)
cursor.execute("SELECT * FROM lab WHERE Email_ID = %s", (email,))
user = cursor.fetchone()
if user:
session["loggedin"] = True
session["email"] = user["Email_ID"]
return redirect(url_for("labpage"))
else:
return render_template("login.html", error="Invalid email or password")
if request.method == "GET":
return render_template("login.html")
return render_template("login.html")
@app.route("/labpage")
def labpage():
if "loggedin" in session:
role = fetch_role(session.get("email"))
return render_template("labpage.html", role=role)
return redirect(url_for("login"))
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
email = request.form["reg_email"]
password = request.form["reg_password"]
role = request.form["role"]
first_name = request.form["first name"]
last_name = request.form["last name"]
middle_name = request.form["middle name"]
roll_no = request.form["roll number"]
lab_name = request.form["Lab name"]
# print(role)
# Insert new user into database
cur = mysql.connection.cursor()
if role == "student":
cur.execute("SELECT * FROM students WHERE Email_ID = %s", (email,))
if cur.fetchone():
return redirect(url_for("register"))
try:
cur.execute(
"INSERT INTO students (Roll_Number, First_Name, Middle_Name, Last_Name, Email_ID, password) VALUES (%s, %s, %s,%s,%s,%s)",
(roll_no, first_name, middle_name, last_name, email, password),
)
except Exception as err:
return redirect(url_for("register"))
# cur.execute("INSERT INTO students (Email_ID, First_Name,password) VALUES (%s, %s, %s,%s)", (email, name,password,role))
elif role == "professor":
cur.execute("SELECT * FROM professor WHERE Email_ID = %s", (email,))
if cur.fetchone():
return redirect(url_for("register"))
try:
cur.execute(
"INSERT INTO professor (Employee_ID,Email_ID, First_Name, Middle_Name, Last_Name, password) VALUES (%s,%s, %s, %s,%s,%s)",
(roll_no, email, first_name, middle_name, last_name, password),
)
except:
return redirect(url_for("register"))
# cur.execute("INSERT INTO professors (Email_ID, First_Name,password) VALUES (%s, %s, %s,%s)", (email, name,password,role))
elif role == "staff":
cur.execute("SELECT * FROM lab WHERE Lab_Name = %s", (lab_name,))
lab = cur.fetchone()
if lab is None:
return redirect("127.0.0.1:5000/404")
cur.execute("SELECT * FROM staff WHERE Email_ID = %s", (email,))
if cur.fetchone():
return redirect(url_for("register"))
try:
cur.execute(
"INSERT INTO staff (Employee_ID,Email_ID, First_Name, Middle_Name, Last_Name, password,Lab_Name) VALUES (%s,%s, %s, %s,%s,%s,%s)",
(
roll_no,
email,
first_name,
middle_name,
last_name,
password,
lab_name,
),
)
except:
return redirect(url_for("register"))
# cur.execute("INSERT INTO staff (email, name,password) VALUES (%s, %s, %s,%s)", (email, name,password,role))
mysql.connection.commit()
cur.close()
# Redirect to login page after successful registration
return redirect("/login")
if request.method == "GET":
return render_template("register.html")
@app.route("/register/oauth", methods=["POST"])
def register_oauth():
email = session.get("email")
role = request.form["role"]
first_name = session.get("first_name")
last_name = session.get("last_name")
middle_name = ""
roll_no = request.form["roll number"]
lab_name = request.form["Lab name"]
password = ""
# print(role)
# Insert new user into database
cur = mysql.connection.cursor()
if role == "student":
cur.execute("SELECT * FROM students WHERE Email_ID = %s", (email,))
if cur.fetchone():
return redirect(url_for("register"))
try:
cur.execute(
"INSERT INTO students (Roll_Number, First_Name, Middle_Name, Last_Name, Email_ID, password) VALUES (%s, %s, %s,%s,%s,%s)",
(roll_no, first_name, middle_name, last_name, email, password),
)
except Exception as err:
return redirect(url_for("register"))
# cur.execute("INSERT INTO students (Email_ID, First_Name,password) VALUES (%s, %s, %s,%s)", (email, name,password,role))
elif role == "professor":
cur.execute("SELECT * FROM professor WHERE Email_ID = %s", (email,))
if cur.fetchone():
return redirect(url_for("register"))
try:
cur.execute(
"INSERT INTO professor (Employee_ID,Email_ID, First_Name, Middle_Name, Last_Name, password) VALUES (%s,%s, %s, %s,%s,%s)",
(roll_no, email, first_name, middle_name, last_name, password),
)
except:
return redirect(url_for("register"))
# cur.execute("INSERT INTO professors (Email_ID, First_Name,password) VALUES (%s, %s, %s,%s)", (email, name,password,role))
elif role == "staff":
cur.execute("SELECT * FROM lab WHERE Lab_Name = %s", (lab_name,))
lab = cur.fetchone()
if lab is None:
return redirect("127.0.0.1:5000/404")
cur.execute("SELECT * FROM staff WHERE Email_ID = %s", (email,))
if cur.fetchone():
return redirect(url_for("register"))
try:
cur.execute(
"INSERT INTO staff (Employee_ID,Email_ID, First_Name, Middle_Name, Last_Name, password,Lab_Name) VALUES (%s,%s, %s, %s,%s,%s,%s)",
(
roll_no,
email,
first_name,
middle_name,
last_name,
password,
lab_name,
),
)
except:
return redirect(url_for("register"))
# cur.execute("INSERT INTO staff (email, name,password) VALUES (%s, %s, %s,%s)", (email, name,password,role))
mysql.connection.commit()
cur.close()
# Redirect to login page after successful registration
return redirect("/login")
@app.route("/bookinglab")
def booking_lab():
if "loggedin" in session:
role = fetch_role(session.get("email"))
courses = fetch_courses()
# print(role)
equipment_issued = fetch_equipment_issued(session.get("email"))
return render_template(
"bookinglab.html",
role=role,
courses=courses,
equipment_issued=equipment_issued,
)
return redirect(url_for("login"))
@app.route("/admintables/bookings", methods=["GET", "POST"])
def bookings():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
(
bookings,
equipment_issued,
courses,
inventory,
student_enrolled,
accessed_tool,
course_slot,
) = fetch_all()
columns = get_column_names("bookings")
# print(columns)
return render_template(
"/admintables/bookings.html",
bookings=bookings,
equipment_issued=equipment_issued,
courses=courses,
inventory=inventory,
columns=columns,
)
@app.route("/admintables/inventory", methods=["GET", "POST"])
def inventory():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
if (
"loggedin" not in session
or session.get("email") != "admin@gmail.com"
or session.get("password") != "admin"
):
return redirect(url_for("login"))
(
bookings,
equipment_issued,
courses,
inventory,
student_enrolled,
accessed_tool,
course_slot,
) = fetch_all()
columns = get_column_names("inventory")
# print(columns)
return render_template(
"/admintables/inventory.html",
bookings=bookings,
equipment_issued=equipment_issued,
courses=courses,
inventory=inventory,
columns=columns,
)
@app.route("/admintables/course", methods=["GET", "POST"])
def course():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
(
bookings,
equipment_issued,
courses,
inventory,
student_enrolled,
accessed_tool,
course_slot,
) = fetch_all()
columns = get_column_names("course")
return render_template(
"/admintables/course.html",
bookings=bookings,
equipment_issued=equipment_issued,
courses=courses,
inventory=inventory,
columns=columns,
)
@app.route("/admintables/student_enrolled", methods=["GET", "POST"])
def student_enrolled():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
(
bookings,
equipment_issued,
courses,
inventory,
student_enrolled,
accessed_tool,
course_slot,
) = fetch_all()
columns = get_column_names("student_enrolled")
return render_template(
"/admintables/student_enrolled.html",
bookings=bookings,
equipment_issued=equipment_issued,
courses=courses,
inventory=inventory,
student_enrolled=student_enrolled,
columns=columns,
)
@app.route("/admintables/accessed_tool", methods=["GET", "POST"])
def accessed_tool():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
(
bookings,
equipment_issued,
courses,
inventory,
student_enrolled,
accessed_tool,
course_slot,
) = fetch_all()
columns = get_column_names("accessed_tool")
return render_template(
"/admintables/accessed_tool.html",
bookings=bookings,
equipment_issued=equipment_issued,
courses=courses,
inventory=inventory,
student_enrolled=student_enrolled,
accessed_tool=accessed_tool,
columns=columns,
)
@app.route("/admintables/course_slot", methods=["GET", "POST"])
def course_slot():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
(
bookings,
equipment_issued,
courses,
inventory,
student_enrolled,
accessed_tool,
course_slot,
) = fetch_all()
columns = get_column_names("course_slot")
return render_template(
"/admintables/course_slot.html",
bookings=bookings,
equipment_issued=equipment_issued,
courses=courses,
inventory=inventory,
student_enrolled=student_enrolled,
accessed_tool=accessed_tool,
columns=columns,
course_slot=course_slot,
)
@app.route("/admintables/grants", methods=["GET", "POST"])
def grants():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
grants = fetch_all_grants()
columns = get_column_names("grants")
return render_template("/admintables/grants.html", grants=grants, columns=columns)
def fetch_all_grants():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM grants")
grants = cur.fetchall() # Fetch all rows
return grants
def fetch_all_instructor():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM instructor")
instructor = cur.fetchall()
return instructor
@app.route("/admintables/instructor", methods=["GET", "POST"])
def instructor():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
instructor = fetch_all_instructor()
columns = get_column_names("instructor")
return render_template(
"/admintables/instructor.html", instructor=instructor, columns=columns
)
def fetch_all_lab_grant():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM lab_grant")
lab_grants = cur.fetchall() # Fetch all rows
return lab_grants
@app.route("/admintables/lab_grant", methods=["GET", "POST"])
def lab_grants():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
lab_grant = fetch_all_lab_grant()
columns = get_column_names("lab_grant")
return render_template(
"/admintables/lab_grant.html", lab_grant=lab_grant, columns=columns
)
def get_all_prof_department():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM prof_department")
prof_department = cur.fetchall() # Fetch all rows
return prof_department
@app.route("/admintables/prof_department", methods=["GET", "POST"])
def prof_department():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
prof_department = get_all_prof_department()
columns = get_column_names("prof_department")
return render_template(
"/admintables/prof_department.html",
prof_department=prof_department,
columns=columns,
)
def get_all_professor():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM professor")
professor = cur.fetchall() # Fetch all rows
return professor
@app.route("/admintables/professor", methods=["GET", "POST"])
def professor():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
professor = get_all_professor()
columns = get_column_names("professor")
return render_template(
"/admintables/professor.html", professor=professor, columns=columns
)
def get_all_project():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM project")
project = cur.fetchall() # Fetch all rows
return project
@app.route("/admintables/project", methods=["GET", "POST"])
def project():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
project = get_all_project()
columns = get_column_names("project")
return render_template(
"/admintables/project.html", project=project, columns=columns
)
def get_all_staff():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM staff")
staff = cur.fetchall() # Fetch all rows
return staff
@app.route("/admintables/staff", methods=["GET", "POST"])
def staff():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
staff = get_all_staff()
columns = get_column_names("staff")
return render_template("/admintables/staff.html", staff=staff, columns=columns)
def get_all_students():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM students")
students = cur.fetchall() # Fetch all rows
return students
@app.route("/admintables/students", methods=["GET", "POST"])
def students():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
students = get_all_students()
columns = get_column_names("students")
return render_template(
"/admintables/students.html", students=students, columns=columns
)
def get_all_time_slot():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM time_slot")
time_slot = cur.fetchall() # Fetch all rows
return time_slot
@app.route("/admintables/time_slot", methods=["GET", "POST"])
def time_slot():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
time_slot = get_all_time_slot()
columns = get_column_names("time_slot")
return render_template(
"/admintables/time_slot.html", time_slot=time_slot, columns=columns
)
def get_all_lab():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM lab")
lab = cur.fetchall() # Fetch all rows
return lab
@app.route("/admintables/lab", methods=["GET", "POST"])
def lab():
chech_admin = auth_admin()
if chech_admin == False:
return redirect(url_for("login"))
lab = get_all_lab()
columns = get_column_names("lab")
return render_template("/admintables/lab.html", lab=lab, columns=columns)
@app.route("/submitadmin", methods=["GET", "POST"])
def submitadmin():
if request.method == "POST":
details = request.form
email = session.get("email")
# print(details)
if details["button"] == "insert":
# try:
table_name = details["table"]
columns = get_column_names(table_name)
values = []
for column in columns:
values.append(details[column])
cur = mysql.connection.cursor()
cur.execute(
f"INSERT INTO {table_name} ({','.join(columns)}) VALUES ({','.join(['%s']*len(columns))})",
values,
)
mysql.connection.commit()
cur.close()
# Handling errors
# except Exception as err:
# return render_template("errorquery.html", error=e)
elif details["button"] == "select":
try:
table_name = details["table"]
condition = details["Where"]
cur = mysql.connection.cursor()
cur.execute(f"SELECT * FROM {table_name} WHERE {condition}")
result = cur.fetchall()
cur.close()
column_names = get_column_names(table_name)
# Capitalize the first letter of each column name and replace _ with space
column_names = [
column.replace("_", " ").capitalize() for column in column_names
]
length = len(column_names)
# List having numbers from 0 to length-1
list_length = [i for i in range(length)]
# print(result)
return render_template(
"/admintables/selectresult.html",
result=result,
column_names=column_names,
list_length=list_length,
)
# Handling errors
except Exception as err:
return render_template("errorquery.html", error=err)
elif details["button"] == "delete":
try:
table_name = details["table"]
condition = details["Where"]
cur = mysql.connection.cursor()
cur.execute(f"DELETE FROM {table_name} WHERE {condition}")
mysql.connection.commit()
cur.close()
# Handling errors
except Exception as err:
return render_template("errorquery.html", error=err)
elif details["button"] == "update":
try:
table_name = details["table"]
condition = details["where"]
cur = mysql.connection.cursor()
cur.execute(
f"UPDATE {table_name} SET {details['set']} WHERE {condition}"
)
mysql.connection.commit()
cur.close()
# Handling errors
except Exception as err:
return render_template("errorquery.html", error=err)
elif details["button"] == "rename":
try:
table_name = details["table"]
columns = get_column_names(table_name)
cur = mysql.connection.cursor()
cur.execute(
f"ALTER TABLE {table_name} RENAME TO {details['new_table']}"
)
mysql.connection.commit()
cur.close()
# Handling errors
except Exception as err: