Enhance exam review question fragment to display parts and responses with HTMX support for improved interactivity and user experience

This commit is contained in:
Ross
2025-11-10 13:00:47 +00:00
parent 24c35148b0
commit 66899b54bd
2 changed files with 133 additions and 1 deletions
+35 -1
View File
@@ -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):