Skip to content
Merged
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
27 changes: 12 additions & 15 deletions src/staff/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
from django.contrib import messages
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 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
Expand Down Expand Up @@ -343,6 +337,11 @@ class SampleDetailView(StaffMixin, DetailView):


class SampleLabView(StaffMixin, SingleTableMixin, TemplateView):
MARKED = "marked"
PLUCKED = "plucked"
ISOLATED = "isolated"
VALID_STATUSES = [MARKED, PLUCKED, ISOLATED]

disable_pagination = False
template_name = "staff/sample_lab.html"
table_class = SampleStatusTable
Expand Down Expand Up @@ -377,7 +376,7 @@ def get_isolation_methods(self) -> list[str]:
)

def get_base_fields(self) -> list[str]:
return ["marked", "plucked", "isolated"]
return self.VALID_STATUSES

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
Expand Down Expand Up @@ -408,7 +407,7 @@ def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:

if status_name:
self.assign_status_to_samples(samples, status_name, request)
if status_name == "isolated":
if status_name == self.ISOLATED:
# Cannot use "samples" here
# because we need to check all samples in the order
self.check_all_isolated(Sample.objects.filter(order=order))
Expand All @@ -422,23 +421,21 @@ def assign_status_to_samples(
status_name: str,
request: HttpRequest,
) -> None:
valid_statuses = ["marked", "plucked", "isolated"]

if status_name not in valid_statuses:
if status_name not in self.VALID_STATUSES:
messages.error(request, f"Status '{status_name}' is not valid.")
return

update_fields = []

if status_name == "marked":
if status_name == self.MARKED:
update_fields.append("is_marked")
samples.update(is_marked=True)

elif status_name == "plucked":
elif status_name == self.PLUCKED:
update_fields.extend(["is_marked", "is_plucked"])
samples.update(is_marked=True, is_plucked=True)

elif status_name == "isolated":
elif status_name == self.ISOLATED:
update_fields.extend(["is_marked", "is_plucked", "is_isolated"])
samples.update(is_marked=True, is_plucked=True, is_isolated=True)

Expand Down