diff --git a/anatomy/views.py b/anatomy/views.py index 902db09f..7c3be34e 100644 --- a/anatomy/views.py +++ b/anatomy/views.py @@ -95,9 +95,43 @@ class AuthorOrCheckerRequiredMixin(object): @register.filter def get_item(dictionary, key, default=None): + """Safe template filter to fetch an item by key/index from various containers. + + Supports dict-like objects (uses .get), lists/tuples (indexing by integer + keys), and other indexable objects. Returns ``default`` when the lookup + fails or the input is None. + """ if dictionary is None: return default - return dictionary.get(key) + + # If it's dict-like, prefer .get + try: + if hasattr(dictionary, "get") and callable(getattr(dictionary, "get")): + # Allow .get default behaviour + try: + return dictionary.get(key, default) + except Exception: + # Fall through to other strategies + pass + + # If it's a list/tuple and key is integer-like, return by index + if isinstance(dictionary, (list, tuple)): + try: + idx = int(key) + except Exception: + return default + try: + return dictionary[idx] + except Exception: + return default + + # Generic attempt: try __getitem__ with key (handles QueryDict, lists with int keys, etc.) + try: + return dictionary[key] + except Exception: + return default + except Exception: + return default def user_is_admin(user): diff --git a/physics/templates/physics/partials/exam_review_question_fragment.html b/physics/templates/physics/partials/exam_review_question_fragment.html index 6b74b338..3a7f0503 100644 --- a/physics/templates/physics/partials/exam_review_question_fragment.html +++ b/physics/templates/physics/partials/exam_review_question_fragment.html @@ -1,3 +1,101 @@ +
+
+
Question
+

{{ question|safe }}

+ +
+
Parts
+
    + {% if question.get_questions %} + {% for part in question.get_questions %} +
  • +
    + Part {{ forloop.counter }}. + {{ part }} +
    +
    + {# If we can determine the correct answer for this part show a badge - best-effort #} + {% if question.get_answers %} + {% with ans=question.get_answers|get_item:forloop.counter0 %} + {% if ans %} + Answer: {{ ans }} + {% endif %} + {% endwith %} + {% endif %} +
    +
  • + {% endfor %} + {% else %} +
  • No parts defined for this question.
  • + {% endif %} +
+
+ +

+ View full question details +

+ +
+
Response summary
+
+ + +
Loading summary…
+
+
+ +
+
+
Responses in this exam
+
+ +
+
+
+ + +
Responses are hidden — click “Show responses” to load.
+
+
+ +
+ {% if prev_index is not None %} + ← Previous + {% else %} + + {% endif %} + + {% if next_index is not None %} + Next → + {% else %} + Complete review + {% endif %} +
+ +
+
Physics