bika.health.browser.immunization.getimmunizations   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 61.64 %

Importance

Changes 0
Metric Value
wmc 6
eloc 33
dl 45
loc 73
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B ajaxGetImmunizations.__call__() 42 42 6

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 ajaxGetImmunizations(BrowserView):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
29
    """ Immunizations vocabulary source for jquery combo dropdown box
30
    """
31
    def __call__(self):
32
        plone.protect.CheckAuthenticator(self.request)
33
        searchTerm = 'searchTerm' in self.request and self.request['searchTerm'].lower() or ''
34
        page = self.request['page']
35
        nr_rows = self.request['rows']
36
        sord = self.request['sord']
37
        sidx = self.request['sidx']
38
        rows = []
39
40
        # lookup objects from ZODB
41
        brains = self.bika_setup_catalog(portal_type='Immunization',
42
                                         is_active=True)
43
        if brains and searchTerm:
44
            brains = [p for p in brains if p.Title.lower().find(searchTerm) > -1]
45
46
        for p in brains:
47
            rows.append({'Title': p.Title,
48
                         'UID': p.UID,
49
                         'Description': p.Description,
50
                         'Immunization': p.Title #This one is used to
51
                                                 #avoid content/
52
                                                 #patient.py L221 to
53
                                                 #use Title to fill
54
                                                 #the box, it could be
55
                                                 #ambiguous for other
56
                                                 #parameters like
57
                                                 #<<Vaccination
58
                                                 #Center>> which could
59
                                                 #use Title too.
60
                         })
61
62
        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...
63
        if sord == 'desc':
64
            rows.reverse()
65
        pages = len(rows) / int(nr_rows)
66
        pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0
67
        ret = {'page': page,
68
               'total': pages,
69
               'records': len(rows),
70
               'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]}
71
72
        return json.dumps(ret)
73