This commit is contained in:
Ross
2021-02-16 17:09:02 +00:00
parent a0b9e92aba
commit 4f60fccb06
10 changed files with 188 additions and 180 deletions
+70 -118
View File
@@ -5,7 +5,7 @@ from django import forms
# from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied, ViewDoesNotExist
from django.contrib.auth.mixins import LoginRequiredMixin
@@ -91,7 +91,7 @@ def long_detail(request, pk):
# logging.debug(long.long_notes.first())
# logging.debug(long.subspecialty.first().name.all())
return render(request, "longs/long_detail.html", {"question": long})
return render(request, "longs/question_detail.html", {"question": long})
@login_required
def long_series_detail(request, pk):
@@ -139,7 +139,7 @@ def long_split(request, pk):
# logging.debug(long.long_notes.first())
# logging.debug(long.subspecialty.first().name.all())
return render(request, "longs/long_detail.html", {"question": long})
return render(request, "longs/question_detail.html", {"question": long})
@login_required
@@ -484,7 +484,7 @@ def long_scrap(request, pk):
long.scrapped = not long.scrapped
long.save()
return HttpResponseRedirect(reverse("longs:long_detail", args=(pk,)))
return HttpResponseRedirect(reverse("longs:question_detail", args=(pk,)))
# @login_required
@@ -656,6 +656,70 @@ def loadJsonAnswer(answer):
return True
@login_required
def mark_answer(request, pk, sk, tk):
exam = get_object_or_404(Exam, pk=pk)
questions = exam.exam_questions.all()
n = sk
question_details = {
"total": len(questions),
"current": n + 1,
}
try:
question = questions[sk]
except IndexError:
raise Http404("Exam question does not exist")
try:
answer = question.cid_user_answers.get(cid=tk)
except ObjectDoesNotExist:
raise Http404("User answer does not exist")
try:
unmarked = question.GetUnmarkedAnswers()
next_unmarked_id = unmarked[0].cid
except IndexError:
next_unmarked_id = False
if request.method == "POST":
form = MarkLongQuestionForm(request.POST)
if form.is_valid():
# If skip button is pressed skip the question without marking
# Skip is problematic if we skip to the next unmarked answer
#if "skip" in request.POST:
# return redirect("longs:mark_answer", pk=pk, sk=sk, tk=next_unmarked_id)
# Extract score from form and save it to the object
answer.score = form.cleaned_data["score"]
answer.save()
if "next" in request.POST:
return redirect("longs:mark_answer", pk=pk, sk=sk, tk=next_unmarked_id)
#elif "previous" in request.POST:
# return redirect("longs:mark", pk=pk, sk=n - 1)
else:
form = MarkLongQuestionForm(initial={'score': answer.score})
return render(
request,
"longs/mark_answer.html",
{
"exam": exam,
"form": form,
"answer": answer,
"question": question,
"question_details": question_details,
"next_unmarked_id": next_unmarked_id,
},
)
# @user_passes_test(user_is_admin, login_url="/accounts/login")
@login_required
@@ -676,129 +740,17 @@ def mark(request, pk, sk):
except IndexError:
raise Http404("Exam question does not exist")
answers_dict = {}
for ans in question.answers.all():
answers_dict[ans.answer_compare] = ans
marked_answers_set = set(answers_dict.keys())
# correct_answers = [i.answer.lower() for i in question.answers.all()]
# half_correct_answers = [
# i.answer.lower() for i in question.half_mark_answers.all()
# ]
# incorrect_answers = [
# i.answer.lower() for i in question.incorrect_answers.all()
# ]
unmarked_user_answers = question.GetUnmarkedAnswers()
if request.method == "POST":
form = MarkLongQuestionForm(request.POST)
# *******************************
# TODO: convert to JSON request
# *******************************
if form.is_valid():
# If skip button is pressed skip the question without marking
if "skip" in request.POST:
return redirect("longs:mark", pk=pk, sk=n + 1)
cd = form.cleaned_data
# correct = cd.get("correct")
# half_correct = cd.get("half_correct")
# incorrect = cd.get("incorrect")
marked_answers = json.loads(cd.get("marked_answers"))
## This is mildly dangerous as we delete all answers
# question.answers.all().delete()
# question.half_mark_answers.all().delete()
# question.incorrect_answers.all().delete()
# This should probably use json (or something else...)
# ajax.....
for ans in marked_answers["correct"]:
ans = ans.strip()
if ans == "":
continue
a = Answer.objects.filter(
answer__iexact=ans, question_id=question.pk
).first()
if a is None:
a = Answer()
a.question_id = question.pk
a.answer = ans
a.status = Answer.MarkOptions.CORRECT
a.save()
# for ans in half_correct.split("--//--"):
for ans in marked_answers["half-correct"]:
ans = ans.strip()
if ans == "":
continue
a = Answer.objects.filter(
answer__iexact=ans, question_id=question.pk
).first()
if a is None:
a = Answer()
a.question_id = question.pk
a.answer = ans
a.status = Answer.MarkOptions.HALF_MARK
a.save()
for ans in marked_answers["incorrect"]:
ans = ans.strip()
if ans == "":
continue
a = Answer.objects.filter(
answer__iexact=ans, question_id=question.pk
).first()
if a is None:
a = Answer()
a.question_id = question.pk
a.answer = ans
a.status = Answer.MarkOptions.INCORRECT
a.save()
# answer = form.save(commit=False)
# answer.user = request.user
# answer.question = question
# answer.published_date = timezone.now()
# answer.save()
if "next" in request.POST:
return redirect("longs:mark", pk=pk, sk=n + 1)
elif "previous" in request.POST:
return redirect("longs:mark", pk=pk, sk=n - 1)
# Reset user answers (relies on the functional javascript)
unmarked_user_answers = set()
else:
form = MarkLongQuestionForm()
correct_answers = question.answers.filter(status=Answer.MarkOptions.CORRECT)
half_mark_answers = question.answers.filter(status=Answer.MarkOptions.HALF_MARK)
incorrect_answers = question.answers.filter(status=Answer.MarkOptions.INCORRECT)
user_answers = question.cid_user_answers.all()
return render(
request,
"longs/mark.html",
{
"exam": exam,
"form": form,
"question": question,
"question_details": question_details,
"user_answers": unmarked_user_answers,
"correct_answers": correct_answers,
"half_mark_answers": half_mark_answers,
"incorrect_answers": incorrect_answers,
"user_answers": user_answers,
},
)