Passed
Push — master ( 41ae8a...4c101c )
by Jordi
05:16
created

bika.lims.browser.worksheet.views.analyses_transposed   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 11.38 %

Importance

Changes 0
Metric Value
wmc 14
eloc 67
dl 14
loc 123
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A AnalysesTransposedView.folderitems() 0 31 2
A AnalysesTransposedView.get_slots() 0 7 3
A AnalysesTransposedView.make_empty_item() 14 14 1
A AnalysesTransposedView.get_analyses_uids() 0 5 1
A AnalysesTransposedView.__init__() 0 5 1
B AnalysesTransposedView.folderitem() 0 37 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from collections import OrderedDict
9
10
from bika.lims.browser.worksheet.views import AnalysesView
11
from bika.lims.utils import get_link
12
from plone.memoize import view
13
14
15
class AnalysesTransposedView(AnalysesView):
16
    """Transposed Manage Results View for Worksheet Analyses
17
    """
18
19
    def __init__(self, context, request):
20
        super(AnalysesTransposedView, self).__init__(context, request)
21
22
        self.headers = OrderedDict()
23
        self.services = OrderedDict()
24
25
    @view.memoize
26
    def get_slots(self):
27
        """Return the current used analyses positions
28
        """
29
        positions = map(
30
            lambda uid: self.get_item_slot(uid), self.get_analyses_uids())
31
        return map(lambda pos: str(pos), sorted(set(positions)))
32
33
    @view.memoize
34
    def get_analyses_uids(self):
35
        """Return assigned analyses UIDs
36
        """
37
        return self.context.getAnalysesUIDs()
38
39 View Code Duplication
    def make_empty_item(self, **kw):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
40
        """Create a new empty item
41
        """
42
        item = {
43
            "uid": None,
44
            "before": {},
45
            "after": {},
46
            "replace": {},
47
            "allow_edit": [],
48
            "disabled": False,
49
            "state_class": "state-active",
50
        }
51
        item.update(**kw)
52
        return item
53
54
    def folderitem(self, obj, item, index):
55
        super(AnalysesTransposedView, self).folderitem(obj, item, index)
56
57
        pos = str(item["Pos"])
58
        service = item["Service"]
59
        review_state = item["review_state"]
60
61
        # Skip retracted folderitems and display only the retest
62
        if review_state in ["retracted"]:
63
            return item
64
65
        # Append info link after the service
66
        # see: bika.lims.site.coffee for the attached event handler
67
        item["before"]["Result"] = get_link(
68
            "analysisservice_info?service_uid={}&analysis_uid={}"
69
            .format(item["service_uid"], item["uid"]),
70
            value="<span class='glyphicon glyphicon-info-sign'></span>",
71
            css_class="service_info")
72
73
        # remember the headers
74
        if "Pos" not in self.headers:
75
            self.headers["Pos"] = self.make_empty_item(
76
                column_key="Positions", item_key="Pos")
77
        if pos not in self.headers["Pos"]:
78
            # Add the item with the Pos header
79
            item["replace"]["Pos"] = self.get_slot_header(item)
80
            self.headers["Pos"][pos] = item
81
82
        # remember the services
83
        if service not in self.services:
84
            self.services[service] = self.make_empty_item(
85
                column_key=service, item_key="Result")
86
        if pos not in self.services[service]:
87
            # Add the item below its position
88
            self.services[service][pos] = item
89
90
        return item
91
92
    def folderitems(self):
93
        super(AnalysesTransposedView, self).folderitems()
94
95
        # Insert the "column key" column
96
        self.columns["column_key"] = {"title": ""}
97
98
        # Insert the columns for the slots
99
        for pos in self.get_slots():
100
            self.columns[pos] = {"title": "", "type": "transposed"}
101
102
        # Restrict visible columns
103
        self.review_states[0]["columns"] = ["column_key"] + self.get_slots()
104
105
        # transposed rows holder
106
        transposed = OrderedDict()
107
108
        # first row contains the HTML slot headers
109
        transposed.update(self.headers)
110
111
        # the collected services (Iron, Copper, Calcium...) come afterwards
112
        services = OrderedDict(reversed(self.services.items()))
113
        # the collected services (Iron, Copper, Calcium...) come afterwards
114
        transposed.update(services)
115
116
        # listing fixtures
117
        self.total = len(transposed.keys())
118
        self.show_select_column = False
119
        self.show_select_all_checkbox = False
120
121
        # return the transposed rows
122
        return transposed.values()
123