Passed
Push — 2.x ( 488b85...c0712a )
by Jordi
06:35
created

senaite.core.browser.samples.multi_results_classic   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 147
Duplicated Lines 14.29 %

Importance

Changes 0
Metric Value
wmc 22
eloc 81
dl 21
loc 147
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A MultiResultsClassicView.show_lab_analyses() 0 10 3
A MultiResultsClassicView.contents_table() 0 12 1
A MultiResultsClassicView.context_state() 0 5 1
A MultiResultsClassicView.get_objects_from_request() 0 5 1
A MultiResultsClassicView.__call__() 0 2 1
A MultiResultsClassicView.show_field_analyses() 0 10 3
A MultiResultsClassicView.get_object_by_uid() 0 8 2
A MultiResultsClassicView.get_samples() 21 21 4
A MultiResultsClassicView.__init__() 0 8 1
A MultiResultsClassicView.get_uids_from_request() 0 8 2
A MultiResultsClassicView.uniquify_items() 0 9 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-2023 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.browser import BrowserView
27
from bika.lims.interfaces import IAnalysisRequest
28
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
29
from senaite.core import logger
30
31
OFF_VALUES = ["0", "off", "no"]
32
33
34
class MultiResultsClassicView(BrowserView):
35
    """Allows to edit results of multiple samples
36
    """
37
    template = ViewPageTemplateFile("templates/multi_results.pt")
38
39
    def __init__(self, context, request):
40
        super(MultiResultsClassicView, self).__init__(context, request)
41
        self.context = context
42
        self.request = request
43
44
        self.classic = True
45
        self.transposed_url = "{}/multi_results?uids={}".format(
46
            self.context.absolute_url(), self.request.form.get("uids"))
47
48
    def __call__(self):
49
        return self.template()
50
51
    @property
52
    def context_state(self):
53
        return api.get_view(
54
            "plone_context_state",
55
            context=self.context, request=self.request)
56
57
    def contents_table(self, sample, poc):
58
        view_name = "table_{}_analyses".format(poc)
59
        view = api.get_view(view_name, context=sample, request=self.request)
60
        # Inject additional hidden field in the listing form for redirect
61
        # https://github.com/senaite/senaite.app.listing/pull/80
62
        view.additional_hidden_fields = [{
63
            "name": "redirect_url",
64
            "value": self.context_state.current_page_url(),
65
        }]
66
        view.update()
67
        view.before_render()
68
        return view.contents_table()
69
70
    def show_lab_analyses(self, sample):
71
        """Show/Hide lab analyses
72
        """
73
        analyses = sample.getAnalyses(getPointOfCapture="lab")
74
        if len(analyses) == 0:
75
            return False
76
        lab_analyses = self.request.get("lab_analyses")
77
        if lab_analyses in OFF_VALUES:
78
            return False
79
        return True
80
81
    def show_field_analyses(self, sample):
82
        """Show/Hide field analyses
83
        """
84
        analyses = sample.getAnalyses(getPointOfCapture="field")
85
        if len(analyses) == 0:
86
            return False
87
        field_analyses = self.request.get("field_analyses", True)
88
        if field_analyses in OFF_VALUES:
89
            return False
90
        return True
91
92 View Code Duplication
    def get_samples(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
93
        """Extract the samples from the request UIDs
94
95
        This might be either a samples container or a sample context
96
        """
97
98
        # fetch objects from request
99
        objs = self.get_objects_from_request()
100
101
        samples = []
102
103
        for obj in objs:
104
            # when coming from the samples listing
105
            if IAnalysisRequest.providedBy(obj):
106
                samples.append(obj)
107
108
        # when coming from the WF menu inside a sample
109
        if IAnalysisRequest.providedBy(self.context):
110
            samples.append(self.context)
111
112
        return self.uniquify_items(samples)
113
114
    def uniquify_items(self, items):
115
        """Uniquify the items with sort order
116
        """
117
        unique = []
118
        for item in items:
119
            if item in unique:
120
                continue
121
            unique.append(item)
122
        return unique
123
124
    def get_objects_from_request(self):
125
        """Returns a list of objects coming from the "uids" request parameter
126
        """
127
        unique_uids = self.get_uids_from_request()
128
        return filter(None, map(self.get_object_by_uid, unique_uids))
129
130
    def get_uids_from_request(self):
131
        """Return a list of uids from the request
132
        """
133
        uids = self.request.form.get("uids", "")
134
        if isinstance(uids, six.string_types):
135
            uids = uids.split(",")
136
        unique_uids = collections.OrderedDict().fromkeys(uids).keys()
137
        return filter(api.is_uid, unique_uids)
138
139
    def get_object_by_uid(self, uid):
140
        """Get the object by UID
141
        """
142
        logger.debug("get_object_by_uid::UID={}".format(uid))
143
        obj = api.get_object_by_uid(uid, None)
144
        if obj is None:
145
            logger.warn("!! No object found for UID #{} !!")
146
        return obj
147