This commit is contained in:
Ross
2025-12-08 11:18:47 +00:00
parent a4f4ba464a
commit eb06e63f77
+57 -1
View File
@@ -6132,9 +6132,34 @@ def exam_search_widget(request):
rqs = m.objects.none() rqs = m.objects.none()
for o in rqs: for o in rqs:
results.append(o) results.append(o)
# Render combined results # Render combined results, but restrict to exams the user can access
def user_can_view_exam(exam, user):
try:
if user.is_superuser:
return True
except Exception:
pass
try:
if exam.open_access:
return True
except Exception:
pass
try:
authors = exam.get_author_objects()
if user in authors:
return True
except Exception:
pass
try:
if hasattr(exam, "markers") and user in exam.markers.all():
return True
except Exception:
pass
return False
rendered_results = [] rendered_results = []
for o in results[:50]: for o in results[:50]:
if user_can_view_exam(o, request.user):
rendered_results.append({"id": getattr(o, "pk", None), "text": str(o)}) rendered_results.append({"id": getattr(o, "pk", None), "text": str(o)})
return render(request, "generic/partials/exam_search_widget_results.html", {"results": rendered_results, "field": field, "q": q_raw}) return render(request, "generic/partials/exam_search_widget_results.html", {"results": rendered_results, "field": field, "q": q_raw})
@@ -6145,8 +6170,39 @@ def exam_search_widget(request):
except Exception: except Exception:
qs = qs[:limit] qs = qs[:limit]
# Restrict results to exams the requesting user can access
def user_can_view_exam(exam, user):
try:
if user.is_superuser:
return True
except Exception:
pass
try:
if getattr(exam, "open_access", False) and getattr(exam, "active", False):
return True
except Exception:
pass
try:
authors = exam.get_author_objects()
if user in authors:
return True
except Exception:
pass
try:
if hasattr(exam, "cid_user_exam") and exam.cid_user_exam.filter(user_user=user).exists():
return True
except Exception:
pass
try:
if hasattr(exam, "markers") and user in exam.markers.all():
return True
except Exception:
pass
return False
results = [] results = []
for e in qs: for e in qs:
if user_can_view_exam(e, request.user):
results.append({"id": e.pk, "text": str(e)}) results.append({"id": e.pk, "text": str(e)})
return render(request, "generic/partials/exam_search_widget_results.html", {"results": results, "field": field, "q": q_raw}) return render(request, "generic/partials/exam_search_widget_results.html", {"results": results, "field": field, "q": q_raw})