Passed
Push — master ( 986469...47c209 )
by Jordi
06:34
created

WorkflowActionPublishSamplesAdapter.publish_sample()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
3
import itertools
4
5
from bika.lims import _
6
from bika.lims import api
7
from bika.lims.browser.workflow import RequestContextAware
8
from bika.lims.interfaces import IWorkflowActionUIDsAdapter
9
from bika.lims.workflow import doActionFor
10
from zope.component.interfaces import implements
11
12
13
class WorkflowActionPublishSamplesAdapter(RequestContextAware):
14
    """Adapter in charge of the client 'publish_samples' action
15
    """
16
    implements(IWorkflowActionUIDsAdapter)
17
18
    def __call__(self, action, uids):
19
        published = []
20
21
        # get the selected ARReport objects
22
        reports = map(api.get_object_by_uid, uids)
23
        # get all the contained sample UIDs of the generated PDFs
24
        sample_uids = map(self.get_sample_uids_in_report, reports)
25
        # uniquify the UIDs of the contained samples
26
        unique_sample_uids = set(list(
27
            itertools.chain.from_iterable(sample_uids)))
28
29
        # publish all the contained samples of the selected reports
30
        for uid in unique_sample_uids:
31
            sample = api.get_object_by_uid(uid)
32
            if self.publish_sample(sample):
33
                published.append(sample)
34
35
        # generate a status message of the published sample IDs
36
        message = _("No items published")
37
        if published:
38
            message = _("Published {}".format(
39
                ", ".join(map(api.get_id, published))))
40
41
        # add the status message for the response
42
        self.add_status_message(message, "info")
43
44
        # redirect back
45
        referer = self.request.get_header("referer")
46
        return self.redirect(redirect_url=referer)
47
48
    def get_sample_uids_in_report(self, report):
49
        """Return a list of contained sample UIDs
50
        """
51
        metadata = report.getMetadata() or {}
52
        return metadata.get("contained_requests", [])
53
54
    def publish_sample(self, sample):
55
        """Set status to prepublished/published/republished
56
        """
57
        status = api.get_workflow_status_of(sample)
58
        transitions = {"verified": "publish", "published": "republish"}
59
        transition = transitions.get(status, "prepublish")
60
        succeed, message = doActionFor(sample, transition)
61
        return succeed
62