ajaxGetSymptoms.__call__()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 39
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 39
rs 7.688
c 0
b 0
f 0
cc 7
nop 1
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
from Products.CMFCore.utils import getToolByName
22
from Products.ZCTextIndex.ParseTree import ParseError
23
from bika.health.icd9cm import icd9_codes
24
from bika.lims.browser import BrowserView
25
from operator import itemgetter
26
import json
27
import plone
28
29
30
class ajaxGetSymptoms(BrowserView):
31
    """ Symptoms from ICD and Site Setup
32
    """
33
    def __call__(self):
34
        plone.protect.CheckAuthenticator(self.request)
35
        title = 'title' in self.request and self.request['title'] or ''
36
        self.searchTerm = (len(title) == 0 and 'searchTerm' in self.request) and self.request['searchTerm'].lower() or title.lower()
37
        page = 'page' in self.request and self.request['page'] or 1
38
        nr_rows = 'rows' in self.request and self.request['rows'] or 10
39
        sord = 'sord' in self.request and self.request['sord'] or 'asc'
40
        sidx = 'sidx' in self.request and self.request['sidx'] or 'Title'
41
        rows = []
42
43
        # lookup objects from ZODB
44
        brains = self.bika_setup_catalog(portal_type='Symptom',
45
                                         is_active=True)
46
        if brains and self.searchTerm:
47
            for brain in brains:
48
                obj = brain.getObject()
49
                symptom = {'Code': obj.getCode(),
50
                           'Title': obj.Title(),
51
                           'Description': obj.Description()}
52
                rows.append(symptom)
53
54
        # lookup objects from ICD code list
55
        for icd9 in icd9_codes['R']:
56
            rows.append({'Code': icd9['code'],
57
                         'Title': icd9['short'],
58
                         'Description': icd9['long']})
59
60
        rows = self.filterBySearchCriteria(rows)
61
        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...
62
        if sord == 'desc':
63
            rows.reverse()
64
        pages = len(rows) / int(nr_rows)
65
        pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0
66
        ret = {'page': page,
67
               'total': pages,
68
               'records': len(rows),
69
               'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]}
70
71
        return json.dumps(ret)
72
73
    def filterBySearchCriteria(self, rows):
74
        """
75
        Used to filter by code, title or description when you are typing the fields
76
        """
77
        return [r for r in rows if 
78
                r.get('Code','').lower().find(self.searchTerm) > -1 or
79
                r.get('Title','').lower().find(self.searchTerm) > -1 or
80
                r.get('Description','').lower().find(self.searchTerm) > -1]
81
82
83
class ajaxGetSymptomsByCode(ajaxGetSymptoms):
84
85
    def filterBySearchCriteria(self, rows):
86
        """
87
        Used to filter by code when you are typing on code field
88
        """
89
        return [r for r in rows if r.get('Code','').lower().find(self.searchTerm) > -1]
90
91
92
class ajaxGetSymptomsByTitle(ajaxGetSymptoms):
93
94
    def filterBySearchCriteria(self, rows):
95
        """
96
        Used to filter by title when you are typing on title field
97
        """
98
        return [r for r in rows if r.get('Title','').lower().find(self.searchTerm) > -1]
99
100
101
class ajaxGetSymptomsByDesc(ajaxGetSymptoms):
102
103
    def filterBySearchCriteria(self, rows):
104
        """
105
        Used to filter by description when you are typing on
106
        description field
107
        """
108
        return [r for r in rows if r.get('Description','').lower().find(self.searchTerm) > -1]
109