vault backup: 2025-12-26 02:09:22
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
This commit is contained in:
87
stroma/quiz/management/commands/populate_exams.py
Normal file
87
stroma/quiz/management/commands/populate_exams.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""
|
||||
Management command to populate Course and Exam models from existing questions.
|
||||
"""
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
from quiz.models import Course, Exam, Question
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Populate Course and Exam models from existing question file paths'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
# Create default course
|
||||
course, created = Course.objects.get_or_create(
|
||||
name="Anatomi & Histologi 2",
|
||||
defaults={'code': 'AH2', 'description': 'Anatomy and Histology course'}
|
||||
)
|
||||
|
||||
if created:
|
||||
self.stdout.write(self.style.SUCCESS(f'Created course: {course.name}'))
|
||||
else:
|
||||
self.stdout.write(f'Course exists: {course.name}')
|
||||
|
||||
# Analyze existing questions and create exams
|
||||
questions = Question.objects.all()
|
||||
exam_folders = {}
|
||||
|
||||
for question in questions:
|
||||
# Extract exam date from file path
|
||||
# Expected: content/Anatomi & Histologi 2/Gamla tentor/2022-01-15/1.md
|
||||
path_parts = Path(question.file_path).parts
|
||||
|
||||
if len(path_parts) >= 2:
|
||||
# Try to find a date-like folder
|
||||
for part in path_parts:
|
||||
if '-' in part and len(part) == 10: # Looks like YYYY-MM-DD
|
||||
try:
|
||||
exam_date = datetime.strptime(part, '%Y-%m-%d').date()
|
||||
folder_path = '/'.join(path_parts[:-1])
|
||||
|
||||
if part not in exam_folders:
|
||||
exam_folders[part] = {
|
||||
'date': exam_date,
|
||||
'folder': folder_path,
|
||||
'questions': []
|
||||
}
|
||||
exam_folders[part]['questions'].append(question)
|
||||
break
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
# Create exams and assign questions
|
||||
exams_created = 0
|
||||
questions_assigned = 0
|
||||
|
||||
for folder_name, data in sorted(exam_folders.items()):
|
||||
exam, created = Exam.objects.get_or_create(
|
||||
course=course,
|
||||
date=data['date'],
|
||||
defaults={
|
||||
'name': folder_name,
|
||||
'folder_path': data['folder']
|
||||
}
|
||||
)
|
||||
|
||||
if created:
|
||||
exams_created += 1
|
||||
self.stdout.write(self.style.SUCCESS(f' Created exam: {exam.date}'))
|
||||
|
||||
# Assign questions to this exam
|
||||
for question in data['questions']:
|
||||
if question.exam != exam:
|
||||
question.exam = exam
|
||||
question.save(update_fields=['exam'])
|
||||
questions_assigned += 1
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
f'\nSummary:\n'
|
||||
f' Exams created: {exams_created}\n'
|
||||
f' Questions assigned: {questions_assigned}\n'
|
||||
f' Total exams: {Exam.objects.count()}\n'
|
||||
f' Questions with exams: {Question.objects.filter(exam__isnull=False).count()}\n'
|
||||
f' Questions without exams: {Question.objects.filter(exam__isnull=True).count()}'
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user