bika.health.browser.analysisrequests.view   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 80
dl 0
loc 128
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A AnalysisRequestsView.__init__() 0 16 1
A AnalysisRequestsView.get_brain() 0 9 4
B AnalysisRequestsView.folderitem() 0 28 5
A AnalysisRequestAddView.__call__() 0 8 2
B AnalysisRequestsView.folderitems() 0 24 5
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 bika.health import bikaMessageFactory as _
23
from bika.health import logger
24
from bika.lims import api
25
from bika.lims.browser.analysisrequest import AnalysisRequestAddView as ARAdd
26
from bika.lims.browser.analysisrequest import AnalysisRequestsView as BaseView
27
from bika.lims.utils import get_link
28
from plone.memoize import view as viewcache
29
30
31
class AnalysisRequestAddView(ARAdd):
32
    """Dummy class to allow redirection when client tries to
33
    create an Analysis Request from analysisrequests base folder
34
    """
35
    def __call__(self):
36
        client = api.get_current_client()
37
        if client:
38
            url = api.get_url(client)
39
            ar_count = self.get_ar_count()
40
            return self.request.response.redirect("{}/ar_add?ar_count={}"
41
                                                  .format(url, ar_count))
42
        return super(AnalysisRequestAddView, self).__call__()
43
44
45
class AnalysisRequestsView(BaseView):
46
47
    def __init__(self, context, request):
48
        super(AnalysisRequestsView, self).__init__(context, request)
49
        self.columns['BatchID']['title'] = _('Case ID')
50
        # Add Client Patient fields
51
        self.columns['getPatientID'] = {
52
            'title': _('Patient ID'),
53
        }
54
        self.columns['getClientPatientID'] = {
55
            'title': _("Client PID"),
56
            'sortable': False,
57
        }
58
        self.columns['getPatientTitle'] = {
59
            'title': _('Patient'),
60
        }
61
        self.columns['getDoctorTitle'] = {
62
            'title': _('Doctor'),
63
        }
64
65
    def folderitems(self, full_objects=False):
66
        logger.info("*** Bika Health's folderitems ***")
67
        pm = getToolByName(self.context, "portal_membership")
68
        member = pm.getAuthenticatedMember()
69
        # We will use this list for each element
70
        roles = member.getRoles()
71
        # delete roles user doesn't have permissions
72
        if 'Manager' not in roles \
73
            and 'LabManager' not in roles \
74
                and 'LabClerk' not in roles:
75
            self.remove_column('getPatientID')
76
            self.remove_column('getClientPatientID')
77
            self.remove_column('getPatientTitle')
78
            self.remove_column('getDoctorTitle')
79
        # Otherwise show the columns in the list
80
        else:
81
            for rs in self.review_states:
82
                i = rs['columns'].index('BatchID') + 1
83
                rs['columns'].insert(i, 'getClientPatientID')
84
                rs['columns'].insert(i, 'getPatientID')
85
                rs['columns'].insert(i, 'getPatientTitle')
86
                rs['columns'].insert(i, 'getDoctorTitle')
87
        return super(AnalysisRequestsView, self).folderitems(
88
            full_objects=False, classic=False)
89
90
    @viewcache.memoize
91
    def get_brain(self, uid, catalog):
92
        if not api.is_uid(uid):
93
            return None
94
        query = dict(UID=uid)
95
        brains = api.search(query, catalog)
96
        if brains and len(brains) == 1:
97
            return brains[0]
98
        return None
99
100
    def folderitem(self, obj, item, index):
101
        item = super(AnalysisRequestsView, self)\
102
            .folderitem(obj, item, index)
103
104
        url = '{}/analysisrequests'.format(obj.getPatientURL)
105
        item['getPatientID'] = obj.getPatientID
106
        item['getPatientTitle'] = obj.getPatientTitle
107
        item['getClientPatientID'] = obj.getClientPatientID
108
109
        # Replace with Patient's URLs
110
        if obj.getClientPatientID:
111
            item['replace']['getClientPatientID'] = get_link(
112
                url, obj.getClientPatientID)
113
114
        if obj.getPatientTitle:
115
            item['replace']['getPatientTitle'] = get_link(
116
                url, obj.getPatientTitle)
117
118
        if obj.getPatientID:
119
            item['replace']['getPatientID'] = get_link(url, obj.getPatientID)
120
121
        # Doctor
122
        item['getDoctorTitle'] = obj.getDoctorTitle
123
        if obj.getDoctorURL:
124
            url = '{}/analysisrequests'.format(obj.getDoctorURL)
125
            item['replace']['getDoctorTitle'] = get_link(url, obj.getDoctorTitle)
126
127
        return item
128