Passed
Push — master ( 6ac657...5de4b7 )
by Jordi
06:03
created

bika.lims.browser.calcs.ajaxCalculateAnalysisEntry.calculate()   F

Complexity

Conditions 44

Size

Total Lines 235
Code Lines 163

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 163
dl 0
loc 235
rs 0
c 0
b 0
f 0
cc 44
nop 2

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.calcs.ajaxCalculateAnalysisEntry.calculate() 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
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
import json
9
10
import plone
11
from Products.CMFCore.utils import getToolByName
12
from bika.lims.browser import BrowserView
13
14
15
class ajaxGetMethodCalculation(BrowserView):
16
    """ Returns the calculation assigned to the defined method.
17
        uid: unique identifier of the method
18
    """
19
    def __call__(self):
20
        plone.protect.CheckAuthenticator(self.request)
21
        calcdict = {}
22
        uc = getToolByName(self, 'uid_catalog')
23
        method = uc(UID=self.request.get("uid", '0'))
24
        if method and len(method) == 1:
25
            calc = method[0].getObject().getCalculation()
26
            if calc:
27
                calcdict = {'uid': calc.UID(),
28
                            'title': calc.Title()}
29
        return json.dumps(calcdict)
30