BatchListingViewAdapter.before_render()   A
last analyzed

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