diff --git a/src/senaite/referral/browser/outbound/add_samples.py b/src/senaite/referral/browser/outbound/add_samples.py index 4f6d655..1d2857b 100644 --- a/src/senaite/referral/browser/outbound/add_samples.py +++ b/src/senaite/referral/browser/outbound/add_samples.py @@ -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 """ @@ -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", @@ -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"), @@ -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 = """
+ {}
+ """ + 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 diff --git a/src/senaite/referral/browser/outbound/samples.py b/src/senaite/referral/browser/outbound/samples.py index cecb7d5..5152aed 100644 --- a/src/senaite/referral/browser/outbound/samples.py +++ b/src/senaite/referral/browser/outbound/samples.py @@ -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): @@ -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", @@ -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", @@ -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 diff --git a/src/senaite/referral/browser/shipment_manifest.py b/src/senaite/referral/browser/shipment_manifest.py index 8c231ee..adf0dd5 100644 --- a/src/senaite/referral/browser/shipment_manifest.py +++ b/src/senaite/referral/browser/shipment_manifest.py @@ -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