From 9f73c004f1895c765ca9885f5568044bce9644d8 Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 12 Jan 2021 18:09:00 +0000 Subject: [PATCH] . --- physics/views.py | 86 ++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/physics/views.py b/physics/views.py index 5d1cd759..3d7dcc92 100644 --- a/physics/views.py +++ b/physics/views.py @@ -34,8 +34,7 @@ import plotly.express as px def question_list(request): questions = Question.objects.all() - return render(request, "physics/question_list.html", - {"questions": questions}) + return render(request, "physics/question_list.html", {"questions": questions}) @login_required @@ -47,9 +46,8 @@ def index(request): @login_required def question_detail(request, pk): question = get_object_or_404(Question, pk=pk) - return render(request, "physics/question_detail.html", - {"question": question}) - + return render(request, "physics/question_detail.html", {"question": question}) + @login_required def exam_list(request): @@ -73,40 +71,40 @@ def exam_overview(request, pk): return render( request, "physics/exam_overview.html", - { - "exam": exam, - "questions": questions, - "question_number": question_number - }, + {"exam": exam, "questions": questions, "question_number": question_number}, ) 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) @@ -119,15 +117,18 @@ def active_exams(request): return render(request, "physics/available_exam_list.html", {"exams": active_exams}) - @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) + .order_by("cid") + .values_list("cid", flat=True) + .distinct() + ) cids = cids.sort() @@ -184,7 +185,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() max_score = len(questions) @@ -212,6 +220,7 @@ def exam_scores_cid(request, pk): }, ) + def exam_scores_cid_user(request, pk, sk): exam = get_object_or_404(Exam, pk=pk) @@ -224,7 +233,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() @@ -236,7 +244,7 @@ def exam_scores_cid_user(request, pk, sk): correct_answers = q.get_answers() if not exam.publish_results: - correct_answers = ("*****", "*****", "*****", "*****", "*****") + correct_answers = ("*****", "*****", "*****", "*****", "*****") answer_scores = (0, 0, 0, 0, 0) answers.append(ans) answers_marks.append(answer_scores) @@ -305,9 +313,7 @@ def postExamAnswers(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) - ): + if (not isinstance(answer["cid"], int)) or (not isinstance(answer["eid"], int)): return JsonResponse({"success": False, "error": "invalid"}) # The model should catch invalid data but this should be less intensive @@ -318,10 +324,9 @@ 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"])} + ) questions = defaultdict(dict) @@ -329,11 +334,10 @@ def loadJsonAnswer(answer): questions[q][n] = a pass - for qid in questions: - exiting_answers = CidUserAnswer.objects.filter(question__id=qid, - exam__id=answer["eid"], - cid=answer["cid"]) + exiting_answers = CidUserAnswer.objects.filter( + question__id=qid, exam__id=answer["eid"], cid=answer["cid"] + ) a = questions[qid]["a"] b = questions[qid]["b"] @@ -362,15 +366,3 @@ def loadJsonAnswer(answer): ans.save() return True - - - - - - - - - - - -