Passed
Push — master ( ef1ceb...20aa4c )
by Jordi
06:12
created

bika.lims.browser.analysisrequest.manage_results   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 42.22 %

Importance

Changes 0
Metric Value
wmc 6
eloc 25
dl 19
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A AnalysisRequestManageResultsView.__call__() 0 3 1
A AnalysisRequestManageResultsView.checkInstrumentsValidity() 19 21 5

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 bika.lims import bikaMessageFactory as _
9
from bika.lims.browser.analysisrequest import AnalysisRequestViewView
10
from Products.CMFPlone.utils import safe_unicode
11
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
12
13
14
class AnalysisRequestManageResultsView(AnalysisRequestViewView):
15
    """Manage Results View
16
    """
17
    template = ViewPageTemplateFile(
18
        "templates/analysisrequest_manage_results.pt")
19
20
    def __call__(self):
21
        self.checkInstrumentsValidity()
22
        return self.template()
23
24 View Code Duplication
    def checkInstrumentsValidity(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
        """Checks the validity of the instruments used in the Analyses
26
27
        If an analysis with an invalid instrument (out-of-date or with
28
        calibration tests failed) is found, a warn message will be displayed.
29
        """
30
        invalid = []
31
32
        ans = [a.getObject() for a in self.context.getAnalyses()]
33
        for an in ans:
34
            valid = an.isInstrumentValid()
35
            if not valid:
36
                inv = '%s (%s)' % (safe_unicode(an.Title()),
37
                                   safe_unicode(an.getInstrument().Title()))
38
                if inv not in invalid:
39
                    invalid.append(inv)
40
        if len(invalid) > 0:
41
            message = _("Some analyses use out-of-date or uncalibrated "
42
                        "instruments. Results edition not allowed")
43
            message = "%s: %s" % (message, (', '.join(invalid)))
44
            self.context.plone_utils.addPortalMessage(message, 'warn')
45