DoctorsView.__call__()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 39
rs 8.6453
c 0
b 0
f 0
cc 5
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 collections import OrderedDict
22
23
from Products.CMFCore.utils import getToolByName
24
from bika.health import bikaMessageFactory as _
25
from bika.health.permissions import *
26
from bika.lims import api
27
from bika.lims.browser.client import ClientContactsView
28
from bika.lims.interfaces import IClient
29
from bika.lims.utils import get_link
30
from plone.memoize import view as viewcache
31
32
33
class DoctorsView(ClientContactsView):
34
35
    def __init__(self, context, request):
36
        super(DoctorsView, self).__init__(context, request)
37
        self.contentFilter = {'portal_type': 'Doctor',
38
                              'sort_on': 'sortable_title'}
39
        self.context_actions = {}
40
        self.title = self.context.translate(_("Doctors"))
41
        self.icon = self.portal_url + "/++resource++bika.health.images/doctor_big.png"
42
        self.description = ""
43
44
        self.columns = OrderedDict((
45
            ("getDoctorID", {
46
                "title": _('Doctor ID'),
47
                "index": "getDoctorID",
48
                "sortable": True, }),
49
            ("getFullname", {
50
                "title": _("Full Name"),
51
                "index": "getFullname",
52
                "sortable": True, }),
53
            ("getPrimaryReferrer", {
54
                "title": _("Primary Referrer"),
55
                "index": "getPrimaryReferrerUID",
56
                "sortable": True, }),
57
            ("Username", {
58
                "title": _("User Name"), }),
59
            ("getEmailAddress", {
60
                "title": _("Email Address"), }),
61
            ("getBusinessPhone", {
62
                "title": _("Business Phone"), }),
63
            ("getMobilePhone", {
64
                "title": _("MobilePhone"), }),
65
        ))
66
67
        self.review_states = [
68
            {'id':'default',
69
             'title': _('Active'),
70
             'contentFilter': {'is_active': True},
71
             'transitions': [],
72
             'columns': self.columns.keys()},
73
        ]
74
75
    def __call__(self):
76
        mtool = getToolByName(self.context, 'portal_membership')
77
        can_add_doctors = mtool.checkPermission(AddDoctor, self.context)
78
        if can_add_doctors:
79
            add_doctors_url = '{}/doctors/createObject?type_name=Doctor' \
80
                .format(self.portal_url)
81
            self.context_actions[_('Add')] = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
82
                'url': add_doctors_url,
83
                'icon': '++resource++bika.lims.images/add.png'
84
            }
85
        if mtool.checkPermission(ManageDoctors, self.context):
86
            self.review_states[0]['transitions'].append({'id':'deactivate'})
87
            self.review_states.append(
88
                {'id':'inactive',
89
                 'title': _('Dormant'),
90
                 'contentFilter': {'is_active': False},
91
                 'transitions': [{'id':'activate'}, ],
92
                 'columns': self.columns.keys()})
93
            self.review_states.append(
94
                {'id':'all',
95
                 'title': _('All'),
96
                 'contentFilter':{},
97
                 'transitions':[{'id':'empty'}],
98
                 'columns': self.columns.keys()})
99
            stat = self.request.get("%s_review_state"%self.form_id, 'default')
100
            self.show_select_column = stat != 'all'
101
102
        # If the current context is a Client, filter Doctors by Client UID
103
        if IClient.providedBy(self.context):
104
            client_uid = api.get_uid(self.context)
105
            self.contentFilter['getPrimaryReferrerUID'] = client_uid
106
107
        # If the current user is a client contact, do not display the doctors
108
        # assigned to other clients
109
        elif self.get_user_client_uid():
110
            client_uid = self.get_user_client_uid()
111
            self.contentFilter['getPrimaryReferrerUID'] = [client_uid, None]
112
113
        return super(DoctorsView, self).__call__()
114
115
    def before_render(self):
116
        """Before template render hook
117
        """
118
        super(DoctorsView, self).before_render()
119
        # Don't allow any context actions on Doctors folder
120
        self.request.set("disable_border", 1)
121
122
    @viewcache.memoize
123
    def get_user_client_uid(self, default=None):
124
        """Returns the id of the client the current user belongs to
125
        """
126
        client = api.get_current_client()
127
        if client:
128
            return api.get_uid(client)
129
        return default
130
131
    def folderitem(self, obj, item, index):
132
        """Applies new properties to the item to be rendered
133
        """
134
        item = super(DoctorsView, self).folderitem(obj, item, index)
135
        url = item.get("url")
136
        doctor_id = item.get("getDoctorID")
137
        item['replace']['getDoctorID'] = get_link(url, value=doctor_id)
138
        item['getPrimaryReferrer'] = ""
139
        doctor = api.get_object(obj)
140
        pri = doctor.getPrimaryReferrer()
141
        if pri:
142
            pri_url = pri.absolute_url()
143
            pri = pri.Title()
144
            item['replace']['getPrimaryReferrer'] = get_link(pri_url, value=pri)
145
        return item
146