Passed
Push — 2.x ( 3a8d9c...f90226 )
by Ramon
06:47
created

senaite.core.browser.controlpanel.dynamicanalysisspecs.dynamicanalysisspec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 54.55 %

Importance

Changes 0
Metric Value
wmc 9
eloc 58
dl 54
loc 99
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A DynamicAnalysisSpecView.is_string() 0 2 1
A DynamicAnalysisSpecView.folderitems() 0 5 2
A DynamicAnalysisSpecView.make_empty_item() 18 18 3
A DynamicAnalysisSpecView.__init__() 36 36 3

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
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2024 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import collections
22
23
import six
24
25
from bika.lims import api
26
from bika.lims import senaiteMessageFactory as _
27
from senaite.app.listing.view import ListingView
28
29
30
class DynamicAnalysisSpecView(ListingView):
31
    """A listing view that shows the contents of the Excel
32
    """
33 View Code Duplication
    def __init__(self, context, request):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
34
        super(DynamicAnalysisSpecView, self).__init__(context, request)
35
36
        self.context_actions = {}
37
        self.show_search = False
38
        self.show_column_toggles = False
39
        self.title = api.get_title(self.context)
40
        self.description = api.get_description(self.context)
41
42
        if self.context.specs_file:
43
            self.description = "{} {}".format(
44
                 _(
45
                    u"dynamic_analysisspec_description",
46
                    default=u"Contents of the file"
47
                 ), self.context.specs_file.filename)
48
49
        self.specs = self.context.get_specs()
50
        self.total = len(self.specs)
51
52
        self.columns = collections.OrderedDict()
53
        for title in self.context.get_header():
54
            self.columns[title] = {
55
                "title": title,
56
                "toggle": True}
57
58
        self.review_states = [
59
            {
60
                "id": "default",
61
                "title": _(
62
                    u"listing_dynamic_analysisspec_state_all",
63
                    default=u"All"
64
                ),
65
                "contentFilter": {},
66
                "transitions": [],
67
                "custom_transitions": [],
68
                "columns": self.columns.keys()
69
            }
70
        ]
71
72 View Code Duplication
    def make_empty_item(self, record):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
        """Create a new empty item
74
        """
75
        item = {
76
            "uid": None,
77
            "before": {},
78
            "after": {},
79
            "replace": {},
80
            "allow_edit": [],
81
            "disabled": False,
82
            "state_class": "state-active",
83
        }
84
        for k, v in record.items():
85
            # ensure keyword dictionary keys contains only strings
86
            if not self.is_string(k):
87
                continue
88
            item[k] = v
89
        return item
90
91
    def is_string(self, value):
92
        return isinstance(value, six.string_types)
93
94
    def folderitems(self):
95
        items = []
96
        for record in self.specs:
97
            items.append(self.make_empty_item(record))
98
        return items
99