Passed
Pull Request — master (#152)
by Pau
04:10
created

SamplePatientFieldsVisibility.isVisible()   A

Complexity

Conditions 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.3333
c 0
b 0
f 0
cc 5
nop 4
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.lims.adapters.widgetvisibility import SenaiteATWidgetVisibility
22
from bika.lims.interfaces import IBatch
23
24
25
class SamplePatientFieldsVisibility(SenaiteATWidgetVisibility):
26
    """
27
    Handles "Batch", "Patient" and "ClientPatientID" fields visibility in Sample (Analysis Request) context. They are
28
    not editable, regardless of the current state of the Sample, except when displayed in AR Add view. The reason is
29
    that all these fields, together with Client field, are strongly related.
30
    """
31
    def __init__(self, context):
32
        super(SamplePatientFieldsVisibility, self).__init__(
33
            context=context, sort=10,
34
            field_names=["Batch", "Patient", "ClientPatientID", ])
35
36
    def isVisible(self, field, mode="view", default="visible"):
37
        if mode == "edit":
38
            return "invisible"
39
40
        elif mode == "add":
41
            container = self.context.aq_parent
42
43
            # Do not display the Patient field if the Sample is being created
44
            # inside a Batch and the latter has a Patient assigned. In such
45
            # case, the patient assigned to the Batch will be used:
46
            # See: adapters.addsample.PatientDefaultFieldValue
47
            if IBatch.providedBy(container):
48
                patient = container.getField("Patient").get(container)
49
                if patient:
50
                    return "hidden"
51
52
        return default
53
54
55 View Code Duplication
class DoctorFieldVisibility(SenaiteATWidgetVisibility):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    """Handles Doctor field visibility in Sample add form and view
57
    """
58
59
    def __init__(self, context):
60
        super(DoctorFieldVisibility, self).__init__(
61
            context=context, sort=10, field_names=["Doctor"])
62
63
    def isVisible(self, field, mode="view", default="visible"):
64
        if mode == "add":
65
            container = self.context.aq_parent
66
67
            # Do not display the doctor field if the Sample is being created
68
            # inside a Batch and the latter has a Doctor assigned. In such
69
            # case, the batch assigned to the Batch will be used:
70
            # See: adapters.addsample.DoctorDefaultFieldValue
71
            if IBatch.providedBy(container):
72
                doctor = container.getField("Doctor").get(container)
73
                if doctor:
74
                    return "hidden"
75
76
        return default
77
78 View Code Duplication
class PatientClientFieldsVisibility(SenaiteATWidgetVisibility):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
79
    """Handles Doctor field visibility in Sample add form and view
80
    """
81
82
    def __init__(self, context):
83
        super(DoctorFieldVisibility, self).__init__(
84
            context=context, sort=10, field_names=["Doctor"])
85
86
    def isVisible(self, field, mode="view", default="visible"):
87
        if mode == "add":
88
            container = self.context.aq_parent
89
90
            # Do not display the doctor field if the Sample is being created
91
            # inside a Batch and the latter has a Doctor assigned. In such
92
            # case, the batch assigned to the Batch will be used:
93
            # See: adapters.addsample.DoctorDefaultFieldValue
94
            if IBatch.providedBy(container):
95
                doctor = container.getField("Doctor").get(container)
96
                if doctor:
97
                    return "hidden"
98
99
        return default
100