From 07d766cb30c5800ea18e1fd77c1b98be70750774 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 22 Jul 2024 11:35:37 +0100 Subject: [PATCH] fix some collection issues --- atlas/models.py | 17 ++++++++++++-- .../atlas/collection_case_detail.html | 9 ++++++++ atlas/templates/atlas/collection_detail.html | 15 +++++++++++++ atlas/urls.py | 5 +++++ atlas/views.py | 22 +++++++++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/atlas/models.py b/atlas/models.py index c0aef884..19338d70 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -1028,6 +1028,12 @@ class SeriesDetail(models.Model): class CaseDetail(models.Model): + """A through table that stores the relationship between a case and a collection. + + This is what user answers are linked to (as there be different questions for the same case in a different collection) + + The collection questions and answers are also defined here. + """ case = models.ForeignKey(Case, on_delete=models.CASCADE) collection = models.ForeignKey(CaseCollection, on_delete=models.CASCADE) @@ -1109,8 +1115,15 @@ class BaseReportAnswer(models.Model): for name, value in self.question.question_schema["properties"].items(): print(name, value) - user_answer = self.json_answer[name] - correct_answer = self.question.question_answers[name] + try: + user_answer = self.json_answer[name] + except KeyError: # If the schema has changed? + user_answer = "" + try: + correct_answer = self.question.question_answers[name] + except KeyError: # If the schema has changed? + correct_answer = "" + print(correct_answer) match value: diff --git a/atlas/templates/atlas/collection_case_detail.html b/atlas/templates/atlas/collection_case_detail.html index 3c354061..146eb488 100644 --- a/atlas/templates/atlas/collection_case_detail.html +++ b/atlas/templates/atlas/collection_case_detail.html @@ -18,6 +18,15 @@

Case: {{case_detail.case.title}}

+ {% if case_detail.question_schema is not None and not case_detail.question_answers %} + + + + {% endif %} +

This form allows you to add questions to a case. A example of how the quesiton will be displayed is shown below.

If answers are supplied the question will be automarked

diff --git a/atlas/templates/atlas/collection_detail.html b/atlas/templates/atlas/collection_detail.html index be4c840c..8b7ee981 100644 --- a/atlas/templates/atlas/collection_detail.html +++ b/atlas/templates/atlas/collection_detail.html @@ -44,6 +44,21 @@

+ +
+ Answer management + +
+

Manage answers for this collection.

+ + Please note these are permanant and cannot be undone. +
+ + + + {% endif %} {% include 'exam_overview_js.html' %} {% endblock %} diff --git a/atlas/urls.py b/atlas/urls.py index 6cdb342f..535fb325 100755 --- a/atlas/urls.py +++ b/atlas/urls.py @@ -131,6 +131,11 @@ urlpatterns = [ views.collection_question_schemas, name="collection_question_schemas", ), + path( + "collection//reset_answers", + views.collection_reset_answers, + name="collection_reset_answers", + ), #path( # "question_schemas_preset", # views.question_schemas_preset, diff --git a/atlas/views.py b/atlas/views.py index ffc24d05..06132ce0 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -2824,3 +2824,25 @@ def collection_question_schemas(request, exam_id: int): "case_details": case_details, }, ) + +def collection_reset_answers(request, exam_id: int): + if request.htmx: + collection = get_object_or_404(CaseCollection, pk=exam_id) + + # Select all case details (answers are linked to these) + case_details = collection.casedetail_set.all().prefetch_related() + + # Delete all answers + cid_answers = case_details.cidreportanswer_set.all() + user_answers = case_details.userreportanswer_set.all() + + + # User statuses + collection.exam_user_status.all().delete() + + # CidUserExams + CidUserExam.objects.filter(exam=collection).delete() + + + else: + raise Http404("Invalid request") \ No newline at end of file