From a0c32532c78500cf8c54380cfca0463e6eca9a04 Mon Sep 17 00:00:00 2001 From: Ole Magnus Fon Johnsen Date: Tue, 8 Jul 2025 10:55:22 +0200 Subject: [PATCH] Get sample count for analysis --- src/staff/tables.py | 2 +- src/staff/templatetags/order_tags.py | 30 ++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/staff/tables.py b/src/staff/tables.py index 1b6a69d8..e5407b27 100644 --- a/src/staff/tables.py +++ b/src/staff/tables.py @@ -482,7 +482,7 @@ class AssignedOrderTable(StatusMixinTable, StaffIDMixinTable): samples_completed = tables.Column( accessor="sample_count", - verbose_name="Samples completed", + verbose_name="Samples isolated", orderable=False, ) diff --git a/src/staff/templatetags/order_tags.py b/src/staff/templatetags/order_tags.py index ff83f0db..09278823 100644 --- a/src/staff/templatetags/order_tags.py +++ b/src/staff/templatetags/order_tags.py @@ -52,7 +52,17 @@ def new_seen_orders_table(context: dict, area: Area | None = None) -> dict: .exclude(is_urgent=True) .select_related("genrequest") .annotate( - sample_count=models.Count("extractionorder__samples"), + sample_count=models.Case( + models.When( + extractionorder__isnull=False, + then=models.Count("extractionorder__samples", distinct=True), + ), + models.When( + analysisorder__isnull=False, + then=models.Count("analysisorder__samples", distinct=True), + ), + default=0, + ), priority=models.Case( models.When(is_urgent=True, then=Order.OrderPriority.URGENT), models.When(is_prioritized=True, then=Order.OrderPriority.PRIORITIZED), @@ -84,7 +94,17 @@ def new_unseen_orders_table(context: dict, area: Area | None = None) -> dict: .exclude(is_urgent=True) .select_related("genrequest") .annotate( - sample_count=models.Count("extractionorder__samples"), + sample_count=models.Case( + models.When( + extractionorder__isnull=False, + then=models.Count("extractionorder__samples", distinct=True), + ), + models.When( + analysisorder__isnull=False, + then=models.Count("analysisorder__samples", distinct=True), + ), + default=0, + ) ) .prefetch_related( "analysisorder__markers", @@ -116,14 +136,12 @@ def assigned_orders_table(context: dict) -> dict: ) .select_related("genrequest") .annotate( - sample_count=models.Count("extractionorder__samples"), - ) - .annotate( + sample_count=models.Count("extractionorder__samples", distinct=True), priority=models.Case( models.When(is_urgent=True, then=Order.OrderPriority.URGENT), models.When(is_prioritized=True, then=Order.OrderPriority.PRIORITIZED), default=1, - ) + ), ) .order_by( models.Case(