.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
{% if results %}
|
||||
<ul class="list-unstyled mb-0" id="exam_search_results_list_{{ field }}">
|
||||
{% for item in results %}
|
||||
<li class="py-1 d-flex justify-content-between align-items-center"
|
||||
<li class="py-1 d-flex justify-content-between align-items-center"
|
||||
data-exam-id="{{ item.id }}" data-exam-text="{{ item.text|escapejs }}"
|
||||
data-exam-archive="{{ item.archive|yesno:'1,0' }}" data-exam-open-access="{{ item.open_access|yesno:'1,0' }}"
|
||||
data-exam-exam-mode="{{ item.exam_mode|yesno:'1,0' }}" data-exam-candidates-only="{{ item.candidates_only|yesno:'1,0' }}">
|
||||
|
||||
+11
-78
@@ -6277,85 +6277,18 @@ def supervisor_search(request):
|
||||
q = (request.GET.get("q") or "").strip()
|
||||
row = request.GET.get("row")
|
||||
if not q:
|
||||
else:
|
||||
# If no model specified, try to search across several known exam models
|
||||
possible_models = [
|
||||
"anatomy.Exam",
|
||||
"longs.Exam",
|
||||
"rapids.Exam",
|
||||
"shorts.Exam",
|
||||
"physics.Exam",
|
||||
"sbas.Exam",
|
||||
]
|
||||
results = []
|
||||
return HttpResponse("")
|
||||
|
||||
# gather optional filters from GET params
|
||||
extra_filters = {}
|
||||
for pname in ("archive", "open_access", "exam_mode", "candidates_only"):
|
||||
val = _get_bool_param(pname)
|
||||
if val is not None:
|
||||
extra_filters[pname] = val
|
||||
# Search Supervisor records by name or email and render a small partial
|
||||
try:
|
||||
from django.db.models import Q
|
||||
qs = Supervisor.objects.filter(Q(name__icontains=q) | Q(email__icontains=q)).order_by("name")[:10]
|
||||
except Exception:
|
||||
qs = Supervisor.objects.none()
|
||||
|
||||
for ml in possible_models:
|
||||
try:
|
||||
m = apps.get_model(*ml.split("."))
|
||||
except Exception:
|
||||
continue
|
||||
try:
|
||||
# try common name-like fields and apply extra_filters when possible
|
||||
try:
|
||||
rqs = m.objects.filter(name__icontains=q, **extra_filters)[:5]
|
||||
except Exception:
|
||||
try:
|
||||
rqs = m.objects.filter(title__icontains=q, **extra_filters)[:5]
|
||||
except Exception:
|
||||
# fallback to unfiltered small slice
|
||||
rqs = m.objects.filter(name__icontains=q)[:5]
|
||||
except Exception:
|
||||
rqs = m.objects.none()
|
||||
for o in rqs:
|
||||
results.append(o)
|
||||
results = []
|
||||
for s in qs:
|
||||
results.append({"id": s.pk, "text": str(s)})
|
||||
|
||||
# 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 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
|
||||
|
||||
rendered_results = []
|
||||
for o in results[:50]:
|
||||
if user_can_view_exam(o, request.user):
|
||||
rendered_results.append({
|
||||
"id": getattr(o, "pk", None),
|
||||
"text": str(o),
|
||||
"archive": getattr(o, "archive", False),
|
||||
"open_access": getattr(o, "open_access", False),
|
||||
"exam_mode": getattr(o, "exam_mode", False),
|
||||
"candidates_only": getattr(o, "candidates_only", False),
|
||||
})
|
||||
return render(request, "generic/partials/exam_search_widget_results.html", {"results": rendered_results, "field": field, "q": q_raw})
|
||||
return render(request, "generic/partials/supervisor_search_results.html", {"results": results, "row": row})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user