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
25 changes: 16 additions & 9 deletions src/genlab_bestilling/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ class SampleCSVSerializer(serializers.ModelSerializer):
analysis_orders = serializers.SerializerMethodField()
project = serializers.SerializerMethodField()
isolation_method = serializers.SerializerMethodField()
marked = serializers.SerializerMethodField()
plucked = serializers.SerializerMethodField()
isolated = serializers.SerializerMethodField()
is_marked = serializers.SerializerMethodField()
is_plucked = serializers.SerializerMethodField()
is_isolated = serializers.SerializerMethodField()
internal_note = serializers.SerializerMethodField()

class Meta:
model = Sample
Expand All @@ -123,9 +124,10 @@ class Meta:
"analysis_orders",
"project",
"isolation_method",
"marked",
"plucked",
"isolated",
"is_marked",
"is_plucked",
"is_isolated",
"internal_note",
]

def get_fish_id(self, obj: Sample) -> str:
Expand All @@ -148,15 +150,20 @@ def get_isolation_method(self, obj: Sample) -> str:
def _flag(self, value: bool) -> str:
return "x" if value else ""

def get_marked(self, obj: Sample) -> str:
def get_is_marked(self, obj: Sample) -> str:
return self._flag(obj.is_marked)

def get_plucked(self, obj: Sample) -> str:
def get_is_plucked(self, obj: Sample) -> str:
return self._flag(obj.is_plucked)

def get_isolated(self, obj: Sample) -> str:
def get_is_isolated(self, obj: Sample) -> str:
return self._flag(obj.is_isolated)

def get_internal_note(self, obj: Sample) -> str:
if obj.internal_note:
return obj.internal_note
return ""


class SampleUpdateSerializer(serializers.ModelSerializer):
has_error = serializers.SerializerMethodField()
Expand Down
137 changes: 114 additions & 23 deletions src/genlab_bestilling/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import uuid

from django.db import transaction
Expand Down Expand Up @@ -92,30 +93,64 @@ class SampleViewset(ModelViewSet):
"length": "Length",
"weight": "Weight",
"classification": "Classification",
"year": "Date",
"notes": "Remarks",
"project": "Projectnumber",
"year": "Year",
"notes": "Notes",
"project": "Project Number",
"isolation_method": "Isolation Method",
"qiagen_number": "Qiagen#",
"is_marked": "Marked",
"is_plucked": "Plucked",
"is_isolated": "Isolated",
"station": "Station",
"placement_in_fridge": "Placement in fridge",
"delivered_to_lab": "Delivered to lab",
"placement_in_fridge": "Placement in Fridge",
"delivered_to_lab": "Delivered to Lab",
"fluidigm#": "Fluidigm#",
"laksov_species": "LaksOV + Species",
"species_date": "Species Date",
"pcr": "PCR",
"fluidigm": "Fluidigm",
"output": "Output",
"analysis_note": "Analysis Note",
"rerun": "Rerun",
"rerun_qiagen": "Rerun Qiagen",
"river": "River",
"str_date": "STR Date",
"rerun_date": "Rerun Date",
"watercourse_number": "Watercourse Number",
"under_locality": "Sub-locality",
"county": "County",
"snp_assay_1": "SNP Assay 1",
"snp_assay_2": "SNP Assay 2",
"str_mixab": "STR Mix AB",
"str_mixc": "STR Mix C",
"str_mixy": "STR Mix Y",
"internal_note": "Internal Note",
"bb#": "BB#",
"output_bb": "Output",
"bba2#": "BBA2#",
"output_bba2": "Output",
"mixab": "Mix AB",
"ab_rep#": "AB Rep#",
"mixc": "Mix C",
"c_rep#": "C Rep#",
"re_extracted_date": "Re-extracted Date",
"re_extracted_qiagen#": "Re-extracted Qiagen#",
"weight_g_for_diet_analysis": "Weight (g) for Diet Analysis",
"position": "Position",
}

# NOTE: This can be modified to include more fields based on species or area.
CSV_FIELDS_BY_AREA: dict[str, list[str]] = {
"Akvatisk": [
"name",
"genlab_id",
"fish_id",
"guid",
"order",
"analysis_orders",
"location.name",
"watercourse_number",
"pop_id",
"name",
"species.name",
"gender",
"length",
Expand All @@ -127,54 +162,89 @@ class SampleViewset(ModelViewSet):
"type.name",
"isolation_method",
"qiagen_number",
"fluidigm#",
"laksov_species",
"species_date",
"is_marked",
"is_plucked",
"is_isolated",
"pcr",
"fluidigm",
"output",
"analysis_note",
"rerun",
"rerun_date",
],
"Elvemusling": [
"name",
"genlab_id",
"fish_id",
"guid",
"river",
"location.name",
"under_locality",
"watercourse_number",
"county",
"year",
"name",
"station",
"type.name",
"length",
"notes",
"isolation_method",
"qiagen_number",
"position",
"placement_in_fridge",
"is_marked",
"is_plucked",
"is_isolated",
"pcr",
"str_date",
"output",
"analysis_note",
"rerun",
"rerun_date",
],
"Terrestrisk": [
"name",
"genlab_id",
"guid",
"name",
"type.name",
"species.name",
"location.name",
"delivered_to_lab",
"order",
"analysis_orders",
"notes",
"snp_assay_1",
"snp_assay_2",
"str_mixab",
"str_mixc",
"str_mixy",
"is_marked",
"is_plucked",
"is_isolated",
"isolation_method",
"qiagen_number",
"internal_note",
"bb#",
"output_bb",
"bba2#",
"output_bba2",
"mixab",
"ab_rep#",
"mixc",
"c_rep#",
"re_extracted_date",
"re_extracted_qiagen#",
"weight_g_for_diet_analysis",
],
# Same as "Terrestrisk" for now, can be modified later if needed.
"default": [
"genlab_id",
"guid",
"name",
"type.name",
"species.name",
"location.name",
"delivered_to_lab",
"order",
"analysis_orders",
"notes",
Expand Down Expand Up @@ -239,19 +309,40 @@ def get_nested(self, obj: dict, dotted: str) -> str | None:
return obj

def build_csv_data(
self, serialized_data: list[dict], fields: list[str]
self, serialized_data: list[dict], fields: list[str], area_name: str
) -> list[dict[str, str]]:
return [
{
self.CSV_FIELD_LABELS[f]: (
", ".join(v)
if isinstance(v := self.get_nested(item, f), list)
else v or ""
)
for f in fields
}
for item in serialized_data
]
rows = []

for item in serialized_data:
row = {}

for f in fields:
if area_name in {"Akvatisk", "Elvemusling"} and f == "location.name":
full_location = self.get_nested(item, "location.name")

if (
full_location
and re.search(r"\d+", full_location)
and " " in full_location
):
watercourse, location = full_location.split(" ", 1)
else:
watercourse, location = "", full_location or ""

row[self.CSV_FIELD_LABELS["watercourse_number"]] = watercourse
row[self.CSV_FIELD_LABELS["location.name"]] = location

# We skip "watercourse_number" because it is handled above.
elif f != "watercourse_number":
value = self.get_nested(item, f)
if isinstance(value, list):
row[self.CSV_FIELD_LABELS[f]] = ", ".join(value)
else:
row[self.CSV_FIELD_LABELS[f]] = value or ""

rows.append(row)

return rows

@action(
methods=["GET"],
Expand All @@ -269,7 +360,7 @@ def csv(self, request: Request) -> HttpResponse:
)

fields, headers = self.get_csv_fields_and_labels(area_name, queryset)
data = self.build_csv_data(serializer.data, fields)
data = self.build_csv_data(serializer.data, fields, area_name)

csv_data = CSVRenderer().render(
data,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.2.3 on 2025-07-18 07:58

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("genlab_bestilling", "0028_sample_is_isolated_sample_is_marked_and_more"),
]

operations = [
migrations.AlterField(
model_name="order",
name="contact_email",
field=models.EmailField(
help_text="Email to contact with questions about this order",
max_length=254,
null=True,
),
),
migrations.AlterField(
model_name="order",
name="contact_person",
field=models.CharField(
help_text="Person to contact with questions about this order", null=True
),
),
]
10 changes: 8 additions & 2 deletions src/genlab_bestilling/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ class OrderPriority:
)
contact_person = models.CharField(
null=True,
blank=True,
blank=False,
help_text="Person to contact with questions about this order",
)
contact_email = models.EmailField(
null=True,
blank=True,
blank=False,
help_text="Email to contact with questions about this order",
)
responsible_staff = models.ManyToManyField(
Expand All @@ -310,6 +310,8 @@ class OrderPriority:
def confirm_order(self) -> None:
self.status = Order.OrderStatus.DELIVERED
self.confirmed_at = timezone.now()
if self.is_urgent:
self.is_seen = True
self.save()

def clone(self) -> None:
Expand All @@ -325,6 +327,10 @@ def to_draft(self) -> None:
self.confirmed_at = None
self.save()

def to_processing(self) -> None:
self.status = Order.OrderStatus.PROCESSING
self.save()

def toggle_seen(self) -> None:
self.is_seen = not self.is_seen
self.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h3 class="text-4xl mb-5">{% block page-title %}{% endblock page-title %}</h3>
<div class="flex flex-wrap gap-4">
{{ filter.form | crispy }}
<button class="btn custom_order_button_blue mt-6" type="submit">Search</button>
<a href="{{ request.path }}" class="btn custom_order_button_grey mt-6">Clear Filters</a>
</div>
</form>

Expand Down
Loading