bika.health.browser.disease.getdiseases   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 43
dl 0
loc 75
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C ajaxGetDiseases.__call__() 0 41 11
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
"""Fetch diseases from ICD and bika_setup tables
22
"""
23
import json
24
from operator import itemgetter
25
26
import plone
27
from bika.health.icd9cm import icd9_codes
28
from bika.lims.browser import BrowserView
29
30
31
class ajaxGetDiseases(BrowserView):
32
    """ Diseases from ICD
33
    """
34
    def __call__(self):
35
        plone.protect.CheckAuthenticator(self.request)
36
        searchTerm = 'searchTerm' in self.request and self.request['searchTerm'].lower() or ''
37
        page = self.request['page']
38
        nr_rows = self.request['rows']
39
        sord = self.request['sord']
40
        sidx = self.request['sidx']
41
        rows = []
42
43
        # lookup objects from ZODB
44
        brains = self.bika_setup_catalog(portal_type='Disease',
45
                                         is_active=True)
46
        if brains and searchTerm:
47
            brains = [p for p in brains if p.Title.lower().find(searchTerm) > -1
48
                      or p.Description.lower().find(searchTerm) > -1]
49
        for p in brains:
50
            p = p.getObject()
51
            rows.append({'Code': p.getICDCode(),
52
                         'Title': p.Title(),
53
                         'Description': p.Description()})
54
55
        for icdprefix in icd9_codes.keys():
56
            for icd9 in icd9_codes[icdprefix]:
57
                if (str(icdprefix) + str(icd9['code'])).lower().find(searchTerm) > -1 \
58
                    or icd9['short'].lower().find(searchTerm) > -1 \
59
                        or icd9['long'].lower().find(searchTerm) > -1:
60
                    rows.append({'Code': str(icdprefix) + str(icd9['code']),
61
                                 'Title': icd9['short'],
62
                                 'Description': icd9['long']})
63
64
        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...
65
        if sord == 'desc':
66
            rows.reverse()
67
        pages = len(rows) / int(nr_rows)
68
        pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0
69
        ret = {'page': page,
70
               'total': pages,
71
               'records': len(rows),
72
               'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]}
73
74
        return json.dumps(ret)
75