-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
552 lines (460 loc) · 19.9 KB
/
Copy pathapp.py
File metadata and controls
552 lines (460 loc) · 19.9 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
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
from flask import Flask, session, render_template, request, redirect, url_for, flash, g
from db_config import get_db_connection
from werkzeug.security import generate_password_hash, check_password_hash
import os
app = Flask(__name__)
app.secret_key = "your_unique_secret_key_here"
# --- DATABASE MANAGEMENT ---
def get_db():
"""Provides a fresh database connection and cursor for the current request."""
if 'db' not in g:
g.db = get_db_connection()
g.cursor = g.db.cursor(dictionary=True, buffered=True)
return g.db, g.cursor
@app.teardown_appcontext
def close_db(error):
"""Automatically closes the database connection after the request is finished."""
db = g.pop('db', None)
if db is not None:
db.close()
# Temporary storage (will be replaced by DB later)
user_data = {}
# --- USER ROUTES ---
# Home
@app.route("/")
def home():
return render_template("index.html", page="home")
# Login
@app.route("/login", methods=["GET", "POST"])
def login():
db, cursor = get_db()
if request.method == "POST":
email = request.form["email"]
password = request.form["password"]
cursor.execute("SELECT * FROM users WHERE email=%s", (email,))
user = cursor.fetchone()
if user and check_password_hash(user["password"], password):
global user_data
user_data = {
"logged_in": True,
"id": user["id"],
"name": user["name"],
"email": user["email"]
}
return redirect(url_for("stream_selection"))
else:
return render_template(
"message.html",
title="Login Failed!",
message="Invalid email or password.",
login_error=True
)
return render_template("login.html")
# Register
@app.route("/register", methods=["GET", "POST"])
def register():
db, cursor = get_db()
if request.method == "POST":
name = request.form["name"]
email = request.form["email"]
password = request.form["password"]
hashed_password = generate_password_hash(password)
cursor.execute("SELECT * FROM users WHERE email=%s", (email,))
existing = cursor.fetchone()
if existing:
return render_template(
"message.html",
title="Registration Failed!",
message="Email already exists.",
duplicate_reg=True
)
cursor.execute(
"INSERT INTO users (name, email, password) VALUES (%s, %s, %s)",
(name, email, hashed_password)
)
db.commit()
return render_template(
"message.html",
title="Registration Successful!",
message=f"Thank you {name}, your account has been created."
)
return render_template("register.html")
# Stream Selection
@app.route("/stream-selection", methods=["GET", "POST"])
def stream_selection():
db, cursor = get_db()
if request.method == "POST":
stream = request.form["stream"]
user_data["stream"] = stream
return redirect(url_for("career_selection"))
cursor.execute("SELECT stream_name FROM streams")
streams = [row["stream_name"] for row in cursor.fetchall()]
return render_template("stream_selection.html", streams=streams, selected_stream=user_data.get("stream"))
# Career Selection
@app.route("/career-selection", methods=["GET", "POST"])
def career_selection():
db, cursor = get_db()
stream = user_data.get("stream")
cursor.execute(
"SELECT career_name FROM careers c JOIN streams s ON c.stream_id = s.id WHERE s.stream_name=%s",
(stream,)
)
careers = [row["career_name"] for row in cursor.fetchall()]
if request.method == "POST":
career = request.form["career"]
user_data["career"] = career
return redirect(url_for("questionnaire"))
return render_template(
"career_selection.html",
careers=careers,
stream=stream,
selected_career=user_data.get("career")
)
# Questionnaire
@app.route("/questionnaire", methods=["GET", "POST"])
def questionnaire():
db, cursor = get_db()
career = user_data.get("career")
cursor.execute(
"SELECT question_text, weight FROM questions q JOIN careers c ON q.career_id=c.id WHERE c.career_name=%s",
(career,)
)
questions = cursor.fetchall()
if request.method == "POST":
score = 0
max_score = sum(q["weight"] for q in questions)
for i, q in enumerate(questions, start=1):
answer = request.form.get(f"q{i}")
if answer == "Yes":
score += q["weight"]
percentage = round((score / max_score) * 100, 2) if max_score > 0 else 0
if percentage >= 70:
suitability = "High"
explanation = f"<b>Excellent match!</b> Your responses strongly align with the requirements for <b>{career}</b>."
elif percentage >= 40:
suitability = "Medium"
explanation = f"<b>Moderate compatibility.</b> You demonstrate some important qualities for <b>{career}</b>."
else:
suitability = "Low"
explanation = f"Currently, your responses indicate <b>limited</b> alignment with <b>{career}</b>."
user_data.update({"score": score, "percentage": percentage, "suitability": suitability, "explanation": explanation})
cursor.execute("SELECT id FROM careers WHERE career_name=%s", (career,))
career_row = cursor.fetchone()
if career_row:
cursor.execute(
"INSERT INTO results (user_id, career_id, score, percentage, suitability) VALUES (%s, %s, %s, %s, %s)",
(user_data["id"], career_row["id"], score, percentage, suitability)
)
db.commit()
return redirect(url_for("result"))
return render_template("questionnaire.html", career=career, questions=questions)
# Result
@app.route("/result")
def result():
return render_template(
"result.html",
career=user_data.get("career"),
percentage=user_data.get("percentage"),
suitability=user_data.get("suitability"),
explanation=user_data.get("explanation")
)
# Results History
@app.route("/results-history")
def results_history():
db, cursor = get_db()
user_id = user_data.get("id")
if not user_id:
return redirect(url_for("login"))
cursor.execute("""
SELECT r.id, c.career_name, r.score, r.percentage, r.suitability, r.taken_at
FROM results r
JOIN careers c ON r.career_id = c.id
WHERE r.user_id = %s
ORDER BY r.taken_at DESC
""", (user_id,))
results = cursor.fetchall()
return render_template("results_history.html", results=results)
@app.context_processor
def inject_user_status():
return dict(
user_logged_in=user_data.get("logged_in", False),
user_name=user_data.get("name", ""),
admin_logged_in=session.get("admin_logged_in", False),
is_admin=session.get("is_admin", False)
)
# Logout
@app.route("/logout")
def logout():
user_data.clear()
return redirect(url_for("home"))
# --- ADMIN ROUTES ---
# Admin
@app.route('/admin')
def admin():
return render_template('admin_login.html')
# Admin Login
@app.route("/admin_login", methods=["GET", "POST"])
def admin_login():
db, cursor = get_db()
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
cursor.execute("SELECT * FROM admin WHERE username=%s AND password=%s", (username, password))
admin = cursor.fetchone()
if admin:
session.update({"user_id": admin["id"], "admin_logged_in": True, "is_admin": True})
return redirect(url_for("admin_dashboard"))
else:
return render_template("message.html", title="Login Failed!", message="Invalid Admin Credentials", admin_error=True)
return render_template("admin_login.html")
# Admin Dashboard
@app.route("/admin_dashboard")
def admin_dashboard():
if not session.get("admin_logged_in") or not session.get("is_admin"):
return redirect(url_for("admin_login"))
return render_template("admin_dashboard.html", admin_logged_in=True)
@app.route('/view_users')
def view_users():
db, cursor = get_db()
query = """
SELECT u.id, u.name, u.email, s.stream_name, c.career_name, r.score, r.percentage, r.taken_at
FROM users u
LEFT JOIN results r ON u.id = r.user_id
LEFT JOIN careers c ON r.career_id = c.id
LEFT JOIN streams s ON c.stream_id = s.id
ORDER BY u.id DESC
"""
cursor.execute(query)
users = cursor.fetchall()
# Format the date for each user
for user in users:
if user['taken_at']:
# Formats date to '10-Mar-2026'
user['formatted_date'] = user['taken_at'].strftime('%d-%b-%Y')
else:
user['formatted_date'] = 'Not Attempted'
return render_template("view_users.html", users=users, admin_logged_in=True)
@app.route('/delete_user/<int:user_id>')
def delete_user(user_id):
db, cursor = get_db()
try:
cursor.execute("DELETE FROM results WHERE user_id = %s", (user_id,))
cursor.execute("DELETE FROM users WHERE id = %s", (user_id,))
db.commit()
flash("User deleted successfully!", "success")
except Exception as e:
db.rollback()
flash(f"Error deleting user: {str(e)}", "danger")
return redirect(url_for('view_users'))
@app.route('/view_questions')
def view_questions():
db, cursor = get_db()
cursor.execute("SELECT * FROM questions")
questions = cursor.fetchall()
return render_template("view_questions.html", questions=questions, admin_logged_in=True)
@app.route('/edit_question/<int:question_id>', methods=['GET', 'POST'])
def edit_question(question_id):
db, cursor = get_db()
cursor.execute("SELECT * FROM questions WHERE id=%s", (question_id,))
q = cursor.fetchone()
if not q:
flash("Question not found!", "danger")
return redirect(url_for('view_questions'))
if request.method == 'POST':
try:
cursor.execute(
"UPDATE questions SET question_text=%s, weight=%s WHERE id=%s",
(request.form['question'], request.form['weight'], question_id)
)
db.commit()
flash("Question updated successfully!", "success")
return redirect(url_for('admin_questions', career_id=q['career_id']))
except Exception as e:
db.rollback()
flash(f"Error updating question: {str(e)}", "danger")
return render_template('edit_question.html', q=q, back_url=url_for('admin_questions', career_id=q['career_id']))
@app.route('/add_question/<int:career_id>', methods=['GET','POST'])
def add_question(career_id):
db, cursor = get_db()
if request.method == 'POST':
try:
cursor.execute(
"INSERT INTO questions(question_text, weight, career_id) VALUES(%s,%s,%s)",
(request.form['question'], request.form['weight'], career_id)
)
db.commit()
flash("Question added successfully!", "success")
return redirect(url_for('admin_questions', career_id=career_id))
except Exception as e:
db.rollback()
flash(f"Error adding question: {str(e)}", "danger")
return render_template("add_question.html", career_id=career_id, back_url=url_for('admin_questions', career_id=career_id), admin_logged_in=True)
@app.route("/delete_question/<int:id>")
def delete_question(id):
db, cursor = get_db()
# 1. Fetch the career_id before deleting so we know where to redirect back to
cursor.execute("SELECT career_id FROM questions WHERE id=%s", (id,))
question = cursor.fetchone()
if question:
career_id = question['career_id']
# 2. Perform the deletion
try:
cursor.execute("DELETE FROM questions WHERE id=%s", (id,))
db.commit()
flash("Question deleted successfully!", "success")
# 3. Redirect back to the specific career's question list
return redirect(url_for("admin_questions", career_id=career_id))
except Exception as e:
db.rollback()
flash(f"Error deleting question: {str(e)}", "danger")
return redirect(url_for("admin_questions", career_id=career_id))
return redirect(url_for("admin_streams"))
@app.route("/admin_logout")
def admin_logout():
session.clear()
return redirect(url_for("admin_login"))
# Manage Questions
@app.route('/admin_streams')
def admin_streams():
db, cursor = get_db()
cursor.execute("SELECT * FROM streams")
return render_template("admin_streams.html", streams=cursor.fetchall(), admin_logged_in=True)
@app.route('/admin_careers/<int:stream_id>')
def admin_careers(stream_id):
db, cursor = get_db()
cursor.execute("SELECT * FROM careers WHERE stream_id=%s", (stream_id,))
return render_template("admin_careers.html", careers=cursor.fetchall(), admin_logged_in=True)
@app.route('/admin_questions/<int:career_id>')
def admin_questions(career_id):
db, cursor = get_db()
cursor.execute("SELECT * FROM questions WHERE career_id=%s", (career_id,))
return render_template("view_questions.html", questions=cursor.fetchall(), career_id=career_id, admin_logged_in=True)
# Manage Streams
@app.route('/admin_manage_streams')
def admin_manage_streams():
if not session.get("admin_logged_in"):
return redirect(url_for("admin_login"))
db, cursor = get_db()
cursor.execute("SELECT * FROM streams")
streams = cursor.fetchall()
return render_template("admin_manage_streams.html", streams=streams, admin_logged_in=True)
@app.route('/add_stream', methods=['POST'])
def add_stream():
db, cursor = get_db()
stream_name = request.form.get('stream_name')
if stream_name:
try:
cursor.execute("INSERT INTO streams (stream_name) VALUES (%s)", (stream_name,))
db.commit()
flash(f"Stream '{stream_name}' added successfully!", "success")
except Exception as e:
db.rollback()
flash("Error: Stream might already exist.", "danger")
return redirect(url_for('admin_manage_streams'))
@app.route('/edit_stream/<int:stream_id>', methods=['GET', 'POST'])
def edit_stream(stream_id):
"""Allows admin to rename a stream."""
if not session.get("admin_logged_in"):
return redirect(url_for("admin_login"))
db, cursor = get_db()
if request.method == 'POST':
new_name = request.form.get('stream_name')
if new_name:
try:
cursor.execute("UPDATE streams SET stream_name = %s WHERE id = %s", (new_name, stream_id))
db.commit()
flash(f"Stream updated to '{new_name}'", "success")
return redirect(url_for('admin_manage_streams'))
except Exception as e:
db.rollback()
flash("Error: Stream name might already exist.", "danger")
# GET: Fetch the current stream data to populate the form
cursor.execute("SELECT * FROM streams WHERE id = %s", (stream_id,))
stream = cursor.fetchone()
if not stream:
flash("Stream not found.", "danger")
return redirect(url_for('admin_manage_streams'))
return render_template("edit_stream.html", stream=stream)
@app.route('/delete_stream/<int:stream_id>')
def delete_stream(stream_id):
db, cursor = get_db()
try:
cursor.execute("DELETE FROM streams WHERE id = %s", (stream_id,))
db.commit()
flash("Stream deleted successfully!", "success")
except Exception as e:
db.rollback()
flash(f"Could not delete stream: {str(e)}", "danger")
return redirect(url_for('admin_manage_streams'))
# Manage Careers
@app.route('/admin_manage_careers_select')
def admin_manage_careers_select():
"""Step 1: Admin selects which stream's careers they want to manage."""
if not session.get("admin_logged_in"):
return redirect(url_for("admin_login"))
db, cursor = get_db()
cursor.execute("SELECT * FROM streams")
streams = cursor.fetchall()
return render_template("admin_manage_careers_select.html", streams=streams, admin_logged_in=True)
@app.route('/admin_manage_careers_list/<int:stream_id>')
def admin_manage_careers_list(stream_id):
if not session.get("admin_logged_in"):
return redirect(url_for("admin_login"))
db, cursor = get_db()
cursor.execute("SELECT * FROM streams WHERE id = %s", (stream_id,))
stream = cursor.fetchone()
cursor.execute("SELECT * FROM careers WHERE stream_id = %s", (stream_id,))
return render_template("admin_manage_careers_list.html", stream=stream, careers=cursor.fetchall(), admin_logged_in=True)
@app.route('/add_career/<int:stream_id>', methods=['POST'])
def add_career(stream_id):
"""Action: Adds a new career to a specific stream."""
db, cursor = get_db()
name = request.form.get('career_name')
if name:
try:
cursor.execute("INSERT INTO careers (career_name, stream_id) VALUES (%s, %s)", (name, stream_id))
db.commit()
flash(f"Career '{name}' added successfully!", "success")
except Exception as e:
db.rollback()
flash("Error adding career. It might already exist.", "danger")
return redirect(url_for('admin_manage_careers_list', stream_id=stream_id))
@app.route('/edit_career/<int:career_id>', methods=['GET', 'POST'])
def edit_career(career_id):
"""Allows admin to rename a specific career."""
if not session.get("admin_logged_in"):
return redirect(url_for("admin_login"))
db, cursor = get_db()
# First, fetch the current career to know its name and which stream it belongs to
cursor.execute("SELECT * FROM careers WHERE id = %s", (career_id,))
career = cursor.fetchone()
if not career:
flash("Career not found.", "danger")
return redirect(url_for('admin_manage_careers_select'))
if request.method == 'POST':
new_name = request.form.get('career_name')
if new_name:
try:
cursor.execute("UPDATE careers SET career_name = %s WHERE id = %s", (new_name, career_id))
db.commit()
flash(f"Career updated to '{new_name}'", "success")
# Redirect back to the list of careers for that specific stream
return redirect(url_for('admin_manage_careers_list', stream_id=career['stream_id']))
except Exception as e:
db.rollback()
flash("Error updating career. Name might be a duplicate.", "danger")
return render_template("edit_career.html", career=career)
@app.route('/delete_career/<int:career_id>/<int:stream_id>')
def delete_career(career_id, stream_id):
"""Action: Deletes a career and stays on the same stream list."""
db, cursor = get_db()
try:
cursor.execute("DELETE FROM careers WHERE id = %s", (career_id,))
db.commit()
flash("Career deleted successfully!", "success")
except Exception as e:
db.rollback()
flash("Error deleting career.", "danger")
return redirect(url_for('admin_manage_careers_list', stream_id=stream_id))
if __name__ == "__main__":
app.run(debug=True)