.
This commit is contained in:
@@ -11,4 +11,7 @@ urlpatterns = [
|
||||
path("examination/ajax/get_examination_id",
|
||||
views.get_examination_id,
|
||||
name="get_examination_id"),
|
||||
path("exam_json_edit",
|
||||
views.generic_exam_json_edit,
|
||||
name="generic_exam_json_edit"),
|
||||
]
|
||||
|
||||
@@ -24,6 +24,13 @@ from .forms import (
|
||||
|
||||
from .models import Examination
|
||||
|
||||
from rapids.models import Rapid as RapidQuestion
|
||||
from rapids.models import Exam as RapidExam
|
||||
from longs.models import Long as LongQuestion
|
||||
from longs.models import Exam as LongExam
|
||||
from anatomy.models import AnatomyQuestion as AnatomyQuestion
|
||||
from anatomy.models import Exam as AnatomyExam
|
||||
|
||||
from django.db.models import Case, When
|
||||
|
||||
|
||||
@@ -52,6 +59,74 @@ def get_examination_id(request):
|
||||
return HttpResponse(json.dumps(data), content_type="application/json")
|
||||
return HttpResponse("/")
|
||||
|
||||
# Generic view to dispatch to indivuals app views
|
||||
@login_required
|
||||
def generic_exam_json_edit(request):
|
||||
if request.is_ajax() and request.method == "POST":
|
||||
question_type = request.POST.get("type")
|
||||
|
||||
if question_type == "rapid":
|
||||
Exam = RapidExam
|
||||
Question = RapidQuestion
|
||||
elif question_type == "long":
|
||||
Exam = LongExam
|
||||
Question = LongQuestion
|
||||
elif question_type == "anatomy":
|
||||
Exam = AnatomyExam
|
||||
Question = AnatomyQuestion
|
||||
else:
|
||||
data = {"status": "error"}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
|
||||
exam = get_object_or_404(Exam, pk=request.POST.get("exam_id"))
|
||||
|
||||
if "add_exam_questions" in request.POST:
|
||||
question_ids = json.loads(request.POST.get("add_exam_questions"))
|
||||
question_objects = Question.objects.filter(pk__in=question_ids)
|
||||
|
||||
exam.exam_questions.add(question_objects)
|
||||
exam.save()
|
||||
data = {"status": "success"}
|
||||
return JsonResponse(data, status=200)
|
||||
|
||||
if "set_open_access" in request.POST:
|
||||
if request.POST.get("set_open_access") == "true":
|
||||
state = True
|
||||
else:
|
||||
state = False
|
||||
|
||||
questions_changed = []
|
||||
for q in exam.exam_questions.all():
|
||||
q.open_access = state
|
||||
q.save()
|
||||
questions_changed.append(q.pk)
|
||||
|
||||
data = {"status": "success", "questions": questions_changed, "state": state}
|
||||
return JsonResponse(data, status=200)
|
||||
|
||||
if "set_exam_order" in request.POST:
|
||||
new_order = json.loads(request.POST.get("set_exam_order"))
|
||||
|
||||
preserved = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(new_order)])
|
||||
objects = Question.objects.filter(pk__in=new_order).order_by(preserved)
|
||||
|
||||
if len(objects) != exam.exam_questions.count():
|
||||
data = {"status": "error, count does not match"}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
exam.exam_questions.set(objects)
|
||||
exam.save()
|
||||
data = {"status": "success"}
|
||||
return JsonResponse(data, status=200)
|
||||
|
||||
|
||||
data = {"status": "error, unknown request"}
|
||||
return JsonResponse(data, status=400)
|
||||
else:
|
||||
data = {"status": "error"}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
|
||||
|
||||
class ExamViews(View, LoginRequiredMixin):
|
||||
|
||||
Reference in New Issue
Block a user