Add question overview page with filtering options and counts by category

This commit is contained in:
Ross
2025-10-28 08:15:10 +00:00
parent 82ce6b0f93
commit 293b5b6682
4 changed files with 184 additions and 0 deletions
+111
View File
@@ -511,6 +511,117 @@ def generate_exam(request):
return render(request, "sbas/generate_exam.html", {"categories": categories, "status_options": status_options})
@login_required
def question_overview(request):
"""Show counts of questions grouped by category (default) with simple filters.
Default behaviour:
- group_by=category
- status=AC (Accepted)
Available GET params:
- status: AC, OD, ER, RJ, IP, ANY, UNREVIEWED
- category: category id to filter (optional)
- open_access: '1' or '0' to filter
"""
# Prepare status options as elsewhere
status_options = [("ANY", "Any"), ("UNREVIEWED", "Unreviewed")]
for v, label in QuestionReview.StatusChoices.choices:
status_options.append((v, label))
status = request.GET.get("status") or QuestionReview.StatusChoices.ACCEPTED
group_by = request.GET.get("group_by") or "category"
category_filter = request.GET.get("category")
open_access = request.GET.get("open_access")
qs = Question.objects.all().order_by("pk")
# Permission filter: non-checkers see open_access or their own questions
try:
if not request.user.groups.filter(name="sbas_checker").exists():
from django.db.models import Q
qs = qs.filter(Q(open_access=True) | Q(author__id=request.user.id))
except Exception:
pass
# Optional category filter
if category_filter:
try:
qs = qs.filter(category__id=int(category_filter))
except Exception:
pass
# Optional open_access filter
if open_access in ("0", "1"):
qs = qs.filter(open_access=(open_access == "1"))
# Build counts by category (default)
counts = {}
total = 0
from django.contrib.contenttypes.models import ContentType
for question in qs:
# Respect authors_only flag
try:
if question.authors_only and not (
request.user.is_superuser or request.user in question.author.all()
):
continue
except Exception:
pass
ct = ContentType.objects.get_for_model(question)
latest = (
QuestionReview.objects.filter(content_type=ct, object_id=question.pk)
.order_by("-created_on")
.first()
)
# Determine whether this question matches requested status
match = False
if status == "ANY":
match = True
elif status == "UNREVIEWED":
match = latest is None
else:
if latest is not None and latest.status == status:
match = True
if not match:
continue
# Grouping
if group_by == "category":
key = question.category.category if question.category else "Uncategorized"
counts[key] = counts.get(key, 0) + 1
else:
# fallback to category grouping if unknown
key = question.category.category if question.category else "Uncategorized"
counts[key] = counts.get(key, 0) + 1
total += 1
# Sort counts by descending
sorted_counts = sorted(counts.items(), key=lambda x: (-x[1], x[0]))
categories = Category.objects.all().order_by("category")
context = {
"status_options": status_options,
"selected_status": status,
"group_by": group_by,
"counts": sorted_counts,
"total": total,
"categories": categories,
"selected_category": category_filter,
"selected_open_access": open_access,
}
return render(request, "sbas/question_overview.html", context)
class UserAnswerTableView(LoginRequiredMixin, SingleTableMixin, FilterView):
model = UserAnswer
table_class = UserAnswerTable