Passed
Push — master ( d539f4...a81f8a )
by Ramon
05:14 queued 14s
created

bika.lims.browser.workflow.worksheet   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 44
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A WorkflowActionReassignAdapter.set_analyst() 0 8 2
B WorkflowActionAssignAdapter.sorted_analyses() 0 33 6
A WorkflowActionReassignAdapter.__call__() 0 8 3
A WorkflowActionAssignAdapter.__call__() 0 14 2
1
from bika.lims import bikaMessageFactory as _
2
from bika.lims.browser.workflow import WorkflowActionGenericAdapter
3
from bika.lims.interfaces import IWorksheet
4
5
6
class WorkflowActionAssignAdapter(WorkflowActionGenericAdapter):
7
    """Adapter in charge of assignment of analyses into a worksheet
8
    """
9
10
    def __call__(self, action, analyses):
11
        worksheet = self.context
12
        if not IWorksheet.providedBy(worksheet):
13
            return self.redirect(message=_("No changes made"), level="warning")
14
15
        # Sort the analyses by AR ID ascending + priority sort key, so the
16
        # positions of the ARs inside the WS are consistent with ARs order
17
        sorted_analyses = self.sorted_analyses(analyses)
18
19
        # Add analyses into the worksheet
20
        worksheet.addAnalyses(sorted_analyses)
21
22
        # Redirect the user to success page
23
        return self.success([worksheet])
24
25
    def sorted_analyses(self, analyses):
26
        """Sort the analyses by AR ID ascending and subsorted by priority
27
        sortkey within the AR they belong to
28
        """
29
        analyses = sorted(analyses, key=lambda an: an.getRequestID())
30
31
        def sorted_by_sortkey(objs):
32
            return sorted(objs, key=lambda an: an.getPrioritySortkey())
33
34
        # Now, we need the analyses within a request ID to be sorted by
35
        # sortkey (sortable_title index), so it will appear in the same
36
        # order as they appear in Analyses list from AR view
37
        current_sample_id = None
38
        current_analyses = []
39
        sorted_analyses = []
40
        for analysis in analyses:
41
            sample_id = analysis.getRequestID()
42
            if sample_id and current_sample_id != sample_id:
43
                # Sort the brains we've collected until now, that
44
                # belong to the same Analysis Request
45
                current_analyses = sorted_by_sortkey(current_analyses)
46
                sorted_analyses.extend(current_analyses)
47
                current_sample_id = sample_id
48
                current_analyses = []
49
50
            # Now we are inside the same AR
51
            current_analyses.append(analysis)
52
            continue
53
54
        # Sort the last set of brains we've collected
55
        current_analyses = sorted_by_sortkey(current_analyses)
56
        sorted_analyses.extend(current_analyses)
57
        return sorted_analyses
58
59
60
class WorkflowActionReassignAdapter(WorkflowActionGenericAdapter):
61
    """Adapter in charge of reassignment of an Analyst to a worksheet
62
    """
63
64
    def __call__(self, action, objects):
65
        # Assign the Analyst
66
        transitioned = filter(lambda obj: self.set_analyst(obj), objects)
67
        if not transitioned:
68
            return self.redirect(message=_("No changes made"), level="warning")
69
70
        # Redirect the user to success page
71
        return self.success(transitioned)
72
73
    def set_analyst(self, worksheet):
74
        analyst = self.get_form_value("Analyst", worksheet)
75
        if not analyst:
76
            # Cannot reassign if no analyst is set
77
            return False
78
        worksheet.setAnalyst(analyst)
79
        worksheet.reindexObject(idxs=["getAnalyst"])
80
        return True
81