All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
from django.http import HttpRequest, HttpResponse
|
|
from django.shortcuts import render, redirect
|
|
from quiz.models import QuizSession, Course, Tag
|
|
from quiz.forms import CreateQuizForm
|
|
|
|
|
|
def create_quiz(request: HttpRequest) -> HttpResponse:
|
|
if request.method == 'POST':
|
|
# Handle quick-start tag-based quiz
|
|
tag_slug = request.POST.get('tag_slug')
|
|
if tag_slug:
|
|
try:
|
|
tag = Tag.objects.get(slug=tag_slug)
|
|
course = Course.objects.first() # Get first course
|
|
session = QuizSession.objects.create(
|
|
user=request.quiz_user,
|
|
course=course,
|
|
question_types=[]
|
|
)
|
|
session.tags.set([tag])
|
|
return redirect('quiz:quiz_mode', session_id=session.id)
|
|
except Tag.DoesNotExist:
|
|
pass
|
|
|
|
# Handle custom form-based quiz
|
|
form = CreateQuizForm(request.POST)
|
|
if form.is_valid():
|
|
course = form.cleaned_data.get('course')
|
|
exams = form.cleaned_data.get('exams')
|
|
tags = form.cleaned_data.get('tags')
|
|
q_types = form.cleaned_data.get('question_type')
|
|
|
|
session = QuizSession.objects.create(
|
|
user=request.quiz_user,
|
|
course=course,
|
|
question_types=q_types if q_types else []
|
|
)
|
|
if tags:
|
|
session.tags.set(tags)
|
|
if exams:
|
|
session.exams.set(exams)
|
|
|
|
return redirect('quiz:quiz_mode', session_id=session.id)
|
|
else:
|
|
form = CreateQuizForm()
|
|
|
|
return render(request, 'quiz_create.html', {'form': form})
|
|
|