Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/genlab_bestilling/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ExtractionPlate,
ExtractPlatePosition,
Genrequest,
IsolationMethod,
Location,
LocationType,
Marker,
Expand Down Expand Up @@ -532,3 +533,7 @@ class SampleStatusAdmin(ModelAdmin): ...

@admin.register(SampleStatusAssignment)
class SampleStatusAssignmentAdmin(ModelAdmin): ...


@admin.register(IsolationMethod)
class IsolationMethodAdmin(ModelAdmin): ...
200 changes: 0 additions & 200 deletions src/genlab_bestilling/libs/genlabid.py

This file was deleted.

75 changes: 74 additions & 1 deletion src/genlab_bestilling/managers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from django.db import models
from __future__ import annotations

from typing import TYPE_CHECKING

from django.db import models, transaction
from django.db.models import QuerySet
from django.db.models.expressions import RawSQL
from polymorphic.managers import PolymorphicManager, PolymorphicQuerySet

from capps.users.models import User

if TYPE_CHECKING:
from .models import GIDSequence, Species


class GenrequestQuerySet(models.QuerySet):
def filter_allowed(self, user: User) -> QuerySet:
Expand Down Expand Up @@ -46,6 +54,9 @@ def filter_in_draft(self) -> QuerySet:
)


DEFAULT_SORTING_FIELDS = ["name_as_int", "name"]


class SampleQuerySet(models.QuerySet):
def filter_allowed(self, user: User) -> QuerySet:
"""
Expand All @@ -61,6 +72,48 @@ def filter_in_draft(self) -> QuerySet:
order__status=self.model.OrderStatus.DRAFT
)

@transaction.atomic
def generate_genlab_ids(
self,
order_id: int,
sorting_order: list[str] | None = DEFAULT_SORTING_FIELDS,
selected_samples: list[int] | None = None,
) -> None:
"""
genlab ids given a certain order_id, sorting order and sample ids

"""

# Lock the samples
samples = (
self.select_related("species", "order")
.filter(order_id=order_id, genlab_id__isnull=True)
.select_for_update()
)

if selected_samples:
samples = samples.filter(id__in=selected_samples)

if sorting_order == DEFAULT_SORTING_FIELDS:
# create an annotation containg all integer values
# of "name", so that it's possible to sort numerically and alphabetically
samples = samples.annotate(
name_as_int=RawSQL(
r"substring(%s from '^\d+$')::int",
params=["name"],
output_field=models.IntegerField(),
)
).order_by(*sorting_order)
else:
samples = samples.order_by(*sorting_order)

updates = []
for sample in samples:
sample.generate_genlab_id(commit=False)
updates.append(sample)

self.bulk_update(updates, ["genlab_id"])


class SampleAnalysisMarkerQuerySet(models.QuerySet):
def filter_allowed(self, user: User) -> QuerySet:
Expand All @@ -76,3 +129,23 @@ def filter_in_draft(self) -> QuerySet:
return self.select_related("order").filter(
order__status=self.model.OrderStatus.DRAFT
)


class GIDSequenceQuerySet(models.QuerySet):
def get_sequence_for_species_year(
self, species: Species, year: int, lock: bool = False
) -> GIDSequence:
"""
Get or creates an ID sequence based on the sample year and species
"""
if lock:
s = self.select_for_update()
else:
s = self

sequence_id, _ = s.get_or_create(
year=year,
species=species,
defaults={"id": f"G{year % 100}{species.code}"},
)
return sequence_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.2.3 on 2025-07-08 10:19

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("genlab_bestilling", "0022_sample_internal_note"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name="order",
name="responsible_staff",
field=models.ManyToManyField(
blank=True,
help_text="Staff members responsible for this order",
related_name="responsible_orders",
to=settings.AUTH_USER_MODEL,
verbose_name="Responsible staff",
),
),
]
Loading