Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/genlab_bestilling/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
"genlab_id",
"fish_id",
"guid",
"order",
"analysis_orders",
"river",
"location.name",
"under_locality",
Expand Down
11 changes: 9 additions & 2 deletions src/genlab_bestilling/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,15 @@ def get_fish_id(self, obj: Sample) -> str:
return obj.fish_id or "-"

def get_analysis_orders(self, obj: Sample) -> list[str]:
if obj.order and obj.order.analysis_orders.exists():
return [str(anl.id) for anl in obj.order.analysis_orders.all()]
if not obj.order:
return []

analysis_orders = obj.order.analysis_orders.all()
# Return all analysis order IDs as strings
# only if there is exactly one analysis order, else return empty list.
# This is to ensure no duplicate rows in staffs common sheet
if analysis_orders.count() == 1:
return [str(analysis_orders.first().id)]
return []

def get_project(self, obj: Sample) -> str:
Expand Down
9 changes: 8 additions & 1 deletion src/genlab_bestilling/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any

from django.db import transaction
from django.db.models import QuerySet
from django.db.models import Prefetch, QuerySet
from django.http import HttpResponse
from django.views import View
from drf_spectacular.utils import extend_schema
Expand Down Expand Up @@ -36,6 +36,7 @@
SpeciesFilter,
)
from ..models import (
AnalysisOrder,
AnalysisType,
ExtractionOrder,
Location,
Expand Down Expand Up @@ -190,6 +191,12 @@ def get_queryset(self) -> QuerySet:
"order__genrequest__area",
"location",
)
.prefetch_related(
Prefetch(
"order__analysis_orders",
queryset=AnalysisOrder.objects.only("id"),
)
)
.order_by("genlab_id", "type")
)

Expand Down