Add timestamps for answer tracking in report models and views; enhance timer display in templates

This commit is contained in:
Ross
2025-10-13 10:02:37 +01:00
parent 6f99cf0d53
commit a355dd223e
4 changed files with 113 additions and 1 deletions
+30
View File
@@ -2737,6 +2737,29 @@ def collection_case_view_take(
).first()
ReportAnswerForm = UserQuestionAnswerForm
# If we have an answer object but it doesn't have a started_at, set it now.
if answer and getattr(answer, 'started_at', None) is None:
try:
answer.started_at = timezone.now()
answer.save()
except Exception:
# Non-fatal: if saving fails, continue without blocking the user
logger.exception("Failed to set started_at on answer", exc_info=True)
# If no answer exists yet, create a placeholder so we have a started_at timestamp
if not answer and collection.collection_type in ("REP", "QUE"):
try:
if cid is not None:
answer = CidReportAnswer(question=case_detail, cid=cid)
else:
answer = UserReportAnswer(question=case_detail, user=request.user)
answer.started_at = timezone.now()
answer.save()
except Exception:
# If creation fails, leave answer as None; the normal flow will handle form creation
answer = None
logger.exception("Failed to create initial answer object", exc_info=True)
if request.method == "POST":
if collection.collection_type in ("REP", "QUE"):
if not collection.publish_results:
@@ -2754,6 +2777,8 @@ def collection_case_view_take(
answer = form.save(commit=False)
answer.set_cid_or_user(cid=cid, user=request.user)
answer.question = case_detail
# Record submission timestamp
answer.submitted_at = timezone.now()
# answer.published_date = timezone.now()
if "complete_case" in request.POST:
@@ -2872,6 +2897,11 @@ def collection_case_view_take(
"cid_user_exam": cid_user_exam,
"question_completed": question_completed,
"self_review": self_review,
# Provide a canonical start timestamp (prefer answer.started_at, then cid_user_exam.start_time)
"answer_started_at_iso": (
(answer.started_at.isoformat() if getattr(answer, 'started_at', None) else None)
or (cid_user_exam.start_time.isoformat() if getattr(cid_user_exam, 'start_time', None) else None)
),
},
)