Add migration actions for Rapid questions to Shorts in admin and templates

This commit is contained in:
Ross
2026-03-25 21:12:30 +00:00
parent ec0bad02c6
commit 06fe73e2a3
6 changed files with 168 additions and 0 deletions
+46
View File
@@ -821,6 +821,52 @@ def question_save_annotation(request, pk):
return JsonResponse(data, status=400)
@user_is_author_or_rapid_checker
def question_migrate_to_shorts(request, pk):
"""Migrate a single Rapid question to a shorts.Question.
Accessible to question authors and members of the rapid_checker group (or superusers).
"""
rapid = get_object_or_404(Rapid, pk=pk)
from shorts.models import Question as ShortQuestion
try:
q = ShortQuestion.from_rapid(rapid)
except Exception as e:
return HttpResponse(f"Migration failed: {e}", status=500)
return redirect(q.get_absolute_url())
@login_required
def exam_migrate_rapids_to_shorts(request, pk):
"""Migrate all rapids associated with an Exam to shorts.Question.
Accessible to exam authors and superusers.
"""
exam = get_object_or_404(Exam, pk=pk)
# check permission: exam author or superuser
if not (request.user.is_superuser or exam.author.filter(id=request.user.id).exists()):
raise PermissionDenied()
from shorts.models import Question as ShortQuestion
for rapid in exam.exam_questions.all():
try:
ShortQuestion.from_rapid(rapid)
except Exception:
# continue on error
continue
# Redirect to the exam overview for shorts if available, otherwise back to rapids exam
try:
return redirect("shorts:exam_overview", pk=exam.pk)
except Exception:
return redirect("rapids:exam_overview", pk=exam.pk)
class ExamClone(ExamCloneMixin, ExamCreate):
"""Clone exam view"""