ajaxGetDoctors.__call__()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 40
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 40
rs 8.1546
c 0
b 0
f 0
cc 6
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
import json
22
from operator import itemgetter
23
24
import plone
25
from bika.lims import api
26
from bika.lims.browser import BrowserView
27
from bika.lims.interfaces import ILabContact, IClient
28
29
30
class ajaxGetDoctors(BrowserView):
31
    """ vocabulary source for jquery combo dropdown box
32
    """
33
    def __call__(self):
34
        plone.protect.CheckAuthenticator(self.request)
35
        searchTerm = 'searchTerm' in self.request and self.request['searchTerm'].lower() or ''
36
        page = self.request['page']
37
        nr_rows = self.request['rows']
38
        sord = self.request['sord']
39
        sidx = self.request['sidx']
40
        rows = []
41
42
        query = dict(portal_type="Doctor", is_active=True)
43
        client = self.get_current_client()
44
        if client:
45
            # Search those Doctors that are assigned to the same client or
46
            # that do not have any client assigned
47
            query["getPrimaryReferrerUID"] = [api.get_uid(client), None]
48
49
        doctors = api.search(query, 'portal_catalog')
50
        for doctor in doctors:
51
            doctor_id = doctor.id
52
            doctor_title = doctor.Title
53
            search_val = ('{} {}'.format(doctor_id, doctor_title)).lower()
54
            if searchTerm not in search_val:
55
                continue
56
57
            rows.append({'Title': doctor.Title() or '',
58
                         'DoctorID': doctor.getDoctorID(),
59
                         'DoctorSysID': doctor.id,
60
                         'DoctorUID': doctor.UID()})
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 = {'pages': 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
74
    def get_current_client(self, default=None):
75
        """Returns the client the current user belongs to
76
        """
77
        user = api.get_current_user()
78
        roles = user.getRoles()
79
        if 'Client' not in roles:
80
            return default
81
82
        contact = api.get_user_contact(user)
83
        if not contact or ILabContact.providedBy(contact):
84
            return default
85
86
        client = api.get_parent(contact)
87
        if not client or not IClient.providedBy(client):
88
            return default
89
90
        return client
91