From 4c97b788068082966b1ebe0146bfe9e7a5bff153 Mon Sep 17 00:00:00 2001 From: aastabk Date: Tue, 15 Jul 2025 17:06:15 +0200 Subject: [PATCH 1/5] Patch to sort properly when name is a mix of int and char, as well as only int or char. Generate genlab ID based on other fields (type). --- src/staff/tables.py | 8 ++++++++ src/staff/views.py | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/staff/tables.py b/src/staff/tables.py index 3bef4381..d485ec20 100644 --- a/src/staff/tables.py +++ b/src/staff/tables.py @@ -197,6 +197,14 @@ class Meta: empty_text = "No Samples" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + if hasattr(self.data, "data"): + self.data.data = self.data.data.annotate( + name_as_int=Cast("name", output_field=IntegerField()) + ) + def render_plate_positions(self, value: Any) -> str: if value: return ", ".join([str(v) for v in value.all()]) diff --git a/src/staff/views.py b/src/staff/views.py index 076738bc..e169e907 100644 --- a/src/staff/views.py +++ b/src/staff/views.py @@ -4,8 +4,13 @@ from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.db import models from django.db.models import ( + Case, Count, + IntegerField, + Value, + When, ) +from django.db.models.functions import Cast from django.forms import Form from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse from django.shortcuts import get_object_or_404 @@ -234,7 +239,7 @@ class OrderExtractionSamplesListView(StaffMixin, SingleTableMixin, FilterView): filterset_class = OrderSampleFilter def get_queryset(self) -> models.QuerySet[Sample]: - return ( + queryset = ( super() .get_queryset() .select_related("type", "location", "species") @@ -242,6 +247,18 @@ def get_queryset(self) -> models.QuerySet[Sample]: .filter(order=self.kwargs["pk"]) ) + # added to sort based on type (int/str) + return queryset.annotate( + name_as_int=Case( + When( + name__regex=r"^\d+$", + then=Cast("name", IntegerField()), + ), + default=Value(None), + output_field=IntegerField(), + ) + ) + def get_context_data(self, **kwargs) -> dict[str, Any]: context = super().get_context_data(**kwargs) order = ExtractionOrder.objects.get(pk=self.kwargs.get("pk")) From 9729a34ec68d07ee0fa18248a50342bb397985bd Mon Sep 17 00:00:00 2001 From: aastabk Date: Wed, 16 Jul 2025 10:36:46 +0200 Subject: [PATCH 2/5] Order is now only decided by the rows in the table, not a sorting_order value. --- src/genlab_bestilling/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/genlab_bestilling/managers.py b/src/genlab_bestilling/managers.py index 987b2b5d..008a0524 100644 --- a/src/genlab_bestilling/managers.py +++ b/src/genlab_bestilling/managers.py @@ -98,7 +98,7 @@ def generate_genlab_ids( samples.sort(key=lambda sample: id_pos.get(sample.id, 99999)) # Safe fallback updates = [] - for sample in samples: + for sample in selected_samples: sample.generate_genlab_id(commit=False) updates.append(sample) From e9eb10ce7f715c620ec3610581fdb5f4eaba1c5b Mon Sep 17 00:00:00 2001 From: Bertine <112892518+aastabk@users.noreply.github.com> Date: Wed, 16 Jul 2025 10:38:11 +0200 Subject: [PATCH 3/5] Update src/staff/tables.py Co-authored-by: Ole Magnus --- src/staff/tables.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/staff/tables.py b/src/staff/tables.py index d485ec20..3bef4381 100644 --- a/src/staff/tables.py +++ b/src/staff/tables.py @@ -197,14 +197,6 @@ class Meta: empty_text = "No Samples" - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - if hasattr(self.data, "data"): - self.data.data = self.data.data.annotate( - name_as_int=Cast("name", output_field=IntegerField()) - ) - def render_plate_positions(self, value: Any) -> str: if value: return ", ".join([str(v) for v in value.all()]) From 1b52adcea464c19f26f6218630a815143b20e200 Mon Sep 17 00:00:00 2001 From: aastabk Date: Wed, 16 Jul 2025 12:18:55 +0200 Subject: [PATCH 4/5] added safe "select_for_update()" --- src/genlab_bestilling/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/genlab_bestilling/managers.py b/src/genlab_bestilling/managers.py index 008a0524..987b2b5d 100644 --- a/src/genlab_bestilling/managers.py +++ b/src/genlab_bestilling/managers.py @@ -98,7 +98,7 @@ def generate_genlab_ids( samples.sort(key=lambda sample: id_pos.get(sample.id, 99999)) # Safe fallback updates = [] - for sample in selected_samples: + for sample in samples: sample.generate_genlab_id(commit=False) updates.append(sample) From eaa30264044f8150e7f4cbef1a2b192bf8450ab4 Mon Sep 17 00:00:00 2001 From: aastabk Date: Thu, 17 Jul 2025 12:04:51 +0200 Subject: [PATCH 5/5] Removed prints --- src/genlab_bestilling/tests/test_models.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/genlab_bestilling/tests/test_models.py b/src/genlab_bestilling/tests/test_models.py index 9228a7bf..f015df43 100644 --- a/src/genlab_bestilling/tests/test_models.py +++ b/src/genlab_bestilling/tests/test_models.py @@ -244,12 +244,8 @@ def test_ids_generation_with_only_numeric_names(genlab_setup): extraction.confirm_order() samples = [s1, s2, s3, s4] - print(samples) samples.sort(key=natural_sort_key) - print(samples) - sample_ids = [str(s.id) for s in samples] - print(sample_ids) Sample.objects.generate_genlab_ids( order_id=extraction.id,