Skip to content

Commit

Permalink
Keep the order of samples in outbound shipment and manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
xispa committed Sep 26, 2024
1 parent 1c7ebfa commit 568c06b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 14 deletions.
76 changes: 72 additions & 4 deletions src/senaite/referral/browser/outbound/add_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
# Copyright 2021-2022 by it's authors.
# Some rights reserved, see README and LICENSE.

from senaite.referral import messageFactory as _
from senaite.referral.browser.outbound.samples import SamplesListingView

import collections
from bika.lims import AddAnalysisRequest
from bika.lims import api
from bika.lims import bikaMessageFactory as _c
from bika.lims import PRIORITIES
from bika.lims.api.security import check_permission
from senaite.core.catalog import SAMPLE_CATALOG
from senaite.referral import messageFactory as _
from senaite.app.listing import ListingView
from senaite.referral.utils import get_image_url


class AddSamplesListingView(SamplesListingView):
class AddSamplesListingView(ListingView):
"""View that lists the Samples available for assignment to current shipment
"""

Expand All @@ -38,8 +42,12 @@ def __init__(self, context, request):
"Create and add a new sample to this shipment or select existing "
"samples you wish to add"
)
self.icon = get_image_url("shipment_samples_big.png")
self.show_select_column = True
self.show_select_all_checkbox = True
self.show_search = True

self.catalog = SAMPLE_CATALOG
self.contentFilter = {
"review_state": "sample_received",
"assigned_state": "unassigned",
Expand All @@ -48,6 +56,45 @@ def __init__(self, context, request):
"isRootAncestor": True
}

self.columns = collections.OrderedDict((
("priority", {
"title": ""}),
("getId", {
"title": _c("Sample ID"),
"attr": "getId",
"replace_url": "getURL",
"index": "getId",
"sortable": True}),
("getDateSampled", {
"title": _c("Date Sampled"),
"toggle": True}),
("getDateReceived", {
"title": _c("Date Received"),
"toggle": True}),
("Client", {
"title": _c("Client"),
"index": "getClientTitle",
"attr": "getClientTitle",
"replace_url": "getClientURL",
"toggle": True}),
("getClientReference", {
"title": _c("Client Ref"),
"sortable": True,
"index": "getClientReference",
"toggle": False}),
("getClientSampleID", {
"title": _c("Client SID"),
"toggle": False}),
("getSampleTypeTitle", {
"title": _c("Sample Type"),
"sortable": True,
"toggle": True}),
("state_title", {
"title": _("State"),
"sortable": True,
"index": "review_state"}),
))

self.review_states = [{
"id": "default",
"title": _("All samples"),
Expand Down Expand Up @@ -78,3 +125,24 @@ def update(self):
"icon": "++resource++bika.lims.images/add.png"
}
})

def folderitem(self, obj, item, index):
"""Applies new properties to item that is currently being rendered as a
row in the list
"""
received = obj.getDateReceived
sampled = obj.getDateSampled

sample = api.get_object(obj)
priority = sample.getPriority()
if priority:
priority_text = PRIORITIES.getValue(priority)
priority_div = """<div class="priority-ico priority-{}">
<span class="notext">{}</span><div>
"""
priority = priority_div.format(priority, priority_text)
item["replace"]["priority"] = priority

item["getDateReceived"] = self.ulocalized_time(received, long_format=1)
item["getDateSampled"] = self.ulocalized_time(sampled, long_format=1)
return item
34 changes: 27 additions & 7 deletions src/senaite/referral/browser/outbound/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
# Some rights reserved, see README and LICENSE.

import collections

from senaite.app.listing import ListingView
from senaite.referral import messageFactory as _
from senaite.referral.utils import get_image_url

from bika.lims import api
from bika.lims import bikaMessageFactory as _c
from bika.lims import PRIORITIES
from bika.lims.catalog import CATALOG_ANALYSIS_REQUEST_LISTING
from senaite.app.listing import ListingView
from senaite.core.catalog import SAMPLE_CATALOG
from senaite.referral import messageFactory as _
from senaite.referral.utils import get_image_url


class SamplesListingView(ListingView):
Expand All @@ -43,7 +41,7 @@ def __init__(self, context, request):
self.show_select_all_checkbox = True
self.show_search = False

self.catalog = CATALOG_ANALYSIS_REQUEST_LISTING
self.catalog = SAMPLE_CATALOG
self.contentFilter = {
"UID": context.getRawSamples(),
"sort_on": "sortable_title",
Expand All @@ -53,6 +51,11 @@ def __init__(self, context, request):
self.columns = collections.OrderedDict((
("priority", {
"title": ""}),
("position", {
"title": _("Pos."),
"index": "sortable_title",
"sortable": True
}),
("getId", {
"title": _c("Sample ID"),
"attr": "getId",
Expand Down Expand Up @@ -114,6 +117,23 @@ def update(self):
"confirm_transitions": [],
})

def folderitems(self):
items = super(SamplesListingView, self).folderitems()

# use 'sortable_title' as the flag to sort by the order in the field
sort_on = self.get_sort_on()
if sort_on != "sortable_title":
return items

# set the position attribute for each item
uids = self.context.getRawSamples()
for item in items:
item["position"] = uids.index(item["uid"]) + 1

# sort them ascending or descending
rev = self.get_sort_order() in ["descending"]
return sorted(items, key=lambda item: item["position"], reverse=rev)

def folderitem(self, obj, item, index):
"""Applies new properties to item that is currently being rendered as a
row in the list
Expand Down
8 changes: 5 additions & 3 deletions src/senaite/referral/browser/shipment_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ def shipment(self):
return self.context

def get_samples(self):
"""Returns the samples of the shipment, sorted by id ascending
"""Returns the samples of the shipment, sorted in the same order as
they were initially added in the shipment
"""
samples = self.shipment.getSamples()
return sorted(samples, key=lambda sample: sample.getId())
# return the samples without sorting, so they are sorted in the same
# order as they were added, ascending
return self.shipment.getSamples()

def to_localized_time(self, date, **kw):
"""Converts the given date to a localized time string
Expand Down

0 comments on commit 568c06b

Please sign in to comment.