From ce1b43968124b98de884294be986f063795f9e19 Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 11 Nov 2025 21:09:00 +0000 Subject: [PATCH] Refactor exam counts annotation to respect archived status toggle --- generic/views.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/generic/views.py b/generic/views.py index 8a57daeb..d0a672d7 100644 --- a/generic/views.py +++ b/generic/views.py @@ -5349,21 +5349,35 @@ class ExamCollectionList(ListView): if not show_archived: base_qs = base_qs.filter(archive=False) + # Build annotation kwargs dynamically so counts respect the show_archived toggle. + if show_archived: + # include archived exams in counts + counts = { + "anatomy_count": Count("anatomy_exams"), + "longs_count": Count("longs_exams"), + "physics_count": Count("physics_exams"), + "rapids_count": Count("rapids_exams"), + "sbas_count": Count("sbas_exams"), + } + else: + # only count non-archived exams + counts = { + "anatomy_count": Count("anatomy_exams", filter=Q(anatomy_exams__archive=False)), + "longs_count": Count("longs_exams", filter=Q(longs_exams__archive=False)), + "physics_count": Count("physics_exams", filter=Q(physics_exams__archive=False)), + "rapids_count": Count("rapids_exams", filter=Q(rapids_exams__archive=False)), + "sbas_count": Count("sbas_exams", filter=Q(sbas_exams__archive=False)), + } + qs = ( base_qs - .annotate( - anatomy_count=Count("anatomy_exams", filter=Q(anatomy_exams__archive=False)), - longs_count=Count("longs_exams", filter=Q(longs_exams__archive=False)), - physics_count=Count("physics_exams", filter=Q(physics_exams__archive=False)), - rapids_count=Count("rapids_exams", filter=Q(rapids_exams__archive=False)), - sbas_count=Count("sbas_exams", filter=Q(sbas_exams__archive=False)), - ) + .annotate(**counts) .annotate( total_count=F("anatomy_count") - +F("longs_count") - +F("physics_count") - +F("rapids_count") - +F("sbas_count") + + F("longs_count") + + F("physics_count") + + F("rapids_count") + + F("sbas_count") ) .prefetch_related("author") .order_by("name")