Passed
Push — 2.x ( 269f1b...27353a )
by Ramon
05:03
created

bika.lims.browser.worksheet.ajax.AttachAnalyses.__call__()   C

Complexity

Conditions 11

Size

Total Lines 52
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 52
rs 5.4
c 0
b 0
f 0
cc 11
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bika.lims.browser.worksheet.ajax.SetInstrument.__call__() 0 11 3
A bika.lims.browser.worksheet.ajax.SetInstrument.__init__() 0 3 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like bika.lims.browser.worksheet.ajax.AttachAnalyses.__call__() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import plone
22
import plone.protect
23
from Products.Archetypes.config import REFERENCE_CATALOG
24
from Products.CMFCore.utils import getToolByName
25
from Products.Five.browser import BrowserView
26
27
28
class SetAnalyst(BrowserView):
29
    """The Analysis dropdown sets worksheet.Analyst immediately
30
    """
31
32
    def __init__(self, context, request):
33
        self.context = context
34
        self.request = request
35
36
    def __call__(self):
37
        mtool = getToolByName(self, 'portal_membership')
38
        plone.protect.CheckAuthenticator(self.request)
39
        plone.protect.PostOnly(self.request)
40
        value = self.request.get('value', '')
41
        if not value:
42
            return
43
        if not mtool.getMemberById(value):
44
            return
45
        self.context.setAnalyst(value)
46
47
48
class SetInstrument(BrowserView):
49
    """The Instrument dropdown sets worksheet.Instrument immediately
50
    """
51
52
    def __init__(self, context, request):
53
        self.context = context
54
        self.request = request
55
56
    def __call__(self):
57
        rc = getToolByName(self.context, REFERENCE_CATALOG)
58
        plone.protect.CheckAuthenticator(self.request)
59
        plone.protect.PostOnly(self.request)
60
        value = self.request.get('value', '')
61
        if not value:
62
            raise Exception("Invalid instrument")
63
        instrument = rc.lookupObject(value)
64
        if not instrument:
65
            raise Exception("Unable to lookup instrument")
66
        self.context.setInstrument(instrument)
67