Enhance answer aggregation logic in exam review to handle diverse answer formats and improve robustness
This commit is contained in:
+41
-10
@@ -466,16 +466,47 @@ def exam_review_question_summary(request, pk: int, q_index: int):
|
||||
# Physics answers may be stored as tuples/lists or named fields; attempt to aggregate
|
||||
counts = defaultdict(int)
|
||||
other_count = 0
|
||||
for ans in ua_qs.values_list("answer", flat=True):
|
||||
if not ans:
|
||||
other_count += 1
|
||||
continue
|
||||
# normalize iterable answers to a string key
|
||||
if isinstance(ans, (list, tuple)):
|
||||
key = ", ".join([str(x) for x in ans])
|
||||
else:
|
||||
key = str(ans)
|
||||
counts[key] += 1
|
||||
# Try simple 'answer' field aggregation first; if not present, fall
|
||||
# back to iterating objects and using get_answer() or composing parts.
|
||||
try:
|
||||
for ans in ua_qs.values_list("answer", flat=True):
|
||||
if not ans:
|
||||
other_count += 1
|
||||
continue
|
||||
if isinstance(ans, (list, tuple)):
|
||||
key = ", ".join([str(x) for x in ans])
|
||||
else:
|
||||
key = str(ans)
|
||||
counts[key] += 1
|
||||
except Exception:
|
||||
for ua in ua_qs:
|
||||
ans_val = None
|
||||
try:
|
||||
if hasattr(ua, "get_answer"):
|
||||
ans_val = ua.get_answer()
|
||||
except Exception:
|
||||
ans_val = None
|
||||
|
||||
if not ans_val:
|
||||
parts = []
|
||||
for fld in ("a", "b", "c", "d", "e"):
|
||||
if hasattr(ua, fld):
|
||||
v = getattr(ua, fld)
|
||||
if v is None:
|
||||
continue
|
||||
if isinstance(v, (list, tuple)):
|
||||
parts.extend([str(x) for x in v])
|
||||
else:
|
||||
parts.append(str(v))
|
||||
|
||||
if parts:
|
||||
ans_val = ", ".join(parts)
|
||||
|
||||
if not ans_val:
|
||||
other_count += 1
|
||||
continue
|
||||
|
||||
counts[ans_val] += 1
|
||||
|
||||
counts["unanswered"] = other_count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user