bika.health.browser.patients.folder_view   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 175
Duplicated Lines 20 %

Importance

Changes 0
Metric Value
wmc 10
eloc 109
dl 35
loc 175
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A PatientsView.folderitem() 0 10 3
A PatientsView.folderitems() 0 3 1
B PatientsView.__init__() 0 78 1
A PatientsView.update() 35 35 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 collections
22
23
from bika.health import bikaMessageFactory as _
24
from bika.health.interfaces import IPatients
25
from bika.health.utils import get_resource_url
26
from bika.health.catalog import CATALOG_PATIENTS
27
from bika.health.permissions import AddPatient
28
from bika.lims import api
29
from bika.lims.api import security
30
from bika.lims.browser.bika_listing import BikaListingView
31
from bika.lims.interfaces import IClient
32
from bika.lims.utils import get_link
33
from plone.app.content.browser.interfaces import IFolderContentsView
34
from plone.app.layout.globals.interfaces import IViewView
35
from zope.interface import implements
36
from bika.lims.api.security import check_permission
37
38
39
class PatientsView(BikaListingView):
40
    """Listing View for all Patients in the System
41
    """
42
    implements(IFolderContentsView, IViewView)
43
44
    def __init__(self, context, request):
45
        super(PatientsView, self).__init__(context, request)
46
        self.title = self.context.translate(_("Patients"))
47
        self.icon = get_resource_url("patient_big.png")
48
        self.form_id = "list_health_patients"
49
        self.show_select_row = False
50
        self.show_select_column = True
51
        self.show_select_all_checkboxes = False
52
        request.set("disable_border", 1)
53
54
        self.sort_on = "created"
55
        self.catalog = CATALOG_PATIENTS
56
        self.contentFilter = {'portal_type': 'Patient',
57
                              'sort_on': 'created',
58
                              'sort_order': 'descending'}
59
60
        self.columns = collections.OrderedDict((
61
            ("Title", {
62
                "title": _("Patient"),
63
                "index": "getFullname",
64
                "replace_url": "absolute_url"}),
65
66
            ('getPatientID', {
67
                'title': _('Patient ID'),
68
                'index': 'getPatientID',
69
                'replace_url': 'getURL'}),
70
71
            ('getClientPatientID', {
72
                'title': _('Client PID'),
73
                'replace_url': 'getURL',
74
                'sortable': False}),
75
76
            ('getGender', {
77
                'title': _('Gender'),
78
                'toggle': True,
79
                'sortable': False}),
80
81
            ('getAgeSplittedStr', {
82
                'title': _('Age'),
83
                'toggle': True,
84
                'sortable': False}),
85
86
            ('getBirthDate', {
87
                'title': _('BirthDate'),
88
                'toggle': True,
89
                'sortable': False}),
90
91
            ('getPrimaryReferrerTitle', {
92
                'title': _('Primary Referrer'),
93
                'replace_url': 'getPrimaryReferrerURL',
94
                'toggle': True,
95
                'sortable': False}),
96
97
            ('getPatientIdentifiersStr', {
98
                'title': _('Additional IDs'),
99
                'attr': 'getPatientIdentifiersStr',
100
                'toggle': False,
101
                'sortable': False}),
102
        ))
103
104
        self.review_states = [
105
            {
106
                "id": "default",
107
                "title": _("Active"),
108
                "contentFilter": {"is_active": True},
109
                "transitions": [],
110
                "columns": self.columns.keys(),
111
            }, {
112
                "id": "inactive",
113
                "title": _("Inactive"),
114
                "contentFilter": {'is_active': False},
115
                "transitions": [],
116
                "columns": self.columns.keys(),
117
            }, {
118
                "id": "all",
119
                "title": _("All"),
120
                "contentFilter": {},
121
                "columns": self.columns.keys(),
122
            },
123
        ]
124
125 View Code Duplication
    def update(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
126
        """Before template render hook
127
        """
128
        super(PatientsView, self).update()
129
130
        if IPatients.providedBy(self.context):
131
            self.request.set("disable_border", 1)
132
133
        # By default, only users with AddPatient permissions for the current
134
        # context can add patients.
135
        self.context_actions = {
136
            _("Add"): {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
137
                "url": "createObject?type_name=Patient",
138
                "permission": AddPatient,
139
                "icon": "++resource++bika.lims.images/add.png"
140
            }
141
        }
142
143
        # If current user is a client contact and current context is not a
144
        # Client, then modify the url for Add action so the Patient gets created
145
        # inside the Client object to which the current user belongs. The
146
        # reason is that Client contacts do not have privileges to create
147
        # Patients inside portal/patients
148
        if not IClient.providedBy(self.context):
149
            # Get the client the current user belongs to
150
            client = api.get_current_client()
151
            if client and check_permission(AddPatient, client):
152
                add_url = self.context_actions[_("Add")]["url"]
153
                add_url = "{}/{}".format(api.get_url(client), add_url)
154
                self.context_actions[_("Add")]["url"] = add_url
155
                del(self.context_actions[_("Add")]["permission"])
156
157
        else:
158
            # The current context is a Client, remove the title column
159
            self.remove_column('getPrimaryReferrerTitle')
160
161
    def folderitems(self, full_objects=False, classic=False):
162
        # Force the folderitems to work with brains instead of objects
163
        return BikaListingView.folderitems(self, classic=classic)
164
165
    def folderitem(self, obj, item, index):
166
        item['getBirthDate'] = self.ulocalized_time(obj.getBirthDate)
167
        # make the columns patient title, patient ID and client patient ID
168
        # redirect to the Analysis Requests of the patient
169
        ars_url = "{}/{}".format(api.get_url(obj), "analysisrequests")
170
        for column in ['Title', 'getPatientID', 'getClientPatientID']:
171
            value = getattr(obj, column, None)
172
            if value:
173
                item["replace"][column] = get_link(ars_url, value)
174
        return item
175