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 import logger |
22
|
|
|
from bika.health.interfaces import IPatient |
23
|
|
|
from bika.lims import api |
24
|
|
|
from bika.lims.api import _marker |
25
|
|
|
from bika.lims.interfaces import IBatch |
26
|
|
|
from bika.lims.utils import render_html_attributes, to_utf8, to_unicode |
27
|
|
|
from zope.i18n import translate |
28
|
|
|
from Products.Archetypes.utils import addStatusMessage |
29
|
|
|
|
30
|
|
|
from bika.lims.utils import tmpID |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def get_obj_from_field(instance, fieldname, default=_marker): |
34
|
|
|
""" Get an object from a Reference Field of any instance |
35
|
|
|
:param fieldname: Reference Field Name |
36
|
|
|
:return: |
37
|
|
|
""" |
38
|
|
|
if not fieldname: |
39
|
|
|
return default |
40
|
|
|
field = instance.getField(fieldname, None) |
41
|
|
|
if not field: |
42
|
|
|
if default is not _marker: |
43
|
|
|
return default |
44
|
|
|
api.fail("{} doesn't have field called {} ".format(repr(instance), |
45
|
|
|
fieldname)) |
46
|
|
|
return field.get(instance) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def get_attr_from_field(instance, fieldname, attrname, default=None): |
50
|
|
|
item = get_obj_from_field(instance, fieldname, None) |
51
|
|
|
if not item: |
52
|
|
|
return default |
53
|
|
|
return api.safe_getattr(item, attr=attrname, default=default) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def get_html_image(name, **kwargs): |
57
|
|
|
"""Returns a well-formed img html |
58
|
|
|
:param name: file name of the image |
59
|
|
|
:param kwargs: additional attributes and values |
60
|
|
|
:return: a well-formed html img |
61
|
|
|
""" |
62
|
|
|
if not name: |
63
|
|
|
return "" |
64
|
|
|
attr = render_html_attributes(**kwargs) |
65
|
|
|
return '<img src="{}" {}/>'.format(get_resource_url(name), attr) |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def get_resource_url(resource, route="++resource++bika.health.images"): |
69
|
|
|
"""Returns the url for the given resource name |
70
|
|
|
""" |
71
|
|
|
portal_url = api.get_url(api.get_portal()) |
72
|
|
|
return "{}/{}/{}".format(portal_url, route, resource) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def translate_i18n(i18n_msg): |
76
|
|
|
"""Safely translate and convert to UTF8, any zope i18n msgid returned from |
77
|
|
|
senaite health's message factory |
78
|
|
|
""" |
79
|
|
|
text = to_unicode(i18n_msg) |
80
|
|
|
try: |
81
|
|
|
request = api.get_request() |
82
|
|
|
domain = getattr(i18n_msg, "domain", "senaite.health") |
83
|
|
|
text = translate(text, domain=domain, context=request) |
84
|
|
|
except UnicodeDecodeError: |
85
|
|
|
logger.warn("{} couldn't be translated".format(text)) |
86
|
|
|
return to_utf8(text) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def get_field_value(instance, field_name, default=_marker): |
90
|
|
|
"""Returns the value of a Schema field from the instance passed in |
91
|
|
|
""" |
92
|
|
|
instance = api.get_object(instance) |
93
|
|
|
field = instance.Schema() and instance.Schema().getField(field_name) or None |
94
|
|
|
if not field: |
95
|
|
|
if default is not _marker: |
96
|
|
|
return default |
97
|
|
|
api.fail("No field {} found for {}".format(field_name, repr(instance))) |
98
|
|
|
return instance.Schema().getField(field_name).get(instance) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
def set_field_value(instance, field_name, value): |
102
|
|
|
"""Sets the value to a Schema field |
103
|
|
|
""" |
104
|
|
|
if field_name == "id": |
105
|
|
|
logger.warn("Assignment of id is not allowed") |
106
|
|
|
return |
107
|
|
|
logger.info("Field {} = {}".format(field_name, repr(value))) |
108
|
|
|
instance = api.get_object(instance) |
109
|
|
|
field = instance.Schema() and instance.Schema().getField(field_name) or None |
110
|
|
|
if not field: |
111
|
|
|
api.fail("No field {} found for {}".format(field_name, repr(instance))) |
112
|
|
|
field.set(instance, value) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
def get_default_num_samples(): |
116
|
|
|
"""Returns the num of Samples (Columns) to be displayed in Sample Add Form |
117
|
|
|
""" |
118
|
|
|
ar_count = api.get_setup().getDefaultNumberOfARsToAdd() |
119
|
|
|
return api.to_int(ar_count, 1) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def handle_after_submit(context, request, state): |
123
|
|
|
"""Handles actions provided in extra_buttons slot from edit forms |
124
|
|
|
""" |
125
|
|
|
if request.get("form.button.new_sample"): |
126
|
|
|
# Redirect to Sample Add from Patient |
127
|
|
|
next_url = api.get_url(context) |
128
|
|
|
if IPatient.providedBy(context): |
129
|
|
|
uid = context.UID() |
130
|
|
|
client = context.getPrimaryReferrer() |
131
|
|
|
folder = client or api.get_portal().analysisrequests |
132
|
|
|
folder_url = api.get_url(folder) |
133
|
|
|
ar_count = get_default_num_samples() |
134
|
|
|
next_url = "{}/ar_add?Patient={}&ar_count={}".format( |
135
|
|
|
folder_url, uid, ar_count) |
136
|
|
|
|
137
|
|
|
# Redirect to Sample Add form from Batch |
138
|
|
|
elif IBatch.providedBy(context): |
139
|
|
|
ar_count = get_default_num_samples() |
140
|
|
|
next_url = "{}/ar_add?ar_count={}".format(next_url, ar_count) |
141
|
|
|
|
142
|
|
|
state.setNextAction('redirect_to:string:{}'.format(next_url)) |
143
|
|
|
|
144
|
|
|
elif request.get("form.button.new_batch"): |
145
|
|
|
# Redirect to New Batch from Patient |
146
|
|
|
next_url = api.get_url(context) |
147
|
|
|
if IPatient.providedBy(context): |
148
|
|
|
client = context.getPrimaryReferrer() |
149
|
|
|
folder = client or api.get_portal().batches |
150
|
|
|
|
151
|
|
|
# Create a temporary Batch |
152
|
|
|
tmp_path = "portal_factory/Batch/{}".format(tmpID()) |
153
|
|
|
tmp_obj = folder.restrictedTraverse(tmp_path) |
154
|
|
|
|
155
|
|
|
# Redirect to Batch's edit view with Patient's uid as a param |
156
|
|
|
batch_url = api.get_url(tmp_obj) |
157
|
|
|
uid = api.get_uid(context) |
158
|
|
|
next_url = "{}/edit?Patient={}".format(batch_url, uid) |
159
|
|
|
|
160
|
|
|
state.setNextAction('redirect_to:string:{}'.format(next_url)) |
161
|
|
|
|
162
|
|
|
elif IPatient.providedBy(context): |
163
|
|
|
# Redirect to Patient's samples view |
164
|
|
|
next_url = "{}/analysisrequests".format(api.get_url(context)) |
165
|
|
|
state.setNextAction('redirect_to:string:{}'.format(next_url)) |
166
|
|
|
|