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, 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"))