bika.health.browser.aetiologicagent.getaetiologicagentsubtypes   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 58.82 %

Importance

Changes 0
Metric Value
wmc 11
eloc 39
dl 40
loc 68
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C ajaxGetAetiologicAgentSubtypes.__call__() 37 37 11

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.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 View Code Duplication
class ajaxGetAetiologicAgentSubtypes(BrowserView):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
29
    """ Aetologic Agent Subtypes for a specified Aetiologic Agent 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
        auid = 'auid' in self.request and self.request['auid'] 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 'Subtype'
40
        rows = []
41
42
        agents = self.bika_setup_catalog(portal_type='AetiologicAgent', UID=auid)
43
        if agents and len(agents) == 1:
44
            agent = agents[0].getObject()
45
            subtypes = agent.getAetiologicAgentSubtypes()
46
            if subtypes and searchTerm:
47
                subtypes = [subtype for subtype in subtypes if subtype['Subtype'].lower().find(searchTerm) > -1]
48
49
        for subtype in subtypes:
0 ignored issues
show
introduced by
The variable subtypes does not seem to be defined in case agents and len(agents) == 1 on line 43 is False. Are you sure this can never be the case?
Loading history...
50
            if (len(title) > 0 and subtype['Subtype'] == title):
51
                rows.append({'Subtype': subtype['Subtype'],
52
                             'SubtypeRemarks': subtype.get('SubtypeRemarks', '')})
53
            elif len(title) == 0:
54
                rows.append({'Subtype': subtype['Subtype'],
55
                             'SubtypeRemarks': subtype.get('SubtypeRemarks', '')})
56
57
        rows = sorted(rows, cmp=lambda x, y: cmp(x.lower(), y.lower()), key=itemgetter(sidx and sidx or 'Subtype'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cmp does not seem to be defined.
Loading history...
58
        if sord == 'desc':
59
            rows.reverse()
60
        pages = len(rows) / int(nr_rows)
61
        pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0
62
        ret = {'page': page,
63
               'total': pages,
64
               'records': len(rows),
65
               'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]}
66
67
        return json.dumps(ret)
68