Passed
Push — master ( 3ee9f7...32807f )
by Jordi
03:14
created

BatchListingViewAdapter.before_render()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
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 collections
22
23
from bika.health import bikaMessageFactory as _
24
from bika.health.utils import get_field_value
25
from bika.lims import api
26
from bika.lims.api import security
27
from bika.lims.interfaces import IClient
28
from bika.lims.permissions import AddBatch
29
from bika.lims.utils import get_link
30
from senaite.core.listing import utils
31
from senaite.core.listing.interfaces import IListingView, IListingViewAdapter
32
from zope.component import adapts
33
from zope.interface import implements
34
35
36
class BatchListingViewAdapter(object):
37
    adapts(IListingView)
38
    implements(IListingViewAdapter)
39
40
    # Order of priority of this subscriber adapter over others
41
    priority_order = 1
42
43
    def __init__(self, listing, context):
44
        self.listing = listing
45
        self.context = context
46
        self.title = self.context.translate(_("Cases"))
47
48
    def before_render(self):
49
        # Additional columns
50
        self.add_columns()
51
        # Remove unnecessary columns
52
        self.hide_columns()
53
54
55
    def folder_item(self, obj, item, index):
56
        batch = api.get_object(obj)
57
        # Doctor
58
        doctor = get_field_value(batch, "Doctor", None)
59
        item["Doctor"] = doctor and doctor.Title() or ""
60
        item["replace"]["Doctor"] = doctor and get_link(api.get_url(doctor),
61
                                                        doctor.Title())
62
        # Onset Date
63
        onset = get_field_value(batch, "OnsetDate", None)
64
        item["OnsetDate"] = onset and self.listing.ulocalized_time(onset) or ""
65
66
        # Patient
67
        item["Patient"] = ""
68
        item["getPatientID"] = ""
69
        item["getClientPatientID"] = ""
70
        patient = get_field_value(batch, "Patient", None)
71
        if patient:
72
            url = api.get_url(patient)
73
            item["Patient"] = patient.Title()
74
            item["replace"]["Patient"] = get_link(url, patient.Title())
75
76
            item["getPatientID"] = patient.id
77
            item["replace"]["getPatientID"] = get_link(url, patient.id)
78
79
            pid = patient.getClientPatientID()
80
            pid_link = pid and get_link(url, pid) or ""
81
            item["getClientPatientID"] = pid or ""
82
            item["replace"]["getClientPatientID"] = pid_link
83
84
        return item
85
86
    def is_client_context(self):
87
        """Returns whether the batch listing is displayed in IClient context
88
        or if the current user is a client contact
89
        """
90
        return api.get_current_client() or IClient.providedBy(self.context)
91
92
    def hide_columns(self):
93
        # Columns to hide
94
        hide = ["Title", "BatchDate", "Description",]
95
        if api.get_current_client():
96
            # Hide client-specific columns
97
            hide.extend(["Client", "ClientID"])
98
99
        # Remove the columns from all review_states
100
        for rv in self.listing.review_states:
101
            rv_columns = rv.get("columns", self.listing.columns.keys())
102
            rv_columns = filter(lambda col: col not in hide, rv_columns)
103
            rv["columns"] = rv_columns
104
105
    def add_columns(self):
106
        """Adds health-specific columns in the listing
107
        """
108
        is_client_context = self.is_client_context()
109
        health_columns = collections.OrderedDict((
110
            ("getPatientID", {
111
                "title": _("Patient ID"),
112
                "toggle": is_client_context,
113
                "after": "ClientBatchID", }),
114
            ("getClientPatientID", {
115
                "title": _("Client PID"),
116
                "toggle": is_client_context,
117
                "after": "getPatientID", }),
118
            ("Patient", {
119
                "title": _("Patient"),
120
                "toggle": is_client_context,
121
                "index": "getPatientTitle",
122
                "after": "getClientPatientID", }),
123
            ("Doctor", {
124
                "title": _("Doctor"),
125
                "toggle": True,
126
                "index": "getDoctorTitle",
127
                "after": "Patient", }),
128
            ("OnsetDate", {
129
                "title": _("Onset Date"),
130
                "toggle": True,
131
                "after": "Patient", }),
132
        ))
133
134
        # Add the columns
135
        rv_keys = map(lambda r: r["id"], self.listing.review_states)
136
        for column_id, column_values in health_columns.items():
137
            utils.add_column(listing=self.listing,
138
                             column_id=column_id,
139
                             column_values=column_values,
140
                             after=column_values["after"],
141
                             review_states=rv_keys)
142