Browse Source
refactor(quiz): Adjust quiz lesson relation
refactor(quiz): Adjust quiz lesson relation
- Changed Quiz.lesson to CourseLesson - Updated serializers and views - Modified admin to restrict access - Added management command to clear data - Updated admin UImaster
10 changed files with 144 additions and 29 deletions
-
6apps/course/serializers/lesson.py
-
2apps/course/views/lesson.py
-
23apps/quiz/admin/quiz.py
-
0apps/quiz/management/__init__.py
-
0apps/quiz/management/commands/__init__.py
-
78apps/quiz/management/commands/clear_quiz_data.py
-
20apps/quiz/migrations/0002_change_quiz_lesson_to_courselesson.py
-
3apps/quiz/models/quiz.py
-
13apps/quiz/serializers/quiz.py
-
28config/settings/base.py
@ -0,0 +1,78 @@ |
|||
from django.core.management.base import BaseCommand |
|||
from django.db import transaction |
|||
|
|||
from apps.quiz.models import Quiz, Question, QuizParticipant, ParticipantAnswer |
|||
|
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Clear all quiz-related data from the database' |
|||
|
|||
def add_arguments(self, parser): |
|||
parser.add_argument( |
|||
'--confirm', |
|||
action='store_true', |
|||
help='Confirm that you want to delete all quiz data', |
|||
) |
|||
|
|||
def handle(self, *args, **options): |
|||
if not options['confirm']: |
|||
self.stdout.write( |
|||
self.style.WARNING( |
|||
'This command will delete ALL quiz-related data from the database!\n' |
|||
'This includes:\n' |
|||
'- All Quizzes\n' |
|||
'- All Questions\n' |
|||
'- All Quiz Participants\n' |
|||
'- All Participant Answers\n\n' |
|||
'Use --confirm flag to proceed with deletion.\n' |
|||
'Example: python manage.py clear_quiz_data --confirm' |
|||
) |
|||
) |
|||
return |
|||
|
|||
try: |
|||
with transaction.atomic(): |
|||
# Count records before deletion |
|||
participant_answers_count = ParticipantAnswer.objects.count() |
|||
quiz_participants_count = QuizParticipant.objects.count() |
|||
questions_count = Question.objects.count() |
|||
quizzes_count = Quiz.objects.count() |
|||
|
|||
self.stdout.write( |
|||
f'Found {participant_answers_count} participant answers, ' |
|||
f'{quiz_participants_count} quiz participants, ' |
|||
f'{questions_count} questions, and ' |
|||
f'{quizzes_count} quizzes.' |
|||
) |
|||
|
|||
# Delete in order to respect foreign key constraints |
|||
# ParticipantAnswer -> QuizParticipant -> Quiz |
|||
# Question -> Quiz |
|||
|
|||
self.stdout.write('Deleting participant answers...') |
|||
ParticipantAnswer.objects.all().delete() |
|||
|
|||
self.stdout.write('Deleting quiz participants...') |
|||
QuizParticipant.objects.all().delete() |
|||
|
|||
self.stdout.write('Deleting questions...') |
|||
Question.objects.all().delete() |
|||
|
|||
self.stdout.write('Deleting quizzes...') |
|||
Quiz.objects.all().delete() |
|||
|
|||
self.stdout.write( |
|||
self.style.SUCCESS( |
|||
f'Successfully deleted all quiz data:\n' |
|||
f'- {participant_answers_count} participant answers\n' |
|||
f'- {quiz_participants_count} quiz participants\n' |
|||
f'- {questions_count} questions\n' |
|||
f'- {quizzes_count} quizzes' |
|||
) |
|||
) |
|||
|
|||
except Exception as e: |
|||
self.stdout.write( |
|||
self.style.ERROR(f'Error occurred while clearing quiz data: {str(e)}') |
|||
) |
|||
raise |
|||
@ -0,0 +1,20 @@ |
|||
# Generated by Django 5.1.8 on 2025-08-12 22:14 |
|||
|
|||
import django.db.models.deletion |
|||
from django.db import migrations, models |
|||
|
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
('course', '0006_participant_is_active'), |
|||
('quiz', '0001_initial'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.AlterField( |
|||
model_name='quiz', |
|||
name='lesson', |
|||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='quizzes', to='course.courselesson', verbose_name='lesson'), |
|||
), |
|||
] |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue