ajaxGetAetiologicAgents.__call__()   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 43
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 43
rs 4.8
c 0
b 0
f 0
cc 12
nop 1

How to fix   Complexity   

Complexity

Complex classes like bika.health.browser.aetiologicagent.getaetiologicagents.ajaxGetAetiologicAgents.__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.HEALTH.
4
#
5
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software 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-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import json
22
from operator import itemgetter
23
24
import plone
25
from bika.lims.browser import BrowserView
26
27
28
class ajaxGetAetiologicAgents(BrowserView):
29
    """ Aetologic Agents from site setup
30
    """
31
    def __call__(self):
32
        plone.protect.CheckAuthenticator(self.request)
33
        title = 'title' in self.request and self.request['title'] or ''
34
        uid = 'uid' in self.request and self.request['uid'] or ''
35
        searchTerm = (len(title) == 0 and 'searchTerm' in self.request) and self.request['searchTerm'].lower() or title.lower()
36
        page = 'page' in self.request and self.request['page'] or 1
37
        nr_rows = 'rows' in self.request and self.request['rows'] or 10
38
        sord = 'sord' in self.request and self.request['sord'] or 'asc'
39
        sidx = 'sidx' in self.request and self.request['sidx'] or 'Title'
40
        rows = []
41
42
        # lookup objects from ZODB
43
        agents = self.bika_setup_catalog(portal_type='AetiologicAgent',
44
                                         is_active=True)
45
        if agents and searchTerm:
46
            agents = [agent for agent in agents if agent.Title.lower().find(searchTerm) > -1
47
                      or agent.Description.lower().find(searchTerm) > -1]
48
        for agent in agents:
49
            agent = agent.getObject()
50
            if (len(title) > 0 and agent.Title() == title):
51
                rows.append({'Title': agent.Title(),
52
                             'Description': agent.Description(),
53
                             'AgentUID': agent.UID()})
54
            elif len(uid) > 0 and agent.UID() == uid:
55
                rows.append({'Title': agent.Title(),
56
                             'Description': agent.Description(),
57
                             'AgentUID': agent.UID()})
58
            elif len(title) == 0 and len(uid) == 0:
59
                rows.append({'Title': agent.Title(),
60
                             'Description': agent.Description(),
61
                             'AgentUID': agent.UID()})
62
63
        rows = sorted(rows, cmp=lambda x, y: cmp(x.lower(), y.lower()), key=itemgetter(sidx and sidx or 'Title'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cmp does not seem to be defined.
Loading history...
64
        if sord == 'desc':
65
            rows.reverse()
66
        pages = len(rows) / int(nr_rows)
67
        pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0
68
        ret = {'page': page,
69
               'total': pages,
70
               'records': len(rows),
71
               'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]}
72
73
        return json.dumps(ret)
74