Passed
Pull Request — master (#152)
by Pau
05:24 queued 01:18
created

PatientClientFieldVisibility.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
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 bika.health.interfaces import IPatient
22
from bika.lims.adapters.widgetvisibility import SenaiteATWidgetVisibility
23
from bika.lims.interfaces import IBatch
24
from bika.lims.interfaces import IClient
25
26
27
class SamplePatientFieldsVisibility(SenaiteATWidgetVisibility):
28
    """
29
    Handles "Batch", "Patient" and "ClientPatientID" fields visibility in Sample (Analysis Request) context. They are
30
    not editable, regardless of the current state of the Sample, except when displayed in AR Add view. The reason is
31
    that all these fields, together with Client field, are strongly related.
32
    """
33
    def __init__(self, context):
34
        super(SamplePatientFieldsVisibility, self).__init__(
35
            context=context, sort=10,
36
            field_names=["Batch", "Patient", "ClientPatientID", ])
37
38
    def isVisible(self, field, mode="view", default="visible"):
39
        if mode == "edit":
40
            return "invisible"
41
42
        elif mode == "add":
43
            container = self.context.aq_parent
44
45
            # Do not display the Patient field if the Sample is being created
46
            # inside a Batch and the latter has a Patient assigned. In such
47
            # case, the patient assigned to the Batch will be used:
48
            # See: adapters.addsample.PatientDefaultFieldValue
49
            if IBatch.providedBy(container):
50
                patient = container.getField("Patient").get(container)
51
                if patient:
52
                    return "hidden"
53
54
        return default
55
56
57
class DoctorFieldVisibility(SenaiteATWidgetVisibility):
58
    """Handles Doctor field visibility in Sample add form and view
59
    """
60
61
    def __init__(self, context):
62
        super(DoctorFieldVisibility, self).__init__(
63
            context=context, sort=10, field_names=["Doctor"])
64
65
    def isVisible(self, field, mode="view", default="visible"):
66
        if mode == "add":
67
            container = self.context.aq_parent
68
69
            # Do not display the doctor field if the Sample is being created
70
            # inside a Batch and the latter has a Doctor assigned. In such
71
            # case, the batch assigned to the Batch will be used:
72
            # See: adapters.addsample.DoctorDefaultFieldValue
73
            if IBatch.providedBy(container):
74
                doctor = container.getField("Doctor").get(container)
75
                if doctor:
76
                    return "hidden"
77
78
        return default
79
80
81
class PatientClientFieldVisibility(SenaiteATWidgetVisibility):
82
    """Handles the Client (PrimaryReferrer) field visibility in Patient context
83
    """
84
85
    def __init__(self, context):
86
        super(PatientClientFieldVisibility, self).__init__(
87
            context=context, sort=10, field_names=["PrimaryReferrer"])
88
89
    def isVisible(self, field, mode="view", default="visible"):
90
        if mode == "edit":
91
            container = self.context.aq_parent
92
93
            # Do not display the Client field if the Patient is created
94
            # or edited inside a Client.
95
            if IClient.providedBy(container):
96
                return "invisible"
97
98
        return default
99
100
101 View Code Duplication
class BatchClientFieldVisibility(SenaiteATWidgetVisibility):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
    """Handles the Client field visibility in Batch context
103
    """
104
105
    def __init__(self, context):
106
        super(BatchClientFieldVisibility, self).__init__(
107
            context=context, sort=10, field_names=["Client"])
108
109
    def isVisible(self, field, mode="view", default="visible"):
110
        if mode == "edit":
111
            container = self.context.aq_parent
112
113
            # Do not display the Client field if the Batch is created
114
            # or edited inside a Client or Patient.
115
            # Senaite Core is taking care of the visibility when the
116
            # Batch is created or edited inside a Client
117
            if IPatient.providedBy(container):
118
                return "invisible"
119
120
        return default
121
122
123 View Code Duplication
class BatchPatientFieldsVisibility(SenaiteATWidgetVisibility):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
124
    """Handles the visibility of Patient and ClientPatientID fields
125
     in a Batch context
126
    """
127
128
    def __init__(self, context):
129
        super(BatchPatientFieldsVisibility, self).__init__(
130
            context=context, sort=10, field_names=["Patient", "ClientPatientID"])
131
132
    def isVisible(self, field, mode="view", default="visible"):
133
        if mode == "edit":
134
            container = self.context.aq_parent
135
            if IPatient.providedBy(container):
136
                return "invisible"
137
138
        return default
139