continue unifying views
This commit is contained in:
+152
-29
@@ -6,8 +6,18 @@ from django.views.decorators.csrf import csrf_exempt
|
||||
from django.http import Http404, JsonResponse
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
|
||||
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
|
||||
from django.core.cache import cache
|
||||
|
||||
import json
|
||||
|
||||
from django.views import View
|
||||
|
||||
from .forms import (
|
||||
ExaminationForm,
|
||||
)
|
||||
@@ -40,12 +50,15 @@ def get_examination_id(request):
|
||||
return HttpResponse("/")
|
||||
|
||||
|
||||
class ExamViews:
|
||||
def __init__(self, exam, app, question_type):
|
||||
|
||||
class ExamViews(View, LoginRequiredMixin):
|
||||
def __init__(self, exam, app, question_type, loadJsonAnswer):
|
||||
self.Exam = exam
|
||||
self.app_name = app
|
||||
self.question_type = question_type
|
||||
self.loadJsonAnswer = loadJsonAnswer
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_toggle_results_published(self, request, pk):
|
||||
if request.is_ajax() and request.method == "POST":
|
||||
|
||||
@@ -62,6 +75,7 @@ class ExamViews:
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_toggle_active(self, request, pk):
|
||||
if request.is_ajax() and request.method == "POST":
|
||||
|
||||
@@ -75,6 +89,105 @@ class ExamViews:
|
||||
data = {"status": "error"}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_json_recreate(self, request, pk):
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
exam.recreate_json = True
|
||||
exam.save()
|
||||
|
||||
return redirect("{}:exam_overview".format(self.app_name), pk=pk)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_list(self, request):
|
||||
exams = self.Exam.objects.all()
|
||||
return render(request, "{}/exam_list.html".format(self.app_name), {"exams": exams})
|
||||
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_overview(self, request, pk):
|
||||
#print(Exam.objects.all())
|
||||
#exams = Exam.objects.all()
|
||||
#print("test", Exam.objects.all().get(id=pk))
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
questions = exam.exam_questions.all()
|
||||
|
||||
question_number = len(questions)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"{}/exam_overview.html".format(self.app_name),
|
||||
{"exam": exam, "questions": questions, "question_number": question_number},
|
||||
)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_json_edit(self, request, pk):
|
||||
if request.is_ajax() and request.method == "POST":
|
||||
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
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)
|
||||
else:
|
||||
data = {"status": "error"}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def mark_overview(self, request, pk):
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
questions = exam.exam_questions.all()
|
||||
|
||||
return render(
|
||||
request, "{}/mark_overview.html".format(self.app_name), {"exam": exam, "questions": questions}
|
||||
)
|
||||
|
||||
@method_decorator(login_required)
|
||||
def index(self, request):
|
||||
exams = self.Exam.objects.all()
|
||||
return render(request, "{}/index.html".format(self.app_name), {"exams": exams})
|
||||
|
||||
@method_decorator(login_required)
|
||||
def exam_question_detail(self, request, pk, sk):
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
question = exam.exam_questions.all()[sk]
|
||||
|
||||
exam_length = len(exam.exam_questions.all())
|
||||
|
||||
pos = exam.get_question_index(question) + 1
|
||||
|
||||
previous = -1
|
||||
if sk > 0:
|
||||
previous = sk - 1
|
||||
next = sk + 1
|
||||
if sk == exam_length - 1:
|
||||
next = False
|
||||
|
||||
return render(
|
||||
request,
|
||||
"{}/question_detail.html".format(self.app_name),
|
||||
{
|
||||
"question": question,
|
||||
"exam": exam,
|
||||
"next": next,
|
||||
"previous": previous,
|
||||
"exam_length": exam_length,
|
||||
"pos": pos,
|
||||
},
|
||||
)
|
||||
|
||||
def active_exams(self, request, json=True):
|
||||
exams = self.Exam.objects.all()
|
||||
@@ -96,35 +209,45 @@ class ExamViews:
|
||||
|
||||
return JsonResponse(active_exams)
|
||||
|
||||
@login_required
|
||||
def exam_json_recreate(self, request, pk):
|
||||
@method_decorator(csrf_exempt)
|
||||
def postExamAnswers(request):
|
||||
if request.is_ajax and request.method == "POST":
|
||||
|
||||
n = 0
|
||||
|
||||
for answer in json.loads(request.POST.get("answers")):
|
||||
ret = self.loadJsonAnswer(answer)
|
||||
|
||||
#if ret is not True:
|
||||
# return ret
|
||||
|
||||
if ret:
|
||||
n = n + 1
|
||||
|
||||
# print(UserAnswer.objects.filter(exam__id=q["eid"]))
|
||||
# print(request.urlencode())
|
||||
|
||||
return JsonResponse({"success": True, "question_count": n})
|
||||
return JsonResponse({"success": False, "error": "Invalid data"})
|
||||
|
||||
def exam_json(self, request, pk):
|
||||
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
exam.recreate_json = True
|
||||
if not exam.active:
|
||||
raise Http404("No available exam")
|
||||
|
||||
exam_json_cache = cache.get("{}_exam_json_{}".format(self.app_name, pk))
|
||||
|
||||
if exam_json_cache is not None and not exam.recreate_json:
|
||||
exam_json_cache["cached"] = True
|
||||
return JsonResponse(exam_json_cache)
|
||||
|
||||
exam_json = exam.get_exam_json()
|
||||
|
||||
exam.recreate_json = False
|
||||
exam.save()
|
||||
|
||||
return redirect("{}:exam_overview".format(self.app_name), pk=pk)
|
||||
|
||||
@login_required
|
||||
def exam_list(self, request):
|
||||
exams = self.Exam.objects.all()
|
||||
return render(request, "{}/exam_list.html".format(self.app_name), {"exams": exams})
|
||||
|
||||
|
||||
@login_required
|
||||
def exam_overview(self, request, pk):
|
||||
#print(Exam.objects.all())
|
||||
#exams = Exam.objects.all()
|
||||
#print("test", Exam.objects.all().get(id=pk))
|
||||
exam = get_object_or_404(self.Exam, pk=pk)
|
||||
|
||||
questions = exam.exam_questions.all()
|
||||
|
||||
question_number = len(questions)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"{}/exam_overview.html".format(self.app_name),
|
||||
{"exam": exam, "questions": questions, "question_number": question_number},
|
||||
)
|
||||
cache.set("{}_exam_json_{}".format(self.app_name, pk), exam_json, 3600)
|
||||
|
||||
return JsonResponse(exam_json)
|
||||
Reference in New Issue
Block a user