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
23 changes: 15 additions & 8 deletions src/genlab_bestilling/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.contrib import admin
from django.db.models import QuerySet
from django.http import HttpRequest
from unfold.admin import ModelAdmin
from unfold.contrib.filters import admin as unfold_filters

Expand Down Expand Up @@ -542,20 +544,25 @@ class AnalysisResultAdmin(ModelAdmin):
@admin.register(IsolationMethod)
class IsolationMethodAdmin(ModelAdmin):
M = IsolationMethod

list_display = [
M.name.field.name,
M.type.field.name,
"get_sample_types",
]

search_help_text = "Search for isolation method name or species name"
search_fields = [
M.name.field.name,
f"{M.type.field.name}__{Species.name.field.name}",
]
search_help_text = "Search for isolation method name"
search_fields = [M.name.field.name]
list_filter = [
(M.name.field.name, unfold_filters.FieldTextFilter),
(M.type.field.name, unfold_filters.AutocompleteSelectFilter),
(M.sample_types.field.name, unfold_filters.AutocompleteSelectMultipleFilter),
]
autocomplete_fields = [M.type.field.name]
autocomplete_fields = [M.sample_types.field.name]
filter_horizontal = [M.sample_types.field.name]
list_filter_submit = True
list_filter_sheet = False

def get_queryset(self, request: HttpRequest) -> QuerySet[IsolationMethod]:
return super().get_queryset(request).prefetch_related("sample_types")

def get_sample_types(self, obj: IsolationMethod) -> str:
return ", ".join([sample_type.name for sample_type in obj.sample_types.all()])
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.2.3 on 2025-07-24 15:45

from django.db import migrations
from django.db.migrations.state import StateApps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def delete_isolation_methods(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
IsolationMethod = apps.get_model("genlab_bestilling", "IsolationMethod")
IsolationMethod.objects.all().delete()


def reverse_delete_isolation_methods(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
):
pass


class Migration(migrations.Migration):
dependencies = [
(
"genlab_bestilling",
"0033_alter_order_contact_person_and_more",
),
]

operations = [
migrations.RunPython(
delete_isolation_methods,
reverse_delete_isolation_methods,
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.2.3 on 2025-07-24 15:49

from django.db import migrations
from django.db.models import ManyToManyField


class Migration(migrations.Migration):
dependencies = [
("genlab_bestilling", "0034_delete_all_isolationmethods_20250724_1745"),
]

operations = [
migrations.RemoveField(model_name="isolationmethod", name="type"),
migrations.AddField(
model_name="isolationmethod",
name="sample_types",
field=ManyToManyField(
to="genlab_bestilling.SampleType",
related_name="isolation_methods",
verbose_name="Sample types",
blank=True,
help_text="Sample types that this isolation method can be used for",
),
),
]
9 changes: 5 additions & 4 deletions src/genlab_bestilling/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,12 @@ def __str__(self) -> str:

class IsolationMethod(models.Model):
name = models.CharField(max_length=255)
type = models.ForeignKey(
sample_types = models.ManyToManyField(
f"{an}.SampleType",
on_delete=models.CASCADE,
related_name="type_isolation_methods",
help_text="The sample type this isolation method is related to.",
related_name="isolation_methods",
verbose_name="Sample types",
blank=True,
help_text="Sample types that this isolation method can be used for",
)

def __str__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/staff/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def get_queryset(self) -> QuerySet[Sample]:
def get_isolation_methods(self) -> QuerySet[IsolationMethod, str]:
types = self.get_queryset().values_list("type", flat=True).distinct()
return (
IsolationMethod.objects.filter(type__in=types)
IsolationMethod.objects.filter(sample_types__in=types)
.values_list("name", flat=True)
.distinct()
)
Expand Down