|
|
|
@@ -1,3 +1,4 @@
|
|
|
|
|
from django.forms.models import model_to_dict
|
|
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
from django import forms
|
|
|
|
@@ -21,15 +22,27 @@ from django.http import Http404, JsonResponse
|
|
|
|
|
|
|
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
|
|
|
|
|
|
|
|
from .forms import AnatomyAnswerForm, AnswerFormSet, AnswerUpdateFormSet, BodyPartForm, ExaminationForm, MarkAnatomyQuestionForm, AnatomyQuestionForm, StructureForm
|
|
|
|
|
from .forms import (
|
|
|
|
|
AnatomyAnswerForm,
|
|
|
|
|
AnswerFormSet,
|
|
|
|
|
AnswerUpdateFormSet,
|
|
|
|
|
BodyPartForm,
|
|
|
|
|
ExaminationForm,
|
|
|
|
|
MarkAnatomyQuestionForm,
|
|
|
|
|
AnatomyQuestionForm,
|
|
|
|
|
StructureForm,
|
|
|
|
|
)
|
|
|
|
|
from .models import (
|
|
|
|
|
AnatomyQuestion, BodyPart,
|
|
|
|
|
CidUserAnswer, Examination, Structure,
|
|
|
|
|
AnatomyQuestion,
|
|
|
|
|
BodyPart,
|
|
|
|
|
CidUserAnswer,
|
|
|
|
|
Examination,
|
|
|
|
|
Structure,
|
|
|
|
|
UserAnswer,
|
|
|
|
|
Exam,
|
|
|
|
|
Answer,
|
|
|
|
|
#HalfMarkAnswers,
|
|
|
|
|
#IncorrectAnswers,
|
|
|
|
|
# HalfMarkAnswers,
|
|
|
|
|
# IncorrectAnswers,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from .tables import AnatomyQuestionTable
|
|
|
|
@@ -60,8 +73,7 @@ def image_as_base64(image_file):
|
|
|
|
|
# encoded_string = base64.b64encode(img_f.read())
|
|
|
|
|
encoded_string = base64.b64encode(image_file.file.read())
|
|
|
|
|
mimetype, enc = mimetypes.guess_type(image_file.path)
|
|
|
|
|
return "data:image/{};base64,{}".format(mimetype,
|
|
|
|
|
encoded_string.decode("utf-8"))
|
|
|
|
|
return "data:image/{};base64,{}".format(mimetype, encoded_string.decode("utf-8"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from django.template.defaulttags import register
|
|
|
|
@@ -81,8 +93,7 @@ def user_is_admin(user):
|
|
|
|
|
|
|
|
|
|
def question_list(request):
|
|
|
|
|
questions = AnatomyQuestion.objects.all()
|
|
|
|
|
return render(request, "anatomy/question_list.html",
|
|
|
|
|
{"questions": questions})
|
|
|
|
|
return render(request, "anatomy/question_list.html", {"questions": questions})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@@ -94,15 +105,15 @@ def index(request):
|
|
|
|
|
@login_required
|
|
|
|
|
def question_detail(request, pk):
|
|
|
|
|
question = get_object_or_404(AnatomyQuestion, pk=pk)
|
|
|
|
|
return render(request, "anatomy/question_detail.html",
|
|
|
|
|
{"question": question})
|
|
|
|
|
return render(request, "anatomy/question_detail.html", {"question": question})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def answer_question(request, pk):
|
|
|
|
|
question = get_object_or_404(AnatomyQuestion, pk=pk)
|
|
|
|
|
answer = question.user_answers.filter(
|
|
|
|
|
user=request.user).first() # .filter(user=User)
|
|
|
|
|
user=request.user
|
|
|
|
|
).first() # .filter(user=User)
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if answer:
|
|
|
|
|
form = AnatomyAnswerForm(request.POST, instance=answer)
|
|
|
|
@@ -117,10 +128,9 @@ def answer_question(request, pk):
|
|
|
|
|
return redirect("question_detail", pk=pk)
|
|
|
|
|
else:
|
|
|
|
|
form = AnatomyAnswerForm(instance=answer)
|
|
|
|
|
return render(request, "anatomy/answer_question.html", {
|
|
|
|
|
"form": form,
|
|
|
|
|
"question": question
|
|
|
|
|
})
|
|
|
|
|
return render(
|
|
|
|
|
request, "anatomy/answer_question.html", {"form": form, "question": question}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@@ -145,11 +155,7 @@ def exam_overview(request, pk):
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"anatomy/exam_overview.html",
|
|
|
|
|
{
|
|
|
|
|
"exam": exam,
|
|
|
|
|
"questions": questions,
|
|
|
|
|
"question_number": question_number
|
|
|
|
|
},
|
|
|
|
|
{"exam": exam, "questions": questions, "question_number": question_number},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -178,8 +184,9 @@ def exam_take(request, pk, sk):
|
|
|
|
|
|
|
|
|
|
# Get data for flagged
|
|
|
|
|
# answered_questions = UserAnswer.objects.filter(user=request.user).values_list("question__pk", flat=True)
|
|
|
|
|
user_answer_data = UserAnswer.objects.filter(
|
|
|
|
|
user=request.user).values_list("question__pk", "answer", "flagged")
|
|
|
|
|
user_answer_data = UserAnswer.objects.filter(user=request.user).values_list(
|
|
|
|
|
"question__pk", "answer", "flagged"
|
|
|
|
|
)
|
|
|
|
|
# flagged_questions = UserAnswer.objects.filter(user=request.user, flagged=True).values_list("question__pk", flat=True)
|
|
|
|
|
# flagged_questions I= UserAnswer.filter(user=request.user, ).values_list("question__pk", flat=True)
|
|
|
|
|
|
|
|
|
@@ -193,7 +200,8 @@ def exam_take(request, pk, sk):
|
|
|
|
|
|
|
|
|
|
# u = question.user_answers
|
|
|
|
|
answer = question.user_answers.filter(
|
|
|
|
|
user=request.user).first() # .filter(user=User)
|
|
|
|
|
user=request.user
|
|
|
|
|
).first() # .filter(user=User)
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if answer:
|
|
|
|
|
form = AnatomyAnswerForm(request.POST, instance=answer)
|
|
|
|
@@ -238,9 +246,9 @@ def flag_question(request):
|
|
|
|
|
|
|
|
|
|
def loadJsonAnswer(answer):
|
|
|
|
|
# As access is not restricted make sure the data appears valid
|
|
|
|
|
if (not isinstance(answer["cid"],
|
|
|
|
|
int)) or (not isinstance(answer["eid"], int) or
|
|
|
|
|
(not isinstance(answer["ans"], str))):
|
|
|
|
|
if (not isinstance(answer["cid"], int)) or (
|
|
|
|
|
not isinstance(answer["eid"], int) or (not isinstance(answer["ans"], str))
|
|
|
|
|
):
|
|
|
|
|
return JsonResponse({"success": False})
|
|
|
|
|
|
|
|
|
|
# The model should catch invalid data but this should be less intensive
|
|
|
|
@@ -251,14 +259,13 @@ def loadJsonAnswer(answer):
|
|
|
|
|
exam = get_object_or_404(Exam, pk=answer["eid"])
|
|
|
|
|
|
|
|
|
|
if not exam.active:
|
|
|
|
|
return JsonResponse({
|
|
|
|
|
"success": False,
|
|
|
|
|
"error": "No active exam: {}".format(answer["eid"])
|
|
|
|
|
})
|
|
|
|
|
return JsonResponse(
|
|
|
|
|
{"success": False, "error": "No active exam: {}".format(answer["eid"])}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
exiting_answers = CidUserAnswer.objects.filter(question__id=answer["qid"],
|
|
|
|
|
exam__id=answer["eid"],
|
|
|
|
|
cid=answer["cid"])
|
|
|
|
|
exiting_answers = CidUserAnswer.objects.filter(
|
|
|
|
|
question__id=answer["qid"], exam__id=answer["eid"], cid=answer["cid"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not exiting_answers:
|
|
|
|
|
ans = CidUserAnswer(answer=answer["ans"], cid=answer["cid"])
|
|
|
|
@@ -303,20 +310,19 @@ def postExamAnswers(request):
|
|
|
|
|
# return render(request, "anatomy/exam.html", {"exam" : exam, "question" : question})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#@user_passes_test(user_is_admin, login_url="/accounts/login")
|
|
|
|
|
# @user_passes_test(user_is_admin, login_url="/accounts/login")
|
|
|
|
|
@login_required
|
|
|
|
|
def mark_overview(request, pk):
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
|
questions = exam.exam_questions.all()
|
|
|
|
|
|
|
|
|
|
return render(request, "anatomy/mark_overview.html", {
|
|
|
|
|
"exam": exam,
|
|
|
|
|
"questions": questions
|
|
|
|
|
})
|
|
|
|
|
return render(
|
|
|
|
|
request, "anatomy/mark_overview.html", {"exam": exam, "questions": questions}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#@user_passes_test(user_is_admin, login_url="/accounts/login")
|
|
|
|
|
# @user_passes_test(user_is_admin, login_url="/accounts/login")
|
|
|
|
|
@login_required
|
|
|
|
|
def mark(request, pk, sk):
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
@@ -342,13 +348,13 @@ def mark(request, pk, sk):
|
|
|
|
|
|
|
|
|
|
marked_answers_set = set(answers_dict.keys())
|
|
|
|
|
|
|
|
|
|
#correct_answers = [i.answer.lower() for i in question.answers.all()]
|
|
|
|
|
#half_correct_answers = [
|
|
|
|
|
# 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 = [
|
|
|
|
|
# ]
|
|
|
|
|
# incorrect_answers = [
|
|
|
|
|
# i.answer.lower() for i in question.incorrect_answers.all()
|
|
|
|
|
#]
|
|
|
|
|
# ]
|
|
|
|
|
|
|
|
|
|
unmarked_user_answers = question.GetUnmarkedAnswers()
|
|
|
|
|
|
|
|
|
@@ -366,9 +372,9 @@ def mark(request, pk, sk):
|
|
|
|
|
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()
|
|
|
|
|
# question.answers.all().delete()
|
|
|
|
|
# question.half_mark_answers.all().delete()
|
|
|
|
|
# question.incorrect_answers.all().delete()
|
|
|
|
|
|
|
|
|
|
# This should probably use json (or something else...)
|
|
|
|
|
# ajax.....
|
|
|
|
@@ -377,7 +383,9 @@ def mark(request, pk, sk):
|
|
|
|
|
if ans == "":
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
a = Answer.objects.filter(answer__iexact=ans, question_id=question.pk).first()
|
|
|
|
|
a = Answer.objects.filter(
|
|
|
|
|
answer__iexact=ans, question_id=question.pk
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if a is None:
|
|
|
|
|
a = Answer()
|
|
|
|
@@ -387,14 +395,15 @@ def mark(request, pk, sk):
|
|
|
|
|
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()
|
|
|
|
|
a = Answer.objects.filter(
|
|
|
|
|
answer__iexact=ans, question_id=question.pk
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if a is None:
|
|
|
|
|
a = Answer()
|
|
|
|
@@ -409,7 +418,9 @@ def mark(request, pk, sk):
|
|
|
|
|
if ans == "":
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
a = Answer.objects.filter(answer__iexact=ans, question_id=question.pk).first()
|
|
|
|
|
a = Answer.objects.filter(
|
|
|
|
|
answer__iexact=ans, question_id=question.pk
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if a is None:
|
|
|
|
|
a = Answer()
|
|
|
|
@@ -433,9 +444,9 @@ def mark(request, pk, sk):
|
|
|
|
|
else:
|
|
|
|
|
form = MarkAnatomyQuestionForm()
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
@@ -452,32 +463,37 @@ def mark(request, pk, sk):
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exam_toggle_results_published(request, pk):
|
|
|
|
|
if request.is_ajax() and request.method=='POST':
|
|
|
|
|
if request.is_ajax() and request.method == "POST":
|
|
|
|
|
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
|
exam.publish_results = True if request.POST.get('publish_results') == 'true' else False
|
|
|
|
|
exam.publish_results = (
|
|
|
|
|
True if request.POST.get("publish_results") == "true" else False
|
|
|
|
|
)
|
|
|
|
|
exam.save()
|
|
|
|
|
data = {'status':'success', 'publish_results':exam.publish_results}
|
|
|
|
|
data = {"status": "success", "publish_results": exam.publish_results}
|
|
|
|
|
return JsonResponse(data, status=200)
|
|
|
|
|
else:
|
|
|
|
|
data = {'status':'error'}
|
|
|
|
|
data = {"status": "error"}
|
|
|
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exam_toggle_active(request, pk):
|
|
|
|
|
if request.is_ajax() and request.method=='POST':
|
|
|
|
|
if request.is_ajax() and request.method == "POST":
|
|
|
|
|
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
|
exam.active = True if request.POST.get('active') == 'true' else False
|
|
|
|
|
exam.active = True if request.POST.get("active") == "true" else False
|
|
|
|
|
exam.save()
|
|
|
|
|
data = {'status':'success', 'active':exam.active}
|
|
|
|
|
data = {"status": "success", "active": exam.active}
|
|
|
|
|
return JsonResponse(data, status=200)
|
|
|
|
|
else:
|
|
|
|
|
data = {'status':'error'}
|
|
|
|
|
data = {"status": "error"}
|
|
|
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def active_exams(request):
|
|
|
|
|
exams = Exam.objects.all()
|
|
|
|
|
print(exams)
|
|
|
|
@@ -486,28 +502,28 @@ def active_exams(request):
|
|
|
|
|
|
|
|
|
|
for exam in exams:
|
|
|
|
|
if exam.active:
|
|
|
|
|
active_exams["exams"].append({
|
|
|
|
|
"name":
|
|
|
|
|
exam.get_exam_name(),
|
|
|
|
|
"url":
|
|
|
|
|
request.build_absolute_uri(exam.get_json_url()),
|
|
|
|
|
})
|
|
|
|
|
active_exams["exams"].append(
|
|
|
|
|
{
|
|
|
|
|
"name": exam.get_exam_name(),
|
|
|
|
|
"url": request.build_absolute_uri(exam.get_json_url()),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return JsonResponse(active_exams)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def question_save_annotation(request, pk):
|
|
|
|
|
if request.is_ajax() and request.method=='POST':
|
|
|
|
|
if request.is_ajax() and request.method == "POST":
|
|
|
|
|
question = get_object_or_404(AnatomyQuestion, pk=pk)
|
|
|
|
|
|
|
|
|
|
question.image_annotations = request.POST.get("annotation")
|
|
|
|
|
print(question.image_annotations)
|
|
|
|
|
|
|
|
|
|
question.save()
|
|
|
|
|
data = {'status':'success'}
|
|
|
|
|
data = {"status": "success"}
|
|
|
|
|
return JsonResponse(data, status=200)
|
|
|
|
|
else:
|
|
|
|
|
data = {'status':'error'}
|
|
|
|
|
data = {"status": "error"}
|
|
|
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -524,7 +540,6 @@ def exam_json(request, pk):
|
|
|
|
|
exam_json_cache["cached"] = True
|
|
|
|
|
return JsonResponse(exam_json_cache)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
questions = exam.exam_questions.all()
|
|
|
|
|
|
|
|
|
|
exam_questions = defaultdict(dict)
|
|
|
|
@@ -554,6 +569,7 @@ def exam_json(request, pk):
|
|
|
|
|
|
|
|
|
|
return JsonResponse(exam_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def exam_json_recreate(request, pk):
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
@@ -563,14 +579,18 @@ def exam_json_recreate(request, pk):
|
|
|
|
|
|
|
|
|
|
return redirect("anatomy:exam_overview", pk=pk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def exam_scores_cid(request, pk):
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
|
questions = exam.exam_questions.all()
|
|
|
|
|
|
|
|
|
|
cids = (CidUserAnswer.objects.filter(question__in=questions).values_list(
|
|
|
|
|
"cid", flat=True).distinct())
|
|
|
|
|
cids = (
|
|
|
|
|
CidUserAnswer.objects.filter(question__in=questions)
|
|
|
|
|
.values_list("cid", flat=True)
|
|
|
|
|
.distinct()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
user_answers_and_marks = defaultdict(list)
|
|
|
|
|
user_answers_marks = defaultdict(list)
|
|
|
|
@@ -607,7 +627,9 @@ def exam_scores_cid(request, pk):
|
|
|
|
|
|
|
|
|
|
user_scores = {}
|
|
|
|
|
for user in user_answers_marks:
|
|
|
|
|
user_scores[user] = sum([i for i in user_answers_marks[user] if i != "unmarked"])
|
|
|
|
|
user_scores[user] = sum(
|
|
|
|
|
[i for i in user_answers_marks[user] if i != "unmarked"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
user_scores_list = list(user_scores.values())
|
|
|
|
|
|
|
|
|
@@ -625,7 +647,14 @@ def exam_scores_cid(request, pk):
|
|
|
|
|
mode = "No unique mode"
|
|
|
|
|
|
|
|
|
|
df = user_scores_list
|
|
|
|
|
fig = px.histogram(df, x=0, title="Distribution of scores", labels={"0": "Score"}, height=400, width=600)
|
|
|
|
|
fig = px.histogram(
|
|
|
|
|
df,
|
|
|
|
|
x=0,
|
|
|
|
|
title="Distribution of scores",
|
|
|
|
|
labels={"0": "Score"},
|
|
|
|
|
height=400,
|
|
|
|
|
width=600,
|
|
|
|
|
)
|
|
|
|
|
fig_html = fig.to_html()
|
|
|
|
|
|
|
|
|
|
total = len(questions)
|
|
|
|
@@ -651,6 +680,7 @@ def exam_scores_cid(request, pk):
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exam_scores_cid_user(request, pk, sk):
|
|
|
|
|
exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
@@ -663,7 +693,6 @@ def exam_scores_cid_user(request, pk, sk):
|
|
|
|
|
answers_marks = []
|
|
|
|
|
answers = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for q in questions:
|
|
|
|
|
# Get user answer
|
|
|
|
|
user_answer = q.cid_user_answers.filter(cid=cid).first()
|
|
|
|
@@ -712,68 +741,70 @@ def exam_scores_cid_user(request, pk, sk):
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cid_selector(request):
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"anatomy/cid_selector.html",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#@login_required
|
|
|
|
|
#def exam_scores(request, pk):
|
|
|
|
|
#exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
|
#questions = exam.exam_questions.all()
|
|
|
|
|
# @login_required
|
|
|
|
|
# def exam_scores(request, pk):
|
|
|
|
|
# exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
|
|
|
|
|
#users = (CidUserAnswer.objects.filter(
|
|
|
|
|
#question__in=questions).values_list("user").distinct())
|
|
|
|
|
# questions = exam.exam_questions.all()
|
|
|
|
|
|
|
|
|
|
#user_answers_and_marks = defaultdict(list)
|
|
|
|
|
#user_answers_marks = defaultdict(list)
|
|
|
|
|
#user_answers = defaultdict(list)
|
|
|
|
|
#user_names = {}
|
|
|
|
|
# users = (CidUserAnswer.objects.filter(
|
|
|
|
|
# question__in=questions).values_list("user").distinct())
|
|
|
|
|
|
|
|
|
|
#by_question = defaultdict(list)
|
|
|
|
|
# user_answers_and_marks = defaultdict(list)
|
|
|
|
|
# user_answers_marks = defaultdict(list)
|
|
|
|
|
# user_answers = defaultdict(list)
|
|
|
|
|
# user_names = {}
|
|
|
|
|
|
|
|
|
|
#for u in users:
|
|
|
|
|
## Convoluted (probably...)
|
|
|
|
|
#user_names[u] = User.objects.filter(pk=u[0]).first().get_username()
|
|
|
|
|
#for q in questions:
|
|
|
|
|
#user_answer = q.user_answers.filter(user__in=u).first()
|
|
|
|
|
#if not user_answer:
|
|
|
|
|
#user_answers_marks[u].append(0)
|
|
|
|
|
#user_answers[u].append("")
|
|
|
|
|
#continue
|
|
|
|
|
# by_question = defaultdict(list)
|
|
|
|
|
|
|
|
|
|
#ans = user_answer.answer
|
|
|
|
|
#answer_score = user_answer.get_answer_score()
|
|
|
|
|
#user_answers[u].append(ans)
|
|
|
|
|
#user_answers_marks[u].append(answer_score)
|
|
|
|
|
#user_answers_and_marks[u].append((ans, answer_score))
|
|
|
|
|
# for u in users:
|
|
|
|
|
## Convoluted (probably...)
|
|
|
|
|
# user_names[u] = User.objects.filter(pk=u[0]).first().get_username()
|
|
|
|
|
# for q in questions:
|
|
|
|
|
# user_answer = q.user_answers.filter(user__in=u).first()
|
|
|
|
|
# if not user_answer:
|
|
|
|
|
# user_answers_marks[u].append(0)
|
|
|
|
|
# user_answers[u].append("")
|
|
|
|
|
# continue
|
|
|
|
|
|
|
|
|
|
#by_question[q].append(ans)
|
|
|
|
|
# ans = user_answer.answer
|
|
|
|
|
# answer_score = user_answer.get_answer_score()
|
|
|
|
|
# user_answers[u].append(ans)
|
|
|
|
|
# user_answers_marks[u].append(answer_score)
|
|
|
|
|
# user_answers_and_marks[u].append((ans, answer_score))
|
|
|
|
|
|
|
|
|
|
#user_scores = {}
|
|
|
|
|
#for user in user_answers_marks:
|
|
|
|
|
#user_scores[user] = sum(user_answers_marks[user])
|
|
|
|
|
# by_question[q].append(ans)
|
|
|
|
|
|
|
|
|
|
#total = len(questions)
|
|
|
|
|
# user_scores = {}
|
|
|
|
|
# for user in user_answers_marks:
|
|
|
|
|
# user_scores[user] = sum(user_answers_marks[user])
|
|
|
|
|
|
|
|
|
|
#return render(
|
|
|
|
|
#request,
|
|
|
|
|
#"anatomy/exam_scores.html",
|
|
|
|
|
#{
|
|
|
|
|
#"exam": exam,
|
|
|
|
|
#"questions": questions,
|
|
|
|
|
#"by_question": by_question,
|
|
|
|
|
#"user_answers": dict(user_answers),
|
|
|
|
|
#"user_answers_marks": dict(user_answers_marks),
|
|
|
|
|
#"user_scores": user_scores,
|
|
|
|
|
#"user_names": user_names,
|
|
|
|
|
#"user_answers_and_marks": user_answers_and_marks,
|
|
|
|
|
#},
|
|
|
|
|
#)
|
|
|
|
|
# total = len(questions)
|
|
|
|
|
|
|
|
|
|
#def cid_scores(request, pk):
|
|
|
|
|
# return render(
|
|
|
|
|
# request,
|
|
|
|
|
# "anatomy/exam_scores.html",
|
|
|
|
|
# {
|
|
|
|
|
# "exam": exam,
|
|
|
|
|
# "questions": questions,
|
|
|
|
|
# "by_question": by_question,
|
|
|
|
|
# "user_answers": dict(user_answers),
|
|
|
|
|
# "user_answers_marks": dict(user_answers_marks),
|
|
|
|
|
# "user_scores": user_scores,
|
|
|
|
|
# "user_names": user_names,
|
|
|
|
|
# "user_answers_and_marks": user_answers_and_marks,
|
|
|
|
|
# },
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# def cid_scores(request, pk):
|
|
|
|
|
# #exam = get_object_or_404(Exam, pk=pk)
|
|
|
|
|
#
|
|
|
|
|
# # TODO:Need some kind of test for cid
|
|
|
|
@@ -787,7 +818,7 @@ def cid_selector(request):
|
|
|
|
|
# if not answers:
|
|
|
|
|
# raise Http404("cid not found")
|
|
|
|
|
#
|
|
|
|
|
# exam_ids = answers.values_list("exam").distinct()
|
|
|
|
|
# exam_ids = answers.values_list("exam").distinct()
|
|
|
|
|
#
|
|
|
|
|
# exams = Exam.objects.filter(id__in=exam_ids)
|
|
|
|
|
#
|
|
|
|
@@ -801,6 +832,7 @@ def cid_selector(request):
|
|
|
|
|
# },
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AnatomyQuestionCreateBase(LoginRequiredMixin, CreateView):
|
|
|
|
|
model = AnatomyQuestion
|
|
|
|
|
form_class = AnatomyQuestionForm
|
|
|
|
@@ -808,10 +840,11 @@ class AnatomyQuestionCreateBase(LoginRequiredMixin, CreateView):
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super(AnatomyQuestionCreateBase, self).get_context_data(**kwargs)
|
|
|
|
|
if self.request.POST:
|
|
|
|
|
context['answer_formset'] = AnswerFormSet(self.request.POST,
|
|
|
|
|
self.request.FILES)
|
|
|
|
|
context["answer_formset"] = AnswerFormSet(
|
|
|
|
|
self.request.POST, self.request.FILES
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
context['answer_formset'] = AnswerFormSet()
|
|
|
|
|
context["answer_formset"] = AnswerFormSet()
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
@@ -822,7 +855,7 @@ class AnatomyQuestionCreateBase(LoginRequiredMixin, CreateView):
|
|
|
|
|
form.instance.author.add(self.request.user.id)
|
|
|
|
|
|
|
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
|
formset = context['answer_formset']
|
|
|
|
|
formset = context["answer_formset"]
|
|
|
|
|
|
|
|
|
|
if formset.is_valid():
|
|
|
|
|
response = super().form_valid(form)
|
|
|
|
@@ -833,19 +866,23 @@ class AnatomyQuestionCreateBase(LoginRequiredMixin, CreateView):
|
|
|
|
|
return response
|
|
|
|
|
# else we redirect to the clone url
|
|
|
|
|
else:
|
|
|
|
|
return redirect('AnatomyQuestions:AnatomyQuestion_clone', pk=self.object.pk)
|
|
|
|
|
return redirect(
|
|
|
|
|
"anatomy:question_clone", pk=self.object.pk
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#@login_required
|
|
|
|
|
# @login_required
|
|
|
|
|
class AnatomyQuestionCreate(AnatomyQuestionCreateBase):
|
|
|
|
|
|
|
|
|
|
#initial = {'laterality': AnatomyQuestion.NONE}
|
|
|
|
|
# initial = {'laterality': AnatomyQuestion.NONE}
|
|
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # There has to be a better way...
|
|
|
|
|
# try:
|
|
|
|
|
# s = (i.pk for i in self.request.user.AnatomyQuestion_default.site.all())
|
|
|
|
@@ -854,19 +891,19 @@ class AnatomyQuestionCreate(AnatomyQuestionCreateBase):
|
|
|
|
|
# pass
|
|
|
|
|
# return self.initial
|
|
|
|
|
#
|
|
|
|
|
# fields = '__all__'
|
|
|
|
|
# #fields = [ 'condition' ]
|
|
|
|
|
# #initial = {'date_of_death': '05/01/2018'}
|
|
|
|
|
# exclude = [ 'created_date', 'published_date' ]
|
|
|
|
|
# fields = '__all__'
|
|
|
|
|
# #fields = [ 'condition' ]
|
|
|
|
|
# #initial = {'date_of_death': '05/01/2018'}
|
|
|
|
|
# exclude = [ 'created_date', 'published_date' ]
|
|
|
|
|
|
|
|
|
|
#self.object = form.save(commit=False)
|
|
|
|
|
#self.object.save()
|
|
|
|
|
# self.object = form.save(commit=False)
|
|
|
|
|
# self.object.save()
|
|
|
|
|
|
|
|
|
|
#form.instance.author.add(self.request.user.id)
|
|
|
|
|
#return super().form_valid(form)
|
|
|
|
|
# form.instance.author.add(self.request.user.id)
|
|
|
|
|
# return super().form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#class AnatomyQuestionUpdate(LoginRequiredMixin, AuthorOrCheckerRequiredMixin,
|
|
|
|
|
# class AnatomyQuestionUpdate(LoginRequiredMixin, AuthorOrCheckerRequiredMixin,
|
|
|
|
|
class AnatomyQuestionUpdate(LoginRequiredMixin, UpdateView):
|
|
|
|
|
model = AnatomyQuestion
|
|
|
|
|
form_class = AnatomyQuestionForm
|
|
|
|
@@ -874,12 +911,12 @@ class AnatomyQuestionUpdate(LoginRequiredMixin, UpdateView):
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super(AnatomyQuestionUpdate, self).get_context_data(**kwargs)
|
|
|
|
|
if self.request.POST:
|
|
|
|
|
context['answer_formset'] = AnswerUpdateFormSet(self.request.POST,
|
|
|
|
|
self.request.FILES,
|
|
|
|
|
instance=self.object)
|
|
|
|
|
context['answer_formset'].full_clean()
|
|
|
|
|
context["answer_formset"] = AnswerUpdateFormSet(
|
|
|
|
|
self.request.POST, self.request.FILES, instance=self.object
|
|
|
|
|
)
|
|
|
|
|
context["answer_formset"].full_clean()
|
|
|
|
|
else:
|
|
|
|
|
context['answer_formset'] = AnswerUpdateFormSet(instance=self.object)
|
|
|
|
|
context["answer_formset"] = AnswerUpdateFormSet(instance=self.object)
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
@@ -890,8 +927,8 @@ class AnatomyQuestionUpdate(LoginRequiredMixin, UpdateView):
|
|
|
|
|
form.instance.author.add(self.request.user.id)
|
|
|
|
|
|
|
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
|
formset = context['answer_formset']
|
|
|
|
|
#logger.debug(formset.is_valid())
|
|
|
|
|
formset = context["answer_formset"]
|
|
|
|
|
# logger.debug(formset.is_valid())
|
|
|
|
|
if formset.is_valid():
|
|
|
|
|
response = super().form_valid(form)
|
|
|
|
|
formset.instance = self.object
|
|
|
|
@@ -900,6 +937,21 @@ class AnatomyQuestionUpdate(LoginRequiredMixin, UpdateView):
|
|
|
|
|
else:
|
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuestionClone(AnatomyQuestionCreateBase):
|
|
|
|
|
"""Clones a existing rapid"""
|
|
|
|
|
# fields = '__all__'
|
|
|
|
|
# #fields = [ 'condition' ]
|
|
|
|
|
# #initial = {'date_of_death': '05/01/2018'}
|
|
|
|
|
# exclude = [ 'created_date', 'published_date' ]
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
# print(self.request)
|
|
|
|
|
old_object = get_object_or_404(AnatomyQuestion, pk=self.kwargs["pk"])
|
|
|
|
|
initial_data = model_to_dict(old_object, exclude=["id"])
|
|
|
|
|
|
|
|
|
|
return initial_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def create_body_part(request):
|
|
|
|
|
form = BodyPartForm(request.POST or None)
|
|
|
|
@@ -907,22 +959,22 @@ def create_body_part(request):
|
|
|
|
|
instance = form.save()
|
|
|
|
|
return HttpResponse(
|
|
|
|
|
'<script>opener.closePopup(window, "%s", "%s", "#id_body_part");</script>'
|
|
|
|
|
% (instance.pk, instance))
|
|
|
|
|
return render(request, "anatomy/create_simple.html", {
|
|
|
|
|
'form': form,
|
|
|
|
|
'name': "BodyPart"
|
|
|
|
|
})
|
|
|
|
|
% (instance.pk, instance)
|
|
|
|
|
)
|
|
|
|
|
return render(
|
|
|
|
|
request, "anatomy/create_simple.html", {"form": form, "name": "BodyPart"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def get_body_part_id(request):
|
|
|
|
|
if request.is_ajax():
|
|
|
|
|
body_part_name = request.GET['body_part_name']
|
|
|
|
|
body_part_name = request.GET["body_part_name"]
|
|
|
|
|
body_part_id = BodyPart.objects.get(name=body_part_name).id
|
|
|
|
|
data = {
|
|
|
|
|
'body_part_id': body_part_id,
|
|
|
|
|
"body_part_id": body_part_id,
|
|
|
|
|
}
|
|
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json')
|
|
|
|
|
return HttpResponse(json.dumps(data), content_type="application/json")
|
|
|
|
|
return HttpResponse("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -933,22 +985,22 @@ def create_examination(request):
|
|
|
|
|
instance = form.save()
|
|
|
|
|
return HttpResponse(
|
|
|
|
|
'<script>opener.closePopup(window, "%s", "%s", "#id_examination");</script>'
|
|
|
|
|
% (instance.pk, instance))
|
|
|
|
|
return render(request, "anatomy/create_simple.html", {
|
|
|
|
|
'form': form,
|
|
|
|
|
'name': "Examination"
|
|
|
|
|
})
|
|
|
|
|
% (instance.pk, instance)
|
|
|
|
|
)
|
|
|
|
|
return render(
|
|
|
|
|
request, "anatomy/create_simple.html", {"form": form, "name": "Examination"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def get_examination_id(request):
|
|
|
|
|
if request.is_ajax():
|
|
|
|
|
examination_name = request.GET['examination_name']
|
|
|
|
|
examination_name = request.GET["examination_name"]
|
|
|
|
|
examination_id = Examination.objects.get(name=examination_name).id
|
|
|
|
|
data = {
|
|
|
|
|
'examination_id': examination_id,
|
|
|
|
|
"examination_id": examination_id,
|
|
|
|
|
}
|
|
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json')
|
|
|
|
|
return HttpResponse(json.dumps(data), content_type="application/json")
|
|
|
|
|
return HttpResponse("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -959,24 +1011,25 @@ def create_structure(request):
|
|
|
|
|
instance = form.save()
|
|
|
|
|
return HttpResponse(
|
|
|
|
|
'<script>opener.closePopup(window, "%s", "%s", "#id_structure");</script>'
|
|
|
|
|
% (instance.pk, instance))
|
|
|
|
|
return render(request, "anatomy/create_simple.html", {
|
|
|
|
|
'form': form,
|
|
|
|
|
'name': "Structure"
|
|
|
|
|
})
|
|
|
|
|
% (instance.pk, instance)
|
|
|
|
|
)
|
|
|
|
|
return render(
|
|
|
|
|
request, "anatomy/create_simple.html", {"form": form, "name": "Structure"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def get_structure_id(request):
|
|
|
|
|
if request.is_ajax():
|
|
|
|
|
structure_name = request.GET['structure_name']
|
|
|
|
|
structure_name = request.GET["structure_name"]
|
|
|
|
|
structure_id = Structure.objects.get(name=structure_name).id
|
|
|
|
|
data = {
|
|
|
|
|
'structure_id': structure_id,
|
|
|
|
|
"structure_id": structure_id,
|
|
|
|
|
}
|
|
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json')
|
|
|
|
|
return HttpResponse(json.dumps(data), content_type="application/json")
|
|
|
|
|
return HttpResponse("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AnatomyQuestionView(SingleTableMixin, FilterView):
|
|
|
|
|
model = AnatomyQuestion
|
|
|
|
|
table_class = AnatomyQuestionTable
|
|
|
|
|