continue unifying views
This commit is contained in:
@@ -15,6 +15,9 @@ import string
|
||||
|
||||
from generic.models import Examination, ExamBase
|
||||
|
||||
from collections import defaultdict
|
||||
from helpers.images import image_as_base64
|
||||
|
||||
image_storage = FileSystemStorage(
|
||||
# Physical file location ROOT
|
||||
location=u"{0}/anatomy/".format(settings.MEDIA_ROOT),
|
||||
@@ -273,6 +276,38 @@ class Exam(ExamBase):
|
||||
help_text="Exam time limit (in seconds)", default=5400
|
||||
)
|
||||
|
||||
def get_exam_json(self):
|
||||
questions = self.exam_questions.all()
|
||||
|
||||
exam_questions = defaultdict(dict)
|
||||
|
||||
exam_order = []
|
||||
|
||||
for q in questions:
|
||||
exam_order.append(q.id)
|
||||
|
||||
exam_questions[q.id] = {
|
||||
"title": "{}".format(q.description),
|
||||
"question": str(q.question_type),
|
||||
"images": [image_as_base64(q.image)],
|
||||
"annotations": [str(q.image_annotations)],
|
||||
"type": "anatomy",
|
||||
}
|
||||
|
||||
exam_json = {
|
||||
"eid": "anatomy/{}".format(self.id),
|
||||
"cached": False,
|
||||
"exam_type": "anatomy",
|
||||
"exam_name": self.name,
|
||||
"exam_mode": True,
|
||||
"exam_order": exam_order,
|
||||
"questions": exam_questions,
|
||||
}
|
||||
|
||||
if self.time_limit:
|
||||
exam_json["exam_time"] = self.time_limit
|
||||
|
||||
return exam_json
|
||||
|
||||
|
||||
# class UserAnswer(models.Model):
|
||||
|
||||
+6
-6
@@ -5,7 +5,7 @@ app_name = "anatomy"
|
||||
|
||||
urlpatterns = [
|
||||
# path('', views.question_list, name='question_list'),
|
||||
path("", views.index, name="index"),
|
||||
path("", views.AnatomyExamViews.index, name="index"),
|
||||
path("question/", views.AnatomyQuestionView.as_view(), name="anatomy_question_view"),
|
||||
path("question/<int:pk>/", views.question_detail, name="question_detail"),
|
||||
path("question/create/", views.AnatomyQuestionCreate.as_view(), name="anatomy_question_create"),
|
||||
@@ -16,21 +16,21 @@ urlpatterns = [
|
||||
name="answer_question"),
|
||||
path("question/<int:pk>/clone", views.QuestionClone.as_view(), name="question_clone"),
|
||||
path("exam/<int:pk>/<int:sk>/mark", views.mark, name="mark"),
|
||||
path("exam/<int:pk>/mark", views.mark_overview, name="mark_overview"),
|
||||
path("exam/<int:pk>/mark", views.AnatomyExamViews.mark_overview, name="mark_overview"),
|
||||
path("exam/<int:pk>/<int:sk>/", views.exam_take, name="exam_take"),
|
||||
path("exam/<int:pk>/question/<int:sk>/", views.exam_question_detail, name="exam_question_detail"),
|
||||
path("exam/<int:pk>/question/<int:sk>/", views.AnatomyExamViews.exam_question_detail, name="exam_question_detail"),
|
||||
path("exam/<int:pk>/", views.AnatomyExamViews.exam_overview, name="exam_overview"),
|
||||
path("exam/<int:pk>/json_edit", views.exam_json_edit, name="exam_json_edit"),
|
||||
path("exam/<int:pk>/json_edit", views.AnatomyExamViews.exam_json_edit, name="exam_json_edit"),
|
||||
path("exam/<int:pk>/scores", views.exam_scores_cid,
|
||||
name="exam_scores_cid"),
|
||||
path("exam/<int:pk>/scores/<int:sk>/", views.exam_scores_cid_user,
|
||||
name="exam_scores_cid_user"),
|
||||
path("exam/<int:pk>/toggle_active", views.AnatomyExamViews.exam_toggle_active, name="exam_toggle_active"),
|
||||
path("exam/<int:pk>/toggle_results_published", views.AnatomyExamViews.exam_toggle_results_published, name="exam_toggle_results_published"),
|
||||
path("exam/submit", views.postExamAnswers, name="exam_answers_submit"),
|
||||
path("exam/submit", views.AnatomyExamViews.postExamAnswers, name="exam_answers_submit"),
|
||||
path("exam/", views.AnatomyExamViews.exam_list, name="exam_list"),
|
||||
path("exam/json/", views.AnatomyExamViews.active_exams, name="active_exams"),
|
||||
path("exam/json/<int:pk>", views.exam_json, name="exam_json"),
|
||||
path("exam/json/<int:pk>", views.AnatomyExamViews.exam_json, name="exam_json"),
|
||||
path("exam/json/<int:pk>/recreate", views.AnatomyExamViews.exam_json_recreate, name="exam_json_recreate"),
|
||||
#path("cid/<int:pk>", views.cid_scores, name="cid_scores"),
|
||||
#path("cid/", views.cid_selector, name="cid_selector"),
|
||||
|
||||
+25
-205
@@ -75,17 +75,12 @@ def user_is_admin(user):
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def question_list(request):
|
||||
questions = AnatomyQuestion.objects.all()
|
||||
return render(request, "anatomy/question_list.html", {"questions": questions})
|
||||
|
||||
|
||||
@login_required
|
||||
def index(request):
|
||||
exams = Exam.objects.all()
|
||||
return render(request, "anatomy/index.html", {"exams": exams})
|
||||
|
||||
|
||||
@login_required
|
||||
def question_detail(request, pk):
|
||||
question = get_object_or_404(AnatomyQuestion, pk=pk)
|
||||
@@ -93,38 +88,6 @@ def question_detail(request, pk):
|
||||
return render(request, "anatomy/question_detail.html", {"question": question})
|
||||
|
||||
|
||||
@login_required
|
||||
def exam_question_detail(request, pk, sk):
|
||||
exam = get_object_or_404(Exam, pk=pk)
|
||||
|
||||
question = exam.exam_questions.all()[sk]
|
||||
|
||||
exam_length = len(exam.exam_questions.all())
|
||||
|
||||
pos = exam.get_question_index(question) + 1
|
||||
|
||||
print(question.exams.through.sort_value)
|
||||
|
||||
previous = -1
|
||||
if sk > 0:
|
||||
previous = sk - 1
|
||||
next = sk + 1
|
||||
if sk == exam_length - 1:
|
||||
next = False
|
||||
|
||||
return render(
|
||||
request,
|
||||
"anatomy/question_detail.html",
|
||||
{
|
||||
"question": question,
|
||||
"exam": exam,
|
||||
"next": next,
|
||||
"previous": previous,
|
||||
"exam_length": exam_length,
|
||||
"pos": pos,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def answer_question(request, pk):
|
||||
@@ -283,63 +246,28 @@ def loadJsonAnswer(answer):
|
||||
return True
|
||||
|
||||
|
||||
def exam_json_edit(request, pk):
|
||||
if request.is_ajax() and request.method == "POST":
|
||||
|
||||
exam = get_object_or_404(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)
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
def postExamAnswers(request):
|
||||
if request.is_ajax and request.method == "POST":
|
||||
|
||||
n = 0
|
||||
# horrible but it works
|
||||
#for k in request.POST.dict():
|
||||
|
||||
for answer in json.loads(request.POST.get("answers")):
|
||||
ret = loadJsonAnswer(answer)
|
||||
|
||||
if ret is not True:
|
||||
return 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"})
|
||||
|
||||
# return render(request, "anatomy/exam.html", {"exam" : exam, "question" : question})
|
||||
|
||||
|
||||
# @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}
|
||||
)
|
||||
#@csrf_exempt
|
||||
#def postExamAnswers(request):
|
||||
# if request.is_ajax and request.method == "POST":
|
||||
#
|
||||
# n = 0
|
||||
# # horrible but it works
|
||||
# #for k in request.POST.dict():
|
||||
#
|
||||
# for answer in json.loads(request.POST.get("answers")):
|
||||
# ret = loadJsonAnswer(answer)
|
||||
#
|
||||
# if ret is not True:
|
||||
# return 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"})
|
||||
#
|
||||
# # return render(request, "anatomy/exam.html", {"exam" : exam, "question" : question})
|
||||
|
||||
|
||||
# @user_passes_test(user_is_admin, login_url="/accounts/login")
|
||||
@@ -487,116 +415,6 @@ def mark(request, pk, sk):
|
||||
},
|
||||
)
|
||||
|
||||
AnatomyExamViews = ExamViews(Exam, "anatomy", "anatomy")
|
||||
|
||||
# def exam_toggle_results_published(request, pk):
|
||||
# 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.save()
|
||||
# data = {"status": "success", "publish_results": exam.publish_results}
|
||||
# return JsonResponse(data, status=200)
|
||||
# else:
|
||||
# data = {"status": "error"}
|
||||
# return JsonResponse(data, status=400)
|
||||
#
|
||||
#
|
||||
# def exam_toggle_active(request, pk):
|
||||
# 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.save()
|
||||
# data = {"status": "success", "active": exam.active}
|
||||
# return JsonResponse(data, status=200)
|
||||
# else:
|
||||
# data = {"status": "error"}
|
||||
# return JsonResponse(data, status=400)
|
||||
#
|
||||
#
|
||||
# def active_exams(request, json=True):
|
||||
# exams = Exam.objects.all()
|
||||
#
|
||||
# active_exams = {"exams": []}
|
||||
#
|
||||
# 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()),
|
||||
# "type": "anatomy",
|
||||
# }
|
||||
# )
|
||||
#
|
||||
# if json == False:
|
||||
# return active_exams["exams"]
|
||||
#
|
||||
# return JsonResponse(active_exams)
|
||||
#
|
||||
# @login_required
|
||||
# def exam_json_recreate(request, pk):
|
||||
# exam = get_object_or_404(Exam, pk=pk)
|
||||
#
|
||||
# exam.recreate_json = True
|
||||
# exam.save()
|
||||
#
|
||||
# return redirect("anatomy:exam_overview", pk=pk)
|
||||
|
||||
def exam_json(request, pk):
|
||||
|
||||
exam = get_object_or_404(Exam, pk=pk)
|
||||
|
||||
if not exam.active:
|
||||
raise Http404("No available exam")
|
||||
|
||||
exam_json_cache = cache.get("anatomy_exam_json_{}".format(pk))
|
||||
|
||||
if exam_json_cache is not None and not exam.recreate_json:
|
||||
exam_json_cache["cached"] = True
|
||||
return JsonResponse(exam_json_cache)
|
||||
|
||||
questions = exam.exam_questions.all()
|
||||
|
||||
exam_questions = defaultdict(dict)
|
||||
|
||||
exam_order = []
|
||||
|
||||
for q in questions:
|
||||
exam_order.append(q.id)
|
||||
|
||||
exam_questions[q.id] = {
|
||||
"title": "{}".format(q.description),
|
||||
"question": str(q.question_type),
|
||||
"images": [image_as_base64(q.image)],
|
||||
"annotations": [str(q.image_annotations)],
|
||||
"type": "anatomy",
|
||||
}
|
||||
|
||||
exam_json = {
|
||||
"eid": "anatomy/{}".format(exam.id),
|
||||
"cached": False,
|
||||
"exam_type": "anatomy",
|
||||
"exam_name": exam.name,
|
||||
"exam_mode": True,
|
||||
"exam_order": exam_order,
|
||||
"questions": exam_questions,
|
||||
}
|
||||
|
||||
if exam.time_limit:
|
||||
exam_json["exam_time"] = exam.time_limit
|
||||
|
||||
exam.recreate_json = False
|
||||
exam.save()
|
||||
|
||||
cache.set("anatomy_exam_json_{}".format(pk), exam_json, 3600)
|
||||
|
||||
return JsonResponse(exam_json)
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -1116,3 +934,5 @@ def question_save_annotation(request, pk):
|
||||
data = {"status": "error"}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
|
||||
AnatomyExamViews = ExamViews(Exam, "anatomy", "anatomy", loadJsonAnswer)
|
||||
Reference in New Issue
Block a user