sapiopycommons 2024.8.2a301__py3-none-any.whl → 2024.8.15a304__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/callbacks/callback_util.py +22 -59
- sapiopycommons/datatype/attachment_util.py +6 -15
- sapiopycommons/eln/experiment_handler.py +29 -66
- sapiopycommons/files/complex_data_loader.py +1 -1
- sapiopycommons/files/file_bridge.py +1 -1
- sapiopycommons/files/file_util.py +4 -13
- sapiopycommons/files/file_validator.py +2 -1
- sapiopycommons/general/aliases.py +1 -21
- sapiopycommons/recordmodel/record_handler.py +16 -78
- sapiopycommons/rules/eln_rule_handler.py +22 -6
- sapiopycommons/rules/on_save_rule_handler.py +28 -6
- sapiopycommons/webhook/webhook_handlers.py +23 -58
- {sapiopycommons-2024.8.2a301.dist-info → sapiopycommons-2024.8.15a304.dist-info}/METADATA +1 -1
- {sapiopycommons-2024.8.2a301.dist-info → sapiopycommons-2024.8.15a304.dist-info}/RECORD +16 -18
- sapiopycommons/general/audit_log.py +0 -200
- sapiopycommons/general/sapio_links.py +0 -48
- {sapiopycommons-2024.8.2a301.dist-info → sapiopycommons-2024.8.15a304.dist-info}/WHEEL +0 -0
- {sapiopycommons-2024.8.2a301.dist-info → sapiopycommons-2024.8.15a304.dist-info}/licenses/LICENSE +0 -0
|
@@ -15,7 +15,7 @@ from sapiopylib.rest.pojo.datatype.FieldDefinition import AbstractVeloxFieldDefi
|
|
|
15
15
|
from sapiopylib.rest.pojo.webhook.ClientCallbackRequest import OptionDialogRequest, ListDialogRequest, \
|
|
16
16
|
FormEntryDialogRequest, InputDialogCriteria, TableEntryDialogRequest, ESigningRequestPojo, \
|
|
17
17
|
DataRecordDialogRequest, InputSelectionRequest, FilePromptRequest, MultiFilePromptRequest, \
|
|
18
|
-
TempTableSelectionRequest
|
|
18
|
+
TempTableSelectionRequest
|
|
19
19
|
from sapiopylib.rest.pojo.webhook.ClientCallbackResult import ESigningResponsePojo
|
|
20
20
|
from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
|
|
21
21
|
from sapiopylib.rest.pojo.webhook.WebhookEnums import FormAccessLevel, ScanToSelectCriteria, SearchType
|
|
@@ -52,23 +52,11 @@ class CallbackUtil:
|
|
|
52
52
|
Set the width that dialogs will appear as for those dialogs that support specifying their width.
|
|
53
53
|
|
|
54
54
|
:param width_pixels: The number of pixels wide that dialogs will appear as.
|
|
55
|
-
:param width_percent: The percentage
|
|
56
|
-
will appear as.
|
|
55
|
+
:param width_percent: The percentage of the client's screen width that dialogs will appear as.
|
|
57
56
|
"""
|
|
58
57
|
self.width_pixels = width_pixels
|
|
59
58
|
self.width_percent = width_percent
|
|
60
|
-
|
|
61
|
-
def toaster_popup(self, message: str, title: str = "", popup_type: PopupType = PopupType.Info) -> None:
|
|
62
|
-
"""
|
|
63
|
-
Display a toaster popup in the bottom right corner of the user's screen.
|
|
64
|
-
|
|
65
|
-
:param message: The message to display in the toaster.
|
|
66
|
-
:param title: The title to display at the top of the toaster.
|
|
67
|
-
:param popup_type: The popup type to use for the toaster. This controls the color that the toaster appears with.
|
|
68
|
-
Info is blue, Success is green, Warning is yellow, and Error is red
|
|
69
|
-
"""
|
|
70
|
-
self.callback.display_popup(DisplayPopupRequest(title, message, popup_type))
|
|
71
|
-
|
|
59
|
+
|
|
72
60
|
def option_dialog(self, title: str, msg: str, options: list[str], default_option: int = 0,
|
|
73
61
|
user_can_cancel: bool = False) -> str:
|
|
74
62
|
"""
|
|
@@ -83,8 +71,7 @@ class CallbackUtil:
|
|
|
83
71
|
SapioUserCancelledException is thrown.
|
|
84
72
|
:return: The name of the button that the user selected.
|
|
85
73
|
"""
|
|
86
|
-
request = OptionDialogRequest(title, msg, options, default_option, user_can_cancel
|
|
87
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
74
|
+
request = OptionDialogRequest(title, msg, options, default_option, user_can_cancel)
|
|
88
75
|
response: int | None = self.callback.show_option_dialog(request)
|
|
89
76
|
if response is None:
|
|
90
77
|
raise SapioUserCancelledException()
|
|
@@ -124,20 +111,16 @@ class CallbackUtil:
|
|
|
124
111
|
"""
|
|
125
112
|
return self.option_dialog(title, msg, ["Yes", "No"], 0 if default_yes else 1, False) == "Yes"
|
|
126
113
|
|
|
127
|
-
def list_dialog(self, title: str, options: list[str], multi_select: bool = False
|
|
128
|
-
preselected_values: list[str] | None = None) -> list[str]:
|
|
114
|
+
def list_dialog(self, title: str, options: list[str], multi_select: bool = False) -> list[str]:
|
|
129
115
|
"""
|
|
130
116
|
Create a list dialog with the given options for the user to choose from.
|
|
131
117
|
|
|
132
118
|
:param title: The title of the dialog.
|
|
133
119
|
:param options: The list options that the user has to choose from.
|
|
134
120
|
:param multi_select: Whether the user is able to select multiple options from the list.
|
|
135
|
-
:param preselected_values: A list of values that will already be selected when the list dialog is created. The
|
|
136
|
-
user can unselect these values if they want to.
|
|
137
121
|
:return: The list of options that the user selected.
|
|
138
122
|
"""
|
|
139
|
-
request = ListDialogRequest(title, multi_select, options
|
|
140
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
123
|
+
request = ListDialogRequest(title, multi_select, options)
|
|
141
124
|
response: list[str] | None = self.callback.show_list_dialog(request)
|
|
142
125
|
if response is None:
|
|
143
126
|
raise SapioUserCancelledException()
|
|
@@ -180,6 +163,8 @@ class CallbackUtil:
|
|
|
180
163
|
builder = FormBuilder(data_type, display_name, plural_display_name)
|
|
181
164
|
for field_def in fields:
|
|
182
165
|
field_name = field_def.data_field_name
|
|
166
|
+
if values and hasattr(field_def, "default_value"):
|
|
167
|
+
field_def.default_value = values.get(field_name)
|
|
183
168
|
column: int = 0
|
|
184
169
|
span: int = 4
|
|
185
170
|
if column_positions and field_name in column_positions:
|
|
@@ -188,8 +173,7 @@ class CallbackUtil:
|
|
|
188
173
|
span = position[1]
|
|
189
174
|
builder.add_field(field_def, column, span)
|
|
190
175
|
|
|
191
|
-
request = FormEntryDialogRequest(title, msg, builder.get_temporary_data_type()
|
|
192
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
176
|
+
request = FormEntryDialogRequest(title, msg, builder.get_temporary_data_type())
|
|
193
177
|
response: FieldMap | None = self.callback.show_form_entry_dialog(request)
|
|
194
178
|
if response is None:
|
|
195
179
|
raise SapioUserCancelledException()
|
|
@@ -231,13 +215,13 @@ class CallbackUtil:
|
|
|
231
215
|
modifier = FieldModifier(visible=True, editable=editable)
|
|
232
216
|
|
|
233
217
|
# Build the form using only those fields that are desired.
|
|
234
|
-
values: dict[str, Any] = {}
|
|
235
218
|
builder = FormBuilder(data_type, type_def.display_name, type_def.plural_display_name)
|
|
236
219
|
for field_name in fields:
|
|
237
220
|
field_def = field_defs.get(field_name)
|
|
238
221
|
if field_def is None:
|
|
239
222
|
raise SapioException(f"No field of name \"{field_name}\" in field definitions of type \"{data_type}\"")
|
|
240
|
-
|
|
223
|
+
if hasattr(field_def, "default_value"):
|
|
224
|
+
field_def.default_value = record.get_field_value(field_name)
|
|
241
225
|
column: int = 0
|
|
242
226
|
span: int = 4
|
|
243
227
|
if column_positions and field_name in column_positions:
|
|
@@ -246,8 +230,7 @@ class CallbackUtil:
|
|
|
246
230
|
span = position[1]
|
|
247
231
|
builder.add_field(modifier.modify_field(field_def), column, span)
|
|
248
232
|
|
|
249
|
-
request = FormEntryDialogRequest(title, msg, builder.get_temporary_data_type()
|
|
250
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
233
|
+
request = FormEntryDialogRequest(title, msg, builder.get_temporary_data_type())
|
|
251
234
|
response: FieldMap | None = self.callback.show_form_entry_dialog(request)
|
|
252
235
|
if response is None:
|
|
253
236
|
raise SapioUserCancelledException()
|
|
@@ -262,8 +245,7 @@ class CallbackUtil:
|
|
|
262
245
|
:param field: The definition for a field that the user must provide input to.
|
|
263
246
|
:return: The response value from the user for the given field.
|
|
264
247
|
"""
|
|
265
|
-
request = InputDialogCriteria(title, msg, field,
|
|
266
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
248
|
+
request = InputDialogCriteria(title, msg, field, self.width_pixels, self.width_percent)
|
|
267
249
|
response: Any | None = self.callback.show_input_dialog(request)
|
|
268
250
|
if response is None:
|
|
269
251
|
raise SapioUserCancelledException()
|
|
@@ -340,8 +322,6 @@ class CallbackUtil:
|
|
|
340
322
|
msg: str,
|
|
341
323
|
fields: list[AbstractVeloxFieldDefinition],
|
|
342
324
|
values: list[FieldMap],
|
|
343
|
-
group_by: str | None = None,
|
|
344
|
-
image_data: list[bytes] | None = None,
|
|
345
325
|
*,
|
|
346
326
|
data_type: str = "Default",
|
|
347
327
|
display_name: str | None = None,
|
|
@@ -355,10 +335,6 @@ class CallbackUtil:
|
|
|
355
335
|
:param fields: The definitions of the fields to display as table columns. Fields will be displayed in the order
|
|
356
336
|
they are provided in this list.
|
|
357
337
|
:param values: The values to set for each row of the table.
|
|
358
|
-
:param group_by: If provided, the created table dialog will be grouped by the field with this name by default.
|
|
359
|
-
The user may remove this grouping if they want to.
|
|
360
|
-
:param image_data: The bytes to the images that should be displayed in the rows of the table. Each element in
|
|
361
|
-
the image data list corresponds to the element at the same index in the values list.
|
|
362
338
|
:param data_type: The data type name for the temporary data type that will be created for this table.
|
|
363
339
|
:param display_name: The display name for the temporary data type. If not provided, defaults to the data type
|
|
364
340
|
name.
|
|
@@ -380,9 +356,7 @@ class CallbackUtil:
|
|
|
380
356
|
for field in fields:
|
|
381
357
|
builder.add_field(modifier.modify_field(field))
|
|
382
358
|
|
|
383
|
-
request = TableEntryDialogRequest(title, msg, builder.get_temporary_data_type(), values
|
|
384
|
-
record_image_data_list=image_data, group_by_field=group_by,
|
|
385
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
359
|
+
request = TableEntryDialogRequest(title, msg, builder.get_temporary_data_type(), values)
|
|
386
360
|
response: list[FieldMap] | None = self.callback.show_table_entry_dialog(request)
|
|
387
361
|
if response is None:
|
|
388
362
|
raise SapioUserCancelledException()
|
|
@@ -393,9 +367,7 @@ class CallbackUtil:
|
|
|
393
367
|
msg: str,
|
|
394
368
|
fields: list[str],
|
|
395
369
|
records: list[SapioRecord],
|
|
396
|
-
editable: bool | None = True
|
|
397
|
-
group_by: str | None = None,
|
|
398
|
-
image_data: list[bytes] | None = None) -> list[FieldMap]:
|
|
370
|
+
editable: bool | None = True) -> list[FieldMap]:
|
|
399
371
|
"""
|
|
400
372
|
Create a table dialog where the user may input data into the fields of the table. The table is constructed from
|
|
401
373
|
a given list of records of a singular type. Provided field names must match fields on the definition of the data
|
|
@@ -412,10 +384,6 @@ class CallbackUtil:
|
|
|
412
384
|
they are provided in this list.
|
|
413
385
|
:param editable: If true, all fields are displayed as editable. If false, all fields are displayed as
|
|
414
386
|
uneditable. If none, only those fields that are defined as editable by the data designer will be editable.
|
|
415
|
-
:param group_by: If provided, the created table dialog will be grouped by the field with this name by default.
|
|
416
|
-
The user may remove this grouping if they want to.
|
|
417
|
-
:param image_data: The bytes to the images that should be displayed in the rows of the table. Each element in
|
|
418
|
-
the image data list corresponds to the element at the same index in the records list.
|
|
419
387
|
:return: A list of dictionaries mapping the data field names of the given field definitions to the response
|
|
420
388
|
value from the user for that field for each row.
|
|
421
389
|
"""
|
|
@@ -442,9 +410,7 @@ class CallbackUtil:
|
|
|
442
410
|
raise SapioException(f"No field of name \"{field_name}\" in field definitions of type \"{data_type}\"")
|
|
443
411
|
builder.add_field(modifier.modify_field(field_def))
|
|
444
412
|
|
|
445
|
-
request = TableEntryDialogRequest(title, msg, builder.get_temporary_data_type(), field_map_list
|
|
446
|
-
record_image_data_list=image_data, group_by_field=group_by,
|
|
447
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
413
|
+
request = TableEntryDialogRequest(title, msg, builder.get_temporary_data_type(), field_map_list)
|
|
448
414
|
response: list[FieldMap] | None = self.callback.show_table_entry_dialog(request)
|
|
449
415
|
if response is None:
|
|
450
416
|
raise SapioUserCancelledException()
|
|
@@ -601,8 +567,7 @@ class CallbackUtil:
|
|
|
601
567
|
for field in final_fields:
|
|
602
568
|
builder.add_field(field)
|
|
603
569
|
|
|
604
|
-
request = TableEntryDialogRequest(title, msg, builder.get_temporary_data_type(), values
|
|
605
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
570
|
+
request = TableEntryDialogRequest(title, msg, builder.get_temporary_data_type(), values)
|
|
606
571
|
response: list[FieldMap] | None = self.callback.show_table_entry_dialog(request)
|
|
607
572
|
if response is None:
|
|
608
573
|
raise SapioUserCancelledException()
|
|
@@ -651,8 +616,7 @@ class CallbackUtil:
|
|
|
651
616
|
raise SapioException(f"The data type \"{data_type}\" does not have a layout by the name "
|
|
652
617
|
f"\"{layout_name}\" in the system.")
|
|
653
618
|
|
|
654
|
-
request = DataRecordDialogRequest(title, record, layout, minimized, access_level, plugin_path_list
|
|
655
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
619
|
+
request = DataRecordDialogRequest(title, record, layout, minimized, access_level, plugin_path_list)
|
|
656
620
|
response: bool = self.callback.data_record_form_view_dialog(request)
|
|
657
621
|
if not response:
|
|
658
622
|
raise SapioUserCancelledException()
|
|
@@ -699,7 +663,7 @@ class CallbackUtil:
|
|
|
699
663
|
return response
|
|
700
664
|
|
|
701
665
|
def record_selection_dialog(self, msg: str, fields: list[str], records: list[SapioRecord],
|
|
702
|
-
multi_select: bool = True) -> list[
|
|
666
|
+
multi_select: bool = True) -> list[FieldMap]:
|
|
703
667
|
"""
|
|
704
668
|
Create a record selection dialog for a list of records for the user to choose from. Provided field names must
|
|
705
669
|
match fields on the definition of the data type of the given records.
|
|
@@ -712,7 +676,7 @@ class CallbackUtil:
|
|
|
712
676
|
they are provided in this list.
|
|
713
677
|
:param records: The records to display as rows in the table.
|
|
714
678
|
:param multi_select: Whether the user is able to select multiple records from the list.
|
|
715
|
-
:return: A list of the
|
|
679
|
+
:return: A list of field maps corresponding to the chosen input records.
|
|
716
680
|
"""
|
|
717
681
|
data_types: set[str] = {x.data_type_name for x in records}
|
|
718
682
|
if len(data_types) > 1:
|
|
@@ -843,8 +807,7 @@ class CallbackUtil:
|
|
|
843
807
|
for field in additional_fields:
|
|
844
808
|
builder.add_field(field)
|
|
845
809
|
temp_dt = builder.get_temporary_data_type()
|
|
846
|
-
request = ESigningRequestPojo(title, msg, show_comment, temp_dt
|
|
847
|
-
width_in_pixels=self.width_pixels, width_percentage=self.width_percent)
|
|
810
|
+
request = ESigningRequestPojo(title, msg, show_comment, temp_dt)
|
|
848
811
|
response: ESigningResponsePojo | None = self.callback.show_esign_dialog(request)
|
|
849
812
|
if response is None:
|
|
850
813
|
raise SapioUserCancelledException()
|
|
@@ -943,7 +906,7 @@ class CallbackUtil:
|
|
|
943
906
|
:param file_name: The name of the file.
|
|
944
907
|
:param file_data: The data of the file, provided as either a string or as a bytes array.
|
|
945
908
|
"""
|
|
946
|
-
data = io.
|
|
909
|
+
data = io.StringIO(file_data) if isinstance(file_data, str) else io.BytesIO(file_data)
|
|
947
910
|
self.callback.send_file(file_name, False, data)
|
|
948
911
|
|
|
949
912
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import io
|
|
2
2
|
|
|
3
|
-
from sapiopylib.rest.DataMgmtService import DataMgmtServer
|
|
4
3
|
from sapiopylib.rest.User import SapioUser
|
|
5
4
|
from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
|
|
6
5
|
from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
|
|
@@ -13,35 +12,31 @@ from sapiopycommons.recordmodel.record_handler import RecordHandler
|
|
|
13
12
|
# FR-46064 - Initial port of PyWebhookUtils to sapiopycommons.
|
|
14
13
|
class AttachmentUtil:
|
|
15
14
|
@staticmethod
|
|
16
|
-
def get_attachment_bytes(context: SapioWebhookContext
|
|
15
|
+
def get_attachment_bytes(context: SapioWebhookContext, attachment: SapioRecord) -> bytes:
|
|
17
16
|
"""
|
|
18
17
|
Get the data bytes for the given attachment record. Makes a webservice call to retrieve the data.
|
|
19
18
|
|
|
20
|
-
:param context: The current webhook context
|
|
19
|
+
:param context: The current webhook context.
|
|
21
20
|
:param attachment: The attachment record.
|
|
22
21
|
:return: The bytes for the attachment's file data.
|
|
23
22
|
"""
|
|
24
23
|
attachment = AliasUtil.to_data_record(attachment)
|
|
25
|
-
if isinstance(context, SapioWebhookContext):
|
|
26
|
-
dr_man = context.data_record_manager
|
|
27
|
-
else:
|
|
28
|
-
dr_man = DataMgmtServer.get_data_record_manager(context)
|
|
29
24
|
with io.BytesIO() as data_sink:
|
|
30
25
|
def consume_data(chunk: bytes):
|
|
31
26
|
data_sink.write(chunk)
|
|
32
|
-
|
|
27
|
+
context.data_record_manager.get_attachment_data(attachment, consume_data)
|
|
33
28
|
data_sink.flush()
|
|
34
29
|
data_sink.seek(0)
|
|
35
30
|
file_bytes = data_sink.read()
|
|
36
31
|
return file_bytes
|
|
37
32
|
|
|
38
33
|
@staticmethod
|
|
39
|
-
def set_attachment_bytes(context: SapioWebhookContext
|
|
34
|
+
def set_attachment_bytes(context: SapioWebhookContext, attachment: SapioRecord,
|
|
40
35
|
file_name: str, file_bytes: bytes) -> None:
|
|
41
36
|
"""
|
|
42
37
|
Set the attachment data for a given attachment record. Makes a webservice call to set the data.
|
|
43
38
|
|
|
44
|
-
:param context: The current webhook context
|
|
39
|
+
:param context: The current webhook context.
|
|
45
40
|
:param attachment: The attachment record. Must be an existing data record that is an attachment type.
|
|
46
41
|
:param file_name: The name of the attachment.
|
|
47
42
|
:param file_bytes: The bytes of the attachment data.
|
|
@@ -50,12 +45,8 @@ class AttachmentUtil:
|
|
|
50
45
|
raise SapioException("Provided record cannot have its attachment data set, as it does not exist in the "
|
|
51
46
|
"system yet.")
|
|
52
47
|
attachment = AliasUtil.to_data_record(attachment)
|
|
53
|
-
if isinstance(context, SapioWebhookContext):
|
|
54
|
-
dr_man = context.data_record_manager
|
|
55
|
-
else:
|
|
56
|
-
dr_man = DataMgmtServer.get_data_record_manager(context)
|
|
57
48
|
with io.BytesIO(file_bytes) as stream:
|
|
58
|
-
|
|
49
|
+
context.data_record_manager.set_attachment_data(attachment, file_name, stream)
|
|
59
50
|
|
|
60
51
|
@staticmethod
|
|
61
52
|
def create_attachment(context: SapioWebhookContext | SapioUser, file_name: str, file_bytes: bytes,
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import time
|
|
2
2
|
from collections.abc import Mapping, Iterable
|
|
3
3
|
|
|
4
|
-
from sapiopylib.rest.DataMgmtService import DataMgmtServer
|
|
5
4
|
from sapiopylib.rest.ELNService import ElnManager
|
|
6
|
-
from sapiopylib.rest.User import SapioUser
|
|
7
5
|
from sapiopylib.rest.pojo.DataRecord import DataRecord
|
|
8
6
|
from sapiopylib.rest.pojo.eln.ElnExperiment import ElnExperiment, TemplateExperimentQueryPojo, ElnTemplate, \
|
|
9
7
|
InitializeNotebookExperimentPojo, ElnExperimentUpdateCriteria
|
|
@@ -19,7 +17,7 @@ from sapiopylib.rest.utils.recordmodel.PyRecordModel import PyRecordModel
|
|
|
19
17
|
from sapiopylib.rest.utils.recordmodel.RecordModelManager import RecordModelInstanceManager, RecordModelManager
|
|
20
18
|
from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
|
|
21
19
|
|
|
22
|
-
from sapiopycommons.general.aliases import AliasUtil, SapioRecord
|
|
20
|
+
from sapiopycommons.general.aliases import AliasUtil, SapioRecord
|
|
23
21
|
from sapiopycommons.general.exceptions import SapioException
|
|
24
22
|
|
|
25
23
|
Step = str | ElnEntryStep
|
|
@@ -29,8 +27,7 @@ itself."""
|
|
|
29
27
|
|
|
30
28
|
# FR-46064 - Initial port of PyWebhookUtils to sapiopycommons.
|
|
31
29
|
class ExperimentHandler:
|
|
32
|
-
|
|
33
|
-
context: SapioWebhookContext | None
|
|
30
|
+
context: SapioWebhookContext
|
|
34
31
|
"""The context that this handler is working from."""
|
|
35
32
|
|
|
36
33
|
# Basic experiment info from the context.
|
|
@@ -80,57 +77,31 @@ class ExperimentHandler:
|
|
|
80
77
|
ElnExperimentStatus.Canceled]
|
|
81
78
|
"""The set of statuses that an ELN experiment could have and be considered locked."""
|
|
82
79
|
|
|
83
|
-
def __init__(self, context: SapioWebhookContext
|
|
80
|
+
def __init__(self, context: SapioWebhookContext, experiment: ElnExperiment | None = None):
|
|
84
81
|
"""
|
|
85
82
|
Initialization will throw an exception if there is no ELN Experiment in the provided context and no experiment
|
|
86
83
|
is provided.
|
|
87
84
|
|
|
88
|
-
:param context: The current webhook context
|
|
85
|
+
:param context: The current webhook context.
|
|
89
86
|
:param experiment: If an experiment is provided that is separate from the experiment that is in the context,
|
|
90
|
-
that experiment will be used by this ExperimentHandler instead.
|
|
91
|
-
|
|
92
|
-
"""
|
|
93
|
-
if isinstance(context, SapioWebhookContext):
|
|
94
|
-
self.user = context.user
|
|
95
|
-
self.context = context
|
|
96
|
-
else:
|
|
97
|
-
self.user = context
|
|
98
|
-
self.context = None
|
|
87
|
+
that experiment will be used by this ExperimentHandler instead.
|
|
88
|
+
"""
|
|
99
89
|
# FR-46495 - Allow the init function of ExperimentHandler to take in an ElnExperiment that is separate from the
|
|
100
90
|
# context.
|
|
101
|
-
|
|
102
|
-
if experiment is not None:
|
|
103
|
-
# If this object is already an ElnExperiment, do nothing.
|
|
104
|
-
if isinstance(experiment, ElnExperiment):
|
|
105
|
-
pass
|
|
106
|
-
# If this object is an ElnExperimentProtocol, then we can get the ElnExperiment from the object.
|
|
107
|
-
elif isinstance(experiment, ElnExperimentProtocol):
|
|
108
|
-
experiment: ElnExperiment = experiment.eln_experiment
|
|
109
|
-
# If this object is an integer, assume it is a notebook ID that we can query the system with.
|
|
110
|
-
elif isinstance(experiment, int):
|
|
111
|
-
notebook_id: int = experiment
|
|
112
|
-
experiment: ElnExperiment = context.eln_manager.get_eln_experiment_by_id(notebook_id)
|
|
113
|
-
if not experiment:
|
|
114
|
-
raise SapioException(f"No experiment with notebook ID {notebook_id} located in the system.")
|
|
115
|
-
# If this object is a record, assume it is an experiment record that we can query the system with.
|
|
116
|
-
else:
|
|
117
|
-
record_id: int = AliasUtil.to_record_ids([experiment])[0]
|
|
118
|
-
experiment: ElnExperiment = context.eln_manager.get_eln_experiment_by_record_id(record_id)
|
|
119
|
-
if not experiment:
|
|
120
|
-
raise SapioException(f"No experiment with record ID {record_id} located in the system.")
|
|
121
|
-
if (context is None or context.eln_experiment is None) and experiment is None:
|
|
91
|
+
if context.eln_experiment is None and experiment is None:
|
|
122
92
|
raise SapioException("Cannot initialize ExperimentHandler. No ELN Experiment in the context.")
|
|
123
|
-
if context
|
|
124
|
-
experiment
|
|
93
|
+
if context.eln_experiment == experiment:
|
|
94
|
+
experiment = None
|
|
95
|
+
self.context = context
|
|
125
96
|
|
|
126
97
|
# Get the basic information about this experiment that already exists in the context and is often used.
|
|
127
98
|
self.__eln_exp = experiment if experiment else context.eln_experiment
|
|
128
|
-
self.__protocol = ElnExperimentProtocol(experiment,
|
|
99
|
+
self.__protocol = ElnExperimentProtocol(experiment, context.user) if experiment else context.active_protocol
|
|
129
100
|
self.__exp_id = self.__protocol.get_id()
|
|
130
101
|
|
|
131
102
|
# Grab various managers that may be used.
|
|
132
|
-
self.__eln_man =
|
|
133
|
-
self.__inst_man = RecordModelManager(
|
|
103
|
+
self.__eln_man = context.eln_manager
|
|
104
|
+
self.__inst_man = RecordModelManager(context.user).instance_manager
|
|
134
105
|
|
|
135
106
|
# Create empty caches to fill when necessary.
|
|
136
107
|
self.__steps = {}
|
|
@@ -292,7 +263,7 @@ class ExperimentHandler:
|
|
|
292
263
|
:return: The data record for this experiment. None if it has no record.
|
|
293
264
|
"""
|
|
294
265
|
if not hasattr(self, "_ExperimentHandler__exp_record"):
|
|
295
|
-
drm =
|
|
266
|
+
drm = self.context.data_record_manager
|
|
296
267
|
dt = self.__eln_exp.experiment_data_type_name
|
|
297
268
|
results = drm.query_data_records_by_id(dt, [self.__eln_exp.experiment_record_id]).result_list
|
|
298
269
|
# PR-46504: Set the exp_record to None if there are no results.
|
|
@@ -609,6 +580,8 @@ class ExperimentHandler:
|
|
|
609
580
|
step.set_records(AliasUtil.to_data_records(records))
|
|
610
581
|
|
|
611
582
|
# FR-46496 - Provide alias of set_step_records for use with form entries.
|
|
583
|
+
# TODO: Provide a similar aliased function for attachment entries once sapiopylib allows setting multiple
|
|
584
|
+
# attachments to an attachment step.
|
|
612
585
|
def set_form_record(self, step: Step, record: SapioRecord) -> None:
|
|
613
586
|
"""
|
|
614
587
|
Sets the record for a form entry.
|
|
@@ -644,7 +617,7 @@ class ExperimentHandler:
|
|
|
644
617
|
if step.eln_entry.entry_type != ElnEntryType.Table:
|
|
645
618
|
raise SapioException("The provided step is not a table entry.")
|
|
646
619
|
dt: str = step.get_data_type_names()[0]
|
|
647
|
-
if not
|
|
620
|
+
if not self.__is_eln_type(dt):
|
|
648
621
|
raise SapioException("The provided step is not an ELN data type entry.")
|
|
649
622
|
return self.__inst_man.add_new_records(dt, count)
|
|
650
623
|
|
|
@@ -668,7 +641,7 @@ class ExperimentHandler:
|
|
|
668
641
|
"""
|
|
669
642
|
step = self.__to_eln_step(step)
|
|
670
643
|
dt: str = step.get_data_type_names()[0]
|
|
671
|
-
if not
|
|
644
|
+
if not self.__is_eln_type(dt):
|
|
672
645
|
raise SapioException("The provided step is not an ELN data type entry.")
|
|
673
646
|
if any([x.data_type_name != dt for x in records]):
|
|
674
647
|
raise SapioException("Not all of the provided records match the data type of the step.")
|
|
@@ -685,6 +658,17 @@ class ExperimentHandler:
|
|
|
685
658
|
for record in record_models:
|
|
686
659
|
record.delete()
|
|
687
660
|
|
|
661
|
+
# TODO: Remove and use the function of the same name in ElnBaseDataType in the future. Currently this function is
|
|
662
|
+
# bugged in sapiopylib and is comparing against base_type.name instead of base_type.value.
|
|
663
|
+
@staticmethod
|
|
664
|
+
def __is_eln_type(data_type: str):
|
|
665
|
+
if data_type is None or not data_type:
|
|
666
|
+
return False
|
|
667
|
+
for base_type in ElnBaseDataType:
|
|
668
|
+
if data_type.lower().startswith(base_type.value.lower()):
|
|
669
|
+
return True
|
|
670
|
+
return False
|
|
671
|
+
|
|
688
672
|
def update_step(self, step: Step,
|
|
689
673
|
entry_name: str | None = None,
|
|
690
674
|
related_entry_set: Iterable[int] | None = None,
|
|
@@ -972,27 +956,6 @@ class ExperimentHandler:
|
|
|
972
956
|
step.unlock_step()
|
|
973
957
|
step.eln_entry.entry_status = ExperimentEntryStatus.UnlockedChangesRequired
|
|
974
958
|
|
|
975
|
-
def disable_step(self, step: Step) -> None:
|
|
976
|
-
"""
|
|
977
|
-
Set the status of the input step to Disabled. This is the state that entries are in when they are waiting for
|
|
978
|
-
entries that they are dependent upon to be submitted before they can be enabled. If you have unsubmitted an
|
|
979
|
-
entry and want its dependent entries to be locked again, then you would use this to set their status to
|
|
980
|
-
disabled.
|
|
981
|
-
|
|
982
|
-
Makes a webservice call to update the step. Checks if the step is already unlocked, and does nothing if so.
|
|
983
|
-
|
|
984
|
-
If no step functions have been called before and a step is being searched for by name, queries for the
|
|
985
|
-
list of steps in the experiment and caches them.
|
|
986
|
-
|
|
987
|
-
:param step:
|
|
988
|
-
The step to disable.
|
|
989
|
-
The step may be provided as either a string for the name of the step or an ElnEntryStep.
|
|
990
|
-
If given a name, throws an exception if no step of the given name exists in the experiment.
|
|
991
|
-
"""
|
|
992
|
-
step = self.__to_eln_step(step)
|
|
993
|
-
if step.eln_entry.entry_status in self.__ENTRY_LOCKED_STATUSES:
|
|
994
|
-
self.update_step(step, entry_status=ExperimentEntryStatus.Disabled)
|
|
995
|
-
|
|
996
959
|
def step_is_submitted(self, step: Step) -> bool:
|
|
997
960
|
"""
|
|
998
961
|
Determine if the input step has already been submitted.
|
|
@@ -23,7 +23,7 @@ class CDL:
|
|
|
23
23
|
"fileName": file_name
|
|
24
24
|
}
|
|
25
25
|
user: SapioUser = context if isinstance(context, SapioUser) else context.user
|
|
26
|
-
with io.
|
|
26
|
+
with io.StringIO(file_data) if isinstance(file_data, str) else io.BytesIO(file_data) as data_stream:
|
|
27
27
|
response = user.post_data_stream(sub_path, params=params, data_stream=data_stream)
|
|
28
28
|
user.raise_for_status(response)
|
|
29
29
|
# The response content is returned as bytes for a comma separated string of record IDs.
|
|
@@ -54,7 +54,7 @@ class FileBridge:
|
|
|
54
54
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
55
55
|
}
|
|
56
56
|
user: SapioUser = context if isinstance(context, SapioUser) else context.user
|
|
57
|
-
with io.
|
|
57
|
+
with io.StringIO(file_data) if isinstance(file_data, str) else io.BytesIO(file_data) as data_stream:
|
|
58
58
|
response = user.post_data_stream(sub_path, params=params, data_stream=data_stream)
|
|
59
59
|
user.raise_for_status(response)
|
|
60
60
|
|
|
@@ -21,8 +21,7 @@ class FileUtil:
|
|
|
21
21
|
"""
|
|
22
22
|
@staticmethod
|
|
23
23
|
def tokenize_csv(file_bytes: bytes, required_headers: list[str] | None = None, header_row_index: int | None = 0,
|
|
24
|
-
seperator: str = ",", *, encoding: str | None = None
|
|
25
|
-
-> tuple[list[dict[str, str]], list[list[str]]]:
|
|
24
|
+
seperator: str = ",", *, encoding: str | None = None) -> tuple[list[dict[str, str]], list[list[str]]]:
|
|
26
25
|
"""
|
|
27
26
|
Tokenize a CSV file. The provided file must be uniform. That is, if row 1 has 10 cells, all the rows in the file
|
|
28
27
|
must have 10 cells. Otherwise, the Pandas parser throws a tokenizer exception.
|
|
@@ -38,8 +37,6 @@ class FileUtil:
|
|
|
38
37
|
:param encoding: The encoding used to read the given file bytes. If not provided, uses utf-8. If your file
|
|
39
38
|
contains a non-utf-8 character, then a UnicodeDecodeError will be thrown. If this happens, consider using
|
|
40
39
|
ISO-8859-1 as the encoding.
|
|
41
|
-
:param exception_on_empty: Throw a user error exception if the provided file bytes result in an empty list in
|
|
42
|
-
the first element of the returned tuple.
|
|
43
40
|
:return: The CSV parsed into a list of dicts where each dict is a row, mapping the headers to the cells for
|
|
44
41
|
that row. Also returns a list of each row above the headers (the metadata), parsed into a list of each cell.
|
|
45
42
|
If the header row index is 0 or None, this list will be empty.
|
|
@@ -52,13 +49,11 @@ class FileUtil:
|
|
|
52
49
|
metadata: list[list[str]] = FileUtil.data_frame_to_lists(file_metadata)
|
|
53
50
|
# Parse the data from the file body into a list of dicts.
|
|
54
51
|
rows: list[dict[str, str]] = FileUtil.data_frame_to_dicts(file_body, required_headers, header_row_index)
|
|
55
|
-
if exception_on_empty and not rows:
|
|
56
|
-
raise SapioUserErrorException("The provided file contains no rows of information below the headers.")
|
|
57
52
|
return rows, metadata
|
|
58
53
|
|
|
59
54
|
@staticmethod
|
|
60
|
-
def tokenize_xlsx(file_bytes: bytes, required_headers: list[str] | None = None, header_row_index: int | None = 0
|
|
61
|
-
|
|
55
|
+
def tokenize_xlsx(file_bytes: bytes, required_headers: list[str] | None = None, header_row_index: int | None = 0) \
|
|
56
|
+
-> tuple[list[dict[str, str]], list[list[str]]]:
|
|
62
57
|
"""
|
|
63
58
|
Tokenize an XLSX file row by row.
|
|
64
59
|
|
|
@@ -69,8 +64,6 @@ class FileUtil:
|
|
|
69
64
|
row is returned in the metadata list. If input is None, then no row is considered to be the header row,
|
|
70
65
|
meaning that required headers are also ignored if any are provided. By default, the first row (0th index)
|
|
71
66
|
is assumed to be the header row.
|
|
72
|
-
:param exception_on_empty: Throw a user error exception if the provided file bytes result in an empty list in
|
|
73
|
-
the first element of the returned tuple.
|
|
74
67
|
:return: The XLSX parsed into a list of dicts where each dict is a row, mapping the headers to the cells for
|
|
75
68
|
that row. Also returns a list of each row above the headers (the metadata), parsed into a list of each cell.
|
|
76
69
|
If the header row index is 0 or None, this list will be empty.
|
|
@@ -82,8 +75,6 @@ class FileUtil:
|
|
|
82
75
|
metadata: list[list[str]] = FileUtil.data_frame_to_lists(file_metadata)
|
|
83
76
|
# Parse the data from the file body into a list of dicts.
|
|
84
77
|
rows: list[dict[str, str]] = FileUtil.data_frame_to_dicts(file_body, required_headers, header_row_index)
|
|
85
|
-
if exception_on_empty and not rows:
|
|
86
|
-
raise SapioUserErrorException("The provided file contains no rows of information below the headers.")
|
|
87
78
|
return rows, metadata
|
|
88
79
|
|
|
89
80
|
@staticmethod
|
|
@@ -238,7 +229,7 @@ class FileUtil:
|
|
|
238
229
|
:param file_data: The CSV file to be converted.
|
|
239
230
|
:return: The bytes of the CSV file converted to an XLSX file.
|
|
240
231
|
"""
|
|
241
|
-
with (io.BytesIO(file_data
|
|
232
|
+
with (io.BytesIO(file_data) if isinstance(file_data, bytes) else io.StringIO(file_data)) as csv:
|
|
242
233
|
# Setting header to false makes pandas read the CSV as-is.
|
|
243
234
|
data_frame = pandas.read_csv(csv, sep=",", header=None)
|
|
244
235
|
|
|
@@ -8,6 +8,7 @@ from sapiopylib.rest.pojo.CustomReport import RawReportTerm, RawTermOperation
|
|
|
8
8
|
from sapiopylib.rest.pojo.datatype.FieldDefinition import VeloxIntegerFieldDefinition, VeloxStringFieldDefinition, \
|
|
9
9
|
AbstractVeloxFieldDefinition
|
|
10
10
|
from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
|
|
11
|
+
from sapiopylib.rest.pojo.webhook.WebhookResult import SapioWebhookResult
|
|
11
12
|
|
|
12
13
|
from sapiopycommons.callbacks.callback_util import CallbackUtil
|
|
13
14
|
from sapiopycommons.files.file_data_handler import FileDataHandler, FilterList
|
|
@@ -79,7 +80,7 @@ class FileValidator:
|
|
|
79
80
|
|
|
80
81
|
return failed_rows
|
|
81
82
|
|
|
82
|
-
def build_violation_report(self, context:
|
|
83
|
+
def build_violation_report(self, context: SapioWebhookResult | SapioUser,
|
|
83
84
|
rule_violations: dict[int, list[ValidationRule]]) -> None:
|
|
84
85
|
"""
|
|
85
86
|
Build a simple report of any rule violations in the file to display to the user as a table dialog.
|
|
@@ -50,27 +50,7 @@ class AliasUtil:
|
|
|
50
50
|
|
|
51
51
|
:return: A list of record IDs for the input records.
|
|
52
52
|
"""
|
|
53
|
-
return [(
|
|
54
|
-
|
|
55
|
-
@staticmethod
|
|
56
|
-
def to_record_id(record: RecordIdentifier):
|
|
57
|
-
"""
|
|
58
|
-
Convert a single variable that could be either an integer, DataRecord, PyRecordModel,
|
|
59
|
-
or WrappedRecordModel to just an integer (taking the record ID from the record).
|
|
60
|
-
|
|
61
|
-
:return: A record ID for the input record.
|
|
62
|
-
"""
|
|
63
|
-
return record if isinstance(record, int) else record.record_id
|
|
64
|
-
|
|
65
|
-
@staticmethod
|
|
66
|
-
def to_data_type_name(record: SapioRecord) -> str:
|
|
67
|
-
"""
|
|
68
|
-
Convert a single variable that could be either a Data Record, PyRecordModel, or WrappedRecordModel to the
|
|
69
|
-
data type name for that record.
|
|
70
|
-
|
|
71
|
-
:return: The data type name for the input record.
|
|
72
|
-
"""
|
|
73
|
-
return record.data_type_name
|
|
53
|
+
return [(x if isinstance(x, int) else x.record_id) for x in records]
|
|
74
54
|
|
|
75
55
|
@staticmethod
|
|
76
56
|
def to_field_map_lists(records: Iterable[SapioRecord]) -> list[FieldMap]:
|