This commit is contained in:
Ross
2021-02-23 21:12:50 +00:00
parent 38a2165893
commit 7f4dbfec51
12 changed files with 153 additions and 24 deletions
+36 -12
View File
@@ -24,6 +24,9 @@ from .forms import (
from .models import Examination
from django.db.models import Case, When
# Create your views here.
@login_required
def create_examination(request):
@@ -52,8 +55,9 @@ def get_examination_id(request):
class ExamViews(View, LoginRequiredMixin):
def __init__(self, exam, app, question_type, loadJsonAnswer):
def __init__(self, exam, question, app, question_type, loadJsonAnswer):
self.Exam = exam
self.Question = question
self.app_name = app
self.question_type = question_type
self.loadJsonAnswer = loadJsonAnswer
@@ -127,19 +131,39 @@ class ExamViews(View, LoginRequiredMixin):
exam = get_object_or_404(self.Exam, pk=pk)
if request.POST.get("set_open_access") == "true":
state = True
else:
state = False
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)
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)
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 = self.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)