Refactor exam counts annotation to respect archived status toggle
This commit is contained in:
+25
-11
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user