Add migration actions for Rapid questions to Shorts in admin and templates
This commit is contained in:
@@ -5,6 +5,7 @@ from django.forms import ModelForm
|
||||
|
||||
from reversion.admin import VersionAdmin
|
||||
from django.forms.widgets import RadioSelect
|
||||
from django.contrib import messages
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Examination)
|
||||
@@ -64,9 +65,54 @@ class RapidAdmin(VersionAdmin):
|
||||
RapidAnswersInline,
|
||||
ExamInline,
|
||||
]
|
||||
actions = ["migrate_to_shorts"]
|
||||
|
||||
def migrate_to_shorts(self, request, queryset):
|
||||
"""Admin action to migrate selected Rapid questions to shorts.Question"""
|
||||
from shorts.models import Question as ShortQuestion
|
||||
|
||||
created = []
|
||||
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))
|
||||
except Exception as e:
|
||||
self.message_user(request, f"Failed to migrate Rapid {rapid.id}: {e}", level=messages.ERROR)
|
||||
else:
|
||||
self.message_user(request, f"No permission to migrate Rapid {rapid.id}", level=messages.ERROR)
|
||||
|
||||
if created:
|
||||
self.message_user(request, f"Created shorts questions: {', '.join(created)}", level=messages.SUCCESS)
|
||||
|
||||
migrate_to_shorts.short_description = "Migrate selected rapids to shorts"
|
||||
|
||||
class ExamAdmin(VersionAdmin, admin.ModelAdmin):
|
||||
save_as = True
|
||||
actions = ["migrate_exam_questions"]
|
||||
|
||||
def migrate_exam_questions(self, request, queryset):
|
||||
"""Admin action to migrate all Rapid questions linked to selected Exam(s)"""
|
||||
from shorts.models import Question as ShortQuestion
|
||||
|
||||
created = []
|
||||
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))
|
||||
except Exception as e:
|
||||
self.message_user(request, f"Failed to migrate Rapid {rapid.id}: {e}", level=messages.ERROR)
|
||||
else:
|
||||
self.message_user(request, f"No permission to migrate Rapid {rapid.id}", level=messages.ERROR)
|
||||
|
||||
if created:
|
||||
self.message_user(request, f"Created shorts questions: {', '.join(created)}", level=messages.SUCCESS)
|
||||
|
||||
migrate_exam_questions.short_description = "Migrate all rapids in selected exam(s) to shorts"
|
||||
|
||||
admin.site.register(Rapid, RapidAdmin)
|
||||
admin.site.register(Exam, ExamAdmin)
|
||||
|
||||
Reference in New Issue
Block a user