Enhance migration process for rapids to shorts; skip normal rapids and provide user feedback on migration status
This commit is contained in:
+14
-2
@@ -72,12 +72,16 @@ class RapidAdmin(VersionAdmin):
|
||||
from shorts.models import Question as ShortQuestion
|
||||
|
||||
created = []
|
||||
skipped = []
|
||||
for rapid in queryset:
|
||||
# allow admin or any author to perform migration
|
||||
if request.user.is_superuser or rapid.author.filter(id=request.user.id).exists():
|
||||
try:
|
||||
q = ShortQuestion.from_rapid(rapid)
|
||||
created.append(str(q.id))
|
||||
if q is None:
|
||||
skipped.append(str(rapid.id))
|
||||
else:
|
||||
created.append(str(q.id))
|
||||
except Exception as e:
|
||||
self.message_user(request, f"Failed to migrate Rapid {rapid.id}: {e}", level=messages.ERROR)
|
||||
else:
|
||||
@@ -85,6 +89,8 @@ class RapidAdmin(VersionAdmin):
|
||||
|
||||
if created:
|
||||
self.message_user(request, f"Created shorts questions: {', '.join(created)}", level=messages.SUCCESS)
|
||||
if skipped:
|
||||
self.message_user(request, f"Skipped normal rapids (not migrated): {', '.join(skipped)}", level=messages.INFO)
|
||||
|
||||
migrate_to_shorts.short_description = "Migrate selected rapids to shorts"
|
||||
|
||||
@@ -97,13 +103,17 @@ class ExamAdmin(VersionAdmin, admin.ModelAdmin):
|
||||
from shorts.models import Question as ShortQuestion
|
||||
|
||||
created = []
|
||||
skipped = []
|
||||
for exam in queryset:
|
||||
for rapid in exam.exam_questions.all():
|
||||
# only allow if admin or author of rapid
|
||||
if request.user.is_superuser or rapid.author.filter(id=request.user.id).exists():
|
||||
try:
|
||||
q = ShortQuestion.from_rapid(rapid)
|
||||
created.append(str(q.id))
|
||||
if q is None:
|
||||
skipped.append(str(rapid.id))
|
||||
else:
|
||||
created.append(str(q.id))
|
||||
except Exception as e:
|
||||
self.message_user(request, f"Failed to migrate Rapid {rapid.id}: {e}", level=messages.ERROR)
|
||||
else:
|
||||
@@ -111,6 +121,8 @@ class ExamAdmin(VersionAdmin, admin.ModelAdmin):
|
||||
|
||||
if created:
|
||||
self.message_user(request, f"Created shorts questions: {', '.join(created)}", level=messages.SUCCESS)
|
||||
if skipped:
|
||||
self.message_user(request, f"Skipped normal rapids (not migrated): {', '.join(skipped)}", level=messages.INFO)
|
||||
|
||||
migrate_exam_questions.short_description = "Migrate all rapids in selected exam(s) to shorts"
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
{% for image in question.get_images %}
|
||||
<img src="{{ image|thumbnail_url:'exam-list' }}" alt="thumbail" />
|
||||
{% empty %}
|
||||
No image added.
|
||||
No image added.
|
||||
{% endfor %}
|
||||
</a>
|
||||
</span>
|
||||
|
||||
+14
-2
@@ -78,6 +78,7 @@ from django.core.cache import cache
|
||||
from helpers.images import image_as_base64
|
||||
|
||||
import logging
|
||||
from django.contrib import messages
|
||||
from copy import deepcopy
|
||||
from django.forms.models import model_to_dict
|
||||
from rapids.forms import RapidCreationDefaultForm
|
||||
@@ -834,8 +835,14 @@ def question_migrate_to_shorts(request, pk):
|
||||
try:
|
||||
q = ShortQuestion.from_rapid(rapid)
|
||||
except Exception as e:
|
||||
return HttpResponse(f"Migration failed: {e}", status=500)
|
||||
messages.error(request, f"Migration failed: {e}")
|
||||
return redirect("rapids:question_detail", pk=rapid.pk)
|
||||
|
||||
if q is None:
|
||||
messages.info(request, "Question skipped (marked normal) — not migrated.")
|
||||
return redirect("rapids:question_detail", pk=rapid.pk)
|
||||
|
||||
messages.success(request, "Migration complete — redirected to new Short question.")
|
||||
return redirect(q.get_absolute_url())
|
||||
|
||||
|
||||
@@ -855,11 +862,16 @@ def exam_migrate_rapids_to_shorts(request, pk):
|
||||
|
||||
for rapid in exam.exam_questions.all():
|
||||
try:
|
||||
ShortQuestion.from_rapid(rapid)
|
||||
q = ShortQuestion.from_rapid(rapid)
|
||||
if q is None:
|
||||
# skipped normal
|
||||
continue
|
||||
except Exception:
|
||||
# continue on error
|
||||
continue
|
||||
|
||||
messages.success(request, "Exam migration attempted. Normal questions were skipped.")
|
||||
|
||||
# Redirect to the exam overview for shorts if available, otherwise back to rapids exam
|
||||
try:
|
||||
return redirect("shorts:exam_overview", pk=exam.pk)
|
||||
|
||||
@@ -302,6 +302,10 @@ class Question(QuestionBase):
|
||||
|
||||
from rapids.models import Answer as RapidAnswer
|
||||
|
||||
# Skip migration for normal rapids (nothing to migrate)
|
||||
if getattr(rapid, "normal", False):
|
||||
return None
|
||||
|
||||
guidance_parts = []
|
||||
guidance_parts.append(f"Migrated from rapid id: {rapid.id}")
|
||||
if rapid.laterality:
|
||||
|
||||
Reference in New Issue
Block a user