From c56413e89696074d5c12c9cbb57895bc18d4461a Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 16 Oct 2021 10:33:26 +0100 Subject: [PATCH] . --- anatomy/views.py | 81 ++++++----------------- rapids/migrations/0033_answermarks.py | 25 +++++++ rapids/models.py | 20 +++++- rapids/templates/rapids/mark.html | 2 +- rapids/views.py | 93 ++++++++------------------- 5 files changed, 92 insertions(+), 129 deletions(-) create mode 100644 rapids/migrations/0033_answermarks.py diff --git a/anatomy/views.py b/anatomy/views.py index 10043f48..8e75e75e 100644 --- a/anatomy/views.py +++ b/anatomy/views.py @@ -337,74 +337,35 @@ def mark(request, pk, sk): return redirect("anatomy: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 + to_run = ( + ("correct", Answer.MarkOptions.CORRECT), + ("half-correct", Answer.MarkOptions.HALF_MARK), + ("incorrect", Answer.MarkOptions.INCORRECT), + ) - a = Answer.objects.filter( - answer__iexact=ans, question_id=question.pk - ).first() + for status_string, status in to_run: + for ans in marked_answers[status_string]: + ans = ans.strip() + if ans == "": + continue - if a is None: - a = Answer() - a.question_id = question.pk - a.answer = ans + a = Answer.objects.filter( + answer__iexact=ans, question_id=question.pk + ).first() - a.status = Answer.MarkOptions.CORRECT - a.save() + if a is None: + a = Answer() + a.question_id = question.pk + a.answer = ans - # 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() + a.status = status + a.save() + if "next" in request.POST: return redirect("anatomy:mark", pk=pk, sk=n + 1) elif "previous" in request.POST: diff --git a/rapids/migrations/0033_answermarks.py b/rapids/migrations/0033_answermarks.py new file mode 100644 index 00000000..d11b11df --- /dev/null +++ b/rapids/migrations/0033_answermarks.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.8 on 2021-10-16 09:24 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('rapids', '0032_exam_double_mark'), + ] + + operations = [ + migrations.CreateModel( + name='AnswerMarks', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('score', models.CharField(choices=[('', 'Unmarked'), ('0', 'Incorrect'), ('1', 'Half mark'), ('2', 'Correct')], max_length=1)), + ('marker', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rapid_marks', to=settings.AUTH_USER_MODEL)), + ('user_answer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='mark', to='rapids.ciduseranswer')), + ], + ), + ] diff --git a/rapids/models.py b/rapids/models.py index 10797fdf..411621d7 100644 --- a/rapids/models.py +++ b/rapids/models.py @@ -661,4 +661,22 @@ class CidUserAnswer(models.Model): mark = 0 else: mark = "unmarked" - return mark \ No newline at end of file + return mark + +@reversion.register +class AnswerMarks(models.Model): + score = models.CharField( + max_length=1, choices=Answer.MarkOptions.choices + ) + + #mark_reason = models.TextField(null=True, blank=True, help_text="Reason for the given mark - not visible to candidates") + + #candidate_feedback = models.TextField(null=True, blank=True, help_text="Feedback for the candidate") + + marker = models.ForeignKey( + settings.AUTH_USER_MODEL, related_name="rapid_marks", on_delete=models.CASCADE + ) + + user_answer = models.ForeignKey( + CidUserAnswer, related_name="mark", on_delete=models.SET_NULL, null=True, blank=True + ) \ No newline at end of file diff --git a/rapids/templates/rapids/mark.html b/rapids/templates/rapids/mark.html index 63accf9d..617c7728 100644 --- a/rapids/templates/rapids/mark.html +++ b/rapids/templates/rapids/mark.html @@ -34,7 +34,7 @@ Region: {{ question.get_regions }}, Abnormalities: {{ question.get_abnormalities
{% csrf_token %} {% if not question.normal %} Click each answer to toggle through marks awarded (as per colour)
- {% if exam_answers_only %} + {% if unmarked_exam_answers_only %} Showing exam answers only (view all) {% else %} Showing all answers (view unmarked exam answers) diff --git a/rapids/views.py b/rapids/views.py index a0c2ff51..0e8a871e 100755 --- a/rapids/views.py +++ b/rapids/views.py @@ -573,15 +573,15 @@ def loadJsonAnswer(answer): def mark_all(request, exam_pk, sk): - return mark(request, exam_pk, sk, exam_answers_only=False) + return mark(request, exam_pk, sk, unmarked_exam_answers_only=False) # @user_passes_test(user_is_admin, login_url="/accounts/login") @login_required -def mark(request, exam_pk, sk, exam_answers_only=True): +def mark(request, exam_pk, sk, unmarked_exam_answers_only=True): exam = get_object_or_404(Exam, pk=exam_pk) mark_url = "rapids:mark" - if not exam_answers_only: + if not unmarked_exam_answers_only: mark_url = "rapids:mark_all" questions = exam.exam_questions.all() @@ -613,7 +613,7 @@ def mark(request, exam_pk, sk, exam_answers_only=True): # i.answer.lower() for i in question.incorrect_answers.all() # ] - if exam_answers_only: + if unmarked_exam_answers_only: unmarked_user_answers = question.get_unmarked_user_answers(exam_pk=exam_pk) else: unmarked_user_answers = question.get_unmarked_user_answers() @@ -630,76 +630,35 @@ def mark(request, exam_pk, sk, exam_answers_only=True): return redirect(mark_url, exam_pk=exam_pk, sk=n + 1) cd = form.cleaned_data - # correct = cd.get("correct") - # half_correct = cd.get("half_correct") - # incorrect = cd.get("incorrect") - print (cd) - print(dir(cd)) - 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() + marked_answers = json.loads(cd.get("marked_answers")) # This should probably use json (or something else...) # ajax..... - for ans in marked_answers["correct"]: - ans = ans.strip() - if ans == "": - continue + to_run = ( + ("correct", Answer.MarkOptions.CORRECT), + ("half-correct", Answer.MarkOptions.HALF_MARK), + ("incorrect", Answer.MarkOptions.INCORRECT), + ) - a = Answer.objects.filter( - answer__iexact=ans, question_id=question.pk - ).first() + for status_string, status in to_run: + for ans in marked_answers[status_string]: + ans = ans.strip() + if ans == "": + continue - if a is None: - a = Answer() - a.question_id = question.pk - a.answer = ans + a = Answer.objects.filter( + answer__iexact=ans, question_id=question.pk + ).first() - a.status = Answer.MarkOptions.CORRECT - a.save() + if a is None: + a = Answer() + a.question_id = question.pk + a.answer = ans - # for ans in half_correct.split("--//--"): - for ans in marked_answers["half-correct"]: - ans = ans.strip() - if ans == "": - continue + a.status = status + a.save() - 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(mark_url, exam_pk=exam_pk, sk=n + 1) elif "previous" in request.POST: @@ -710,7 +669,7 @@ def mark(request, exam_pk, sk, exam_answers_only=True): else: form = MarkRapidQuestionForm() - if exam_answers_only: + if unmarked_exam_answers_only: correct_answers = [] half_mark_answers = [] incorrect_answers = [] @@ -740,7 +699,7 @@ def mark(request, exam_pk, sk, exam_answers_only=True): "correct_answers": correct_answers, "half_mark_answers": half_mark_answers, "incorrect_answers": incorrect_answers, - "exam_answers_only": exam_answers_only, + "unmarked_exam_answers_only": unmarked_exam_answers_only, }, )