Passed
Push — master ( bdefc6...4ef287 )
by Pau
10:04 queued 06:03
created

ClientDefaultFieldValue.__call__()   B

Complexity

Conditions 6

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 29
rs 8.5166
c 0
b 0
f 0
cc 6
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 IDoctor, IPatient
22
from bika.lims import api
23
from bika.lims.interfaces import IGetDefaultFieldValueARAddHook, IClient, IBatch
24
from zope.component import adapts
25
26
27
class AddFormFieldDefaultValueAdapter(object):
28
    """Generic adapter for objects retrieval based on request uid and field name
29
    """
30
31
    def __init__(self, request):
32
        self.request = request
33
34
    def get_object_from_request_field(self, field_name):
35
        """Returns the object for the field_name specified in the request
36
        """
37
        uid = self.request.get(field_name)
38
        return api.get_object_by_uid(uid, default=None)
39
40
41
class ClientDefaultFieldValue(AddFormFieldDefaultValueAdapter):
42
    """Adapter that returns the default value for field Client in Sample form
43
    """
44
45
    adapts(IGetDefaultFieldValueARAddHook)
46
47
    def __call__(self, context):
48
49
        if IClient.providedBy(context):
50
            return context
51
52
        # Try with client object explicitly defined in request
53
        client = self.get_object_from_request_field("Client")
54
        if client:
55
            return client
56
57
        # Try to get the client from selected Patient
58
        patient = PatientDefaultFieldValue(self.request)(context)
59
        client = patient and patient.getPrimaryReferrer() or None
60
        if client:
61
            return client
62
63
        # Try to get the client from selected Doctor
64
        doctor = DoctorDefaultFieldValue(self.request)(context)
65
        client = doctor and doctor.getPrimaryReferrer() or None
66
        if client:
67
            return client
68
69
        # Try to get the client from selected Batch
70
        batch = BatchDefaultFieldValue(self.request)(context)
71
        client = batch and batch.getClient() or None
72
        if client:
73
            return client
74
75
        return None
76
77
class PatientDefaultFieldValue(AddFormFieldDefaultValueAdapter):
78
    """Adapter that returns the default value for field Patient in Sample form
79
    """
80
    adapts(IGetDefaultFieldValueARAddHook)
81
82
    def __call__(self, context):
83
        if IPatient.providedBy(context):
84
            return context
85
86
        # Try with patient explicitly defined in request
87
        return self.get_object_from_request_field("Patient")
88
89
90
class DoctorDefaultFieldValue(AddFormFieldDefaultValueAdapter):
91
    """Adapter that returns the default value for field Doctor in Sample form
92
    """
93
    adapts(IGetDefaultFieldValueARAddHook)
94
95
    def __call__(self, context):
96
        if IDoctor.providedBy(context):
97
            return context
98
99
        # Try with doctor explicitly defined in request
100
        return self.get_object_from_request_field("Doctor")
101
102
103
class BatchDefaultFieldValue(AddFormFieldDefaultValueAdapter):
104
    """Adapter that returns the default value for field Batch in Sample form
105
    """
106
    adapts(IGetDefaultFieldValueARAddHook)
107
108
    def __call__(self, context):
109
        if IBatch.providedBy(context):
110
            return context
111
112
        # Try with batch explicitly defined in request
113
        return self.get_object_from_request_field("Batch")
114