This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
706 lines (563 loc) · 24.5 KB
/
Copy pathapp.py
File metadata and controls
706 lines (563 loc) · 24.5 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
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
from flask import Flask, render_template, request, redirect, session, flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
app = Flask(__name__)
app.secret_key = 'RADHAKRISHNA'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///quizmaster.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
full_name = db.Column(db.String(100))
qualification = db.Column(db.String(100))
dob = db.Column(db.String(20))
is_admin = db.Column(db.Boolean, default=False)
scores = db.relationship('Score', backref='user', lazy=True)
class Subject(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
chapters = db.relationship('Chapter', backref='subject', lazy=True, cascade="all, delete-orphan")
class Chapter(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
subject_id = db.Column(db.Integer, db.ForeignKey('subject.id'), nullable=False)
quizzes = db.relationship('Quiz', backref='chapter', lazy=True, cascade="all, delete-orphan")
class Quiz(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_of_quiz = db.Column(db.String(20))
time_duration = db.Column(db.String(10))
remarks = db.Column(db.Text)
chapter_id = db.Column(db.Integer, db.ForeignKey('chapter.id'), nullable=False)
questions = db.relationship('Question', backref='quiz', lazy=True, cascade="all, delete-orphan")
scores = db.relationship('Score', backref='quiz', lazy=True, cascade="all, delete-orphan")
class Question(db.Model):
id = db.Column(db.Integer, primary_key=True)
question_statement = db.Column(db.Text, nullable=False)
option1 = db.Column(db.Text, nullable=False)
option2 = db.Column(db.Text, nullable=False)
option3 = db.Column(db.Text)
option4 = db.Column(db.Text)
correct_option = db.Column(db.Integer, nullable=False) # 1-4
quiz_id = db.Column(db.Integer, db.ForeignKey('quiz.id'), nullable=False)
class Score(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
quiz_id = db.Column(db.Integer, db.ForeignKey('quiz.id'), nullable=False)
score = db.Column(db.Integer, nullable=False)
total_questions = db.Column(db.Integer, nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
def is_logged_in():
return 'user_id' in session
def is_admin():
return is_logged_in() and User.query.get(session['user_id']).is_admin
def create_admin():
if not User.query.filter_by(is_admin=True).first():
admin = User(
username='admin',
password=generate_password_hash('0000'),
full_name='Admin',
is_admin=True
)
db.session.add(admin)
db.session.commit()
@app.route('/')
def home():
if is_logged_in():
if is_admin():
return redirect('/admin/dashboard')
return redirect('/user/dashboard')
return render_template('home.html')
# Authentication
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password, password):
session['user_id'] = user.id
session['is_admin'] = user.is_admin
flash('Login successful', 'success')
if user.is_admin:
return redirect('/admin/dashboard')
return redirect('/user/dashboard')
flash('Invalid credentials', 'danger')
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
full_name = request.form['full_name']
if User.query.filter_by(username=username).first():
flash('Username already exists', 'danger')
return redirect('/register')
new_user = User(
username=username,
password=generate_password_hash(password),
full_name=full_name,
qualification=request.form.get('qualification'),
dob=request.form.get('dob')
)
db.session.add(new_user)
db.session.commit()
flash('Registration successful. Please login.', 'success')
return redirect('/login')
return render_template('register.html')
@app.route('/logout')
def logout():
session.clear()
flash('You have been logged out', 'info')
return redirect('/')
@app.route('/admin/subject/add', methods=['POST'])
def add_subject():
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
name = request.form['name']
description = request.form['description']
new_subject = Subject(name=name, description=description)
db.session.add(new_subject)
db.session.commit()
flash('Subject added successfully', 'success')
return redirect('/admin/dashboard')
@app.route('/admin/subject/<int:subject_id>/chapter/add', methods=['POST'])
def add_chapter(subject_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
name = request.form['name']
description = request.form['description']
new_chapter = Chapter(
name=name,
description=description,
subject_id=subject_id
)
db.session.add(new_chapter)
db.session.commit()
flash('Chapter added successfully', 'success')
return redirect(f'/admin/subject/{subject_id}')
@app.route('/admin/chapter/<int:chapter_id>/quiz/add', methods=['POST'])
def add_quiz(chapter_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
title = request.form['title']
date_of_quiz = request.form['date_of_quiz']
time_duration = request.form['time_duration']
remarks = request.form.get('remarks', '')
new_quiz = Quiz(
title=title,
date_of_quiz=date_of_quiz,
time_duration=time_duration,
remarks=remarks,
chapter_id=chapter_id
)
db.session.add(new_quiz)
db.session.commit()
flash('Quiz added successfully', 'success')
return redirect(f'/admin/chapter/{chapter_id}')
@app.route('/admin/quiz/<int:quiz_id>/question/add', methods=['POST'])
def add_question(quiz_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
question_statement = request.form['question_statement']
option1 = request.form['option1']
option2 = request.form['option2']
option3 = request.form.get('option3', '')
option4 = request.form.get('option4', '')
correct_option = int(request.form['correct_option'])
new_question = Question(
question_statement=question_statement,
option1=option1,
option2=option2,
option3=option3,
option4=option4,
correct_option=correct_option,
quiz_id=quiz_id
)
db.session.add(new_question)
db.session.commit()
flash('Question added successfully', 'success')
return redirect(f'/admin/quiz/{quiz_id}')
# @app.route('/admin/dashboard', methods=['GET', 'POST'])
# def admin_dashboard():
# if not is_admin():
# flash('Unauthorized access', 'danger')
# return redirect('/')
# # Score summary for all users
# score_summary = db.session.query(
# User.username,
# User.full_name,
# func.count(Score.id).label('total_attempts'),
# func.avg(Score.score * 100.0 / Score.total_questions).label('avg_score')
# ).join(Score, User.id == Score.user_id).group_by(User.id).all()
# # Count of all entities
# subjects_count = Subject.query.count()
# chapters_count = Chapter.query.count()
# quizzes_count = Quiz.query.count()
# questions_count = Question.query.count()
# subjects = Subject.query.all()
# return render_template('admin/dashboard.html',
# subjects=subjects,
# score_summary=score_summary,
# subjects_count=subjects_count,
# chapters_count=chapters_count,
# quizzes_count=quizzes_count,
# questions_count=questions_count)
@app.route('/admin/dashboard', methods=['GET', 'POST'])
def admin_dashboard():
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
# Handle subject creation from modal
if request.method == 'POST' and 'create_subject' in request.form:
name = request.form['name']
description = request.form['description']
new_subject = Subject(name=name, description=description)
db.session.add(new_subject)
db.session.commit()
flash('Subject created successfully', 'success')
return redirect('/admin/dashboard')
subjects = Subject.query.all()
return render_template('admin/dashboard.html', subjects=subjects)
# Admin Subject View - Handles chapter creation via modal
@app.route('/admin/subject/<int:subject_id>', methods=['GET', 'POST'])
def view_subject(subject_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
subject = Subject.query.get_or_404(subject_id)
if request.method == 'POST' and 'create_chapter' in request.form:
name = request.form['name']
description = request.form['description']
new_chapter = Chapter(
name=name,
description=description,
subject_id=subject_id
)
db.session.add(new_chapter)
db.session.commit()
flash('Chapter created successfully', 'success')
return redirect(f'/admin/subject/{subject_id}')
# Load all chapters for this subject
chapters = Chapter.query.filter_by(subject_id=subject_id).all()
return render_template('admin/subject.html', subject=subject, chapters=chapters)
# Admin Chapter View - Handles quiz creation via modal
@app.route('/admin/chapter/<int:chapter_id>', methods=['GET', 'POST'])
def view_chapter(chapter_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
chapter = Chapter.query.get_or_404(chapter_id)
# Handle quiz creation from modal
if request.method == 'POST' and 'create_quiz' in request.form:
title = request.form['title']
date_of_quiz = request.form['date_of_quiz']
time_duration = request.form['time_duration']
remarks = request.form.get('remarks', '')
new_quiz = Quiz(
title=title,
date_of_quiz=date_of_quiz,
time_duration=time_duration,
remarks=remarks,
chapter_id=chapter_id
)
db.session.add(new_quiz)
db.session.commit()
flash('Quiz created successfully', 'success')
return redirect(f'/admin/chapter/{chapter_id}')
return render_template('admin/chapter.html', chapter=chapter)
# Admin Quiz View - Handles question creation via modal
@app.route('/admin/quiz/<int:quiz_id>', methods=['GET', 'POST'])
def view_quiz(quiz_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
quiz = Quiz.query.get_or_404(quiz_id)
# Handle question creation from modal
if request.method == 'POST' and 'create_question' in request.form:
question_statement = request.form['question_statement']
option1 = request.form['option1']
option2 = request.form['option2']
option3 = request.form.get('option3', '')
option4 = request.form.get('option4', '')
correct_option = int(request.form['correct_option'])
new_question = Question(
question_statement=question_statement,
option1=option1,
option2=option2,
option3=option3,
option4=option4,
correct_option=correct_option,
quiz_id=quiz_id
)
db.session.add(new_question)
db.session.commit()
flash('Question added successfully', 'success')
return redirect(f'/admin/quiz/{quiz_id}')
return render_template('admin/quiz.html', quiz=quiz)
@app.route('/admin/subject/<int:subject_id>/edit', methods=['GET', 'POST'])
def edit_subject(subject_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
subject = Subject.query.get_or_404(subject_id)
if request.method == 'POST':
subject.name = request.form['name']
subject.description = request.form['description']
db.session.commit()
flash('Subject updated successfully', 'success')
return redirect('/admin/dashboard')
return render_template('admin/edit_subject.html', subject=subject)
@app.route('/admin/subject/<int:subject_id>/delete', methods=['POST'])
def delete_subject(subject_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
subject = Subject.query.get_or_404(subject_id)
db.session.delete(subject)
db.session.commit()
flash('Subject deleted successfully', 'success')
return redirect('/admin/dashboard')
# Admin CRUD for Chapters
@app.route('/admin/chapter/<int:chapter_id>/edit', methods=['GET', 'POST'])
def edit_chapter(chapter_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
chapter = Chapter.query.get_or_404(chapter_id)
if request.method == 'POST':
chapter.name = request.form['name']
chapter.description = request.form['description']
db.session.commit()
flash('Chapter updated successfully', 'success')
return redirect(f'/admin/subject/{chapter.subject_id}')
return render_template('admin/edit_chapter.html', chapter=chapter)
@app.route('/admin/chapter/<int:chapter_id>/delete', methods=['POST'])
def delete_chapter(chapter_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
chapter = Chapter.query.get_or_404(chapter_id)
subject_id = chapter.subject_id
db.session.delete(chapter)
db.session.commit()
flash('Chapter deleted successfully', 'success')
return redirect(f'/admin/subject/{subject_id}')
# Admin CRUD for Quizzes
@app.route('/admin/quiz/<int:quiz_id>/edit', methods=['GET', 'POST'])
def edit_quiz(quiz_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
quiz = Quiz.query.get_or_404(quiz_id)
if request.method == 'POST':
quiz.title = request.form['title']
quiz.date_of_quiz = request.form['date_of_quiz']
quiz.time_duration = request.form['time_duration']
quiz.remarks = request.form.get('remarks', '')
db.session.commit()
flash('Quiz updated successfully', 'success')
return redirect(f'/admin/chapter/{quiz.chapter_id}')
return render_template('admin/edit_quiz.html', quiz=quiz)
@app.route('/admin/quiz/<int:quiz_id>/delete', methods=['POST'])
def delete_quiz(quiz_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
quiz = Quiz.query.get_or_404(quiz_id)
chapter_id = quiz.chapter_id
db.session.delete(quiz)
db.session.commit()
flash('Quiz deleted successfully', 'success')
return redirect(f'/admin/chapter/{chapter_id}')
# Admin CRUD for Questions
@app.route('/admin/question/<int:question_id>/edit', methods=['GET', 'POST'])
def edit_question(question_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
question = Question.query.get_or_404(question_id)
if request.method == 'POST':
question.question_statement = request.form['question_statement']
question.option1 = request.form['option1']
question.option2 = request.form['option2']
question.option3 = request.form.get('option3', '')
question.option4 = request.form.get('option4', '')
question.correct_option = int(request.form['correct_option'])
db.session.commit()
flash('Question updated successfully', 'success')
return redirect(f'/admin/quiz/{question.quiz_id}')
return render_template('admin/edit_question.html', question=question)
@app.route('/admin/question/<int:question_id>/delete', methods=['POST'])
def delete_question(question_id):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
question = Question.query.get_or_404(question_id)
quiz_id = question.quiz_id
db.session.delete(question)
db.session.commit()
flash('Question deleted successfully', 'success')
return redirect(f'/admin/quiz/{quiz_id}')
@app.route('/user/dashboard', methods=['GET', 'POST'])
def user_dashboard():
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
user = User.query.get(session['user_id'])
query = request.args.get('q', '')
# User's score summary
user_scores = Score.query.filter_by(user_id=user.id).all()
total_attempts = len(user_scores)
# Safely calculate average score
avg_score = 0
if total_attempts > 0:
total_percentage = 0
valid_attempts = 0
for s in user_scores:
if s.total_questions > 0: # Only include attempts with questions
total_percentage += (s.score / s.total_questions) * 100
valid_attempts += 1
if valid_attempts > 0:
avg_score = total_percentage / valid_attempts
# Search functionality
if query:
subjects = Subject.query.filter(Subject.name.ilike(f'%{query}%')).all()
chapters = Chapter.query.filter(Chapter.name.ilike(f'%{query}%')).all()
quizzes = Quiz.query.filter(Quiz.title.ilike(f'%{query}%')).all()
return render_template('user/dashboard.html',
user=user,
subjects=subjects,
chapters=chapters,
quizzes=quizzes,
query=query,
total_attempts=total_attempts,
avg_score=avg_score)
# Normal dashboard view
subjects = Subject.query.all()
return render_template('user/dashboard.html',
user=user,
subjects=subjects,
total_attempts=total_attempts,
avg_score=avg_score)
@app.route('/user/subject/<int:subject_id>')
def user_view_subject(subject_id):
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
subject = Subject.query.get_or_404(subject_id)
return render_template('user/subject.html', subject=subject)
# User Chapter View
@app.route('/user/chapter/<int:chapter_id>')
def user_view_chapter(chapter_id):
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
chapter = Chapter.query.get_or_404(chapter_id)
return render_template('user/chapter.html', chapter=chapter)
# Take Quiz
@app.route('/user/quiz/<int:quiz_id>', methods=['GET', 'POST'])
def take_quiz(quiz_id):
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
quiz = Quiz.query.get_or_404(quiz_id)
if request.method == 'POST':
score = 0
total_questions = len(quiz.questions) # Use actual question count
user_answers = {}
for question in quiz.questions:
user_answer = request.form.get(f'question_{question.id}')
user_answers[question.id] = user_answer
if user_answer and int(user_answer) == question.correct_option:
score += 1
# Only record score if there were questions
if total_questions > 0:
new_score = Score(
user_id=session['user_id'],
quiz_id=quiz_id,
score=score,
total_questions=total_questions
)
db.session.add(new_score)
db.session.commit()
# Store user answers in session for result page
session['quiz_answers'] = {
'quiz_id': quiz_id,
'user_answers': user_answers,
'score_id': new_score.id
}
return redirect(f'/user/quiz/{quiz_id}/result')
else:
flash('This quiz has no questions to score', 'warning')
return redirect(f'/user/quiz/{quiz_id}')
return render_template('user/take_quiz.html', quiz=quiz)
@app.route('/user/quiz/<int:quiz_id>/result')
def quiz_result(quiz_id):
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
quiz = Quiz.query.get_or_404(quiz_id)
saved_answers = session.get('quiz_answers', {})
# Check if this is the correct quiz result
if saved_answers.get('quiz_id') != quiz_id:
flash('Invalid quiz result access', 'danger')
return redirect('/user/dashboard')
score = Score.query.get_or_404(saved_answers['score_id'])
user_answers = saved_answers.get('user_answers', {})
# Handle case where total_questions is zero
if score.total_questions == 0:
flash('This quiz had no questions', 'warning')
return redirect(f'/user/quiz/{quiz_id}')
return render_template('user/quiz_result.html',
quiz=quiz,
score=score,
user_answers=user_answers)
@app.route('/user/scores')
def user_scores():
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
scores = Score.query.filter_by(
user_id=session['user_id']
).order_by(Score.timestamp.desc()).all()
return render_template('user/scores.html', scores=scores)
@app.route('/user/quiz/<int:quiz_id>/result/<int:attempt_id>')
def view_attempt(quiz_id, attempt_id):
if not is_logged_in() or is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
quiz = Quiz.query.get_or_404(quiz_id)
score = Score.query.filter_by(
id=attempt_id,
user_id=session['user_id'],
quiz_id=quiz_id
).first_or_404()
return render_template('user/quiz_result.html',
quiz=quiz,
score=score,
user_answers={})
@app.route('/admin/user/<username>/scores')
def admin_view_user_scores(username):
if not is_admin():
flash('Unauthorized access', 'danger')
return redirect('/')
user = User.query.filter_by(username=username).first_or_404()
scores = Score.query.filter_by(user_id=user.id).order_by(Score.timestamp.desc()).all()
return render_template('admin/user_scores.html', user=user, scores=scores)
# Initialize app
with app.app_context():
db.create_all()
create_admin()
if __name__ == '__main__':
app.run(debug=True)