sapiopycommons 2025.4.9a150__py3-none-any.whl → 2025.4.9a476__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.

Files changed (42) hide show
  1. sapiopycommons/callbacks/callback_util.py +1262 -392
  2. sapiopycommons/callbacks/field_builder.py +2 -0
  3. sapiopycommons/chem/Molecules.py +0 -2
  4. sapiopycommons/customreport/auto_pagers.py +281 -0
  5. sapiopycommons/customreport/term_builder.py +1 -1
  6. sapiopycommons/datatype/attachment_util.py +4 -2
  7. sapiopycommons/datatype/data_fields.py +23 -1
  8. sapiopycommons/eln/experiment_cache.py +173 -0
  9. sapiopycommons/eln/experiment_handler.py +933 -279
  10. sapiopycommons/eln/experiment_report_util.py +15 -10
  11. sapiopycommons/eln/experiment_step_factory.py +474 -0
  12. sapiopycommons/eln/experiment_tags.py +7 -0
  13. sapiopycommons/eln/plate_designer.py +159 -59
  14. sapiopycommons/eln/step_creation.py +235 -0
  15. sapiopycommons/files/file_bridge.py +76 -0
  16. sapiopycommons/files/file_bridge_handler.py +325 -110
  17. sapiopycommons/files/file_data_handler.py +2 -2
  18. sapiopycommons/files/file_util.py +40 -15
  19. sapiopycommons/files/file_validator.py +6 -5
  20. sapiopycommons/files/file_writer.py +1 -1
  21. sapiopycommons/flowcyto/flow_cyto.py +1 -1
  22. sapiopycommons/general/accession_service.py +3 -3
  23. sapiopycommons/general/aliases.py +51 -28
  24. sapiopycommons/general/audit_log.py +2 -2
  25. sapiopycommons/general/custom_report_util.py +24 -1
  26. sapiopycommons/general/data_structure_util.py +115 -0
  27. sapiopycommons/general/directive_util.py +86 -0
  28. sapiopycommons/general/exceptions.py +41 -2
  29. sapiopycommons/general/popup_util.py +2 -2
  30. sapiopycommons/multimodal/multimodal.py +1 -0
  31. sapiopycommons/processtracking/custom_workflow_handler.py +46 -30
  32. sapiopycommons/recordmodel/record_handler.py +547 -159
  33. sapiopycommons/rules/eln_rule_handler.py +41 -30
  34. sapiopycommons/rules/on_save_rule_handler.py +41 -30
  35. sapiopycommons/samples/aliquot.py +48 -0
  36. sapiopycommons/webhook/webhook_handlers.py +448 -55
  37. sapiopycommons/webhook/webservice_handlers.py +2 -2
  38. {sapiopycommons-2025.4.9a150.dist-info → sapiopycommons-2025.4.9a476.dist-info}/METADATA +1 -1
  39. sapiopycommons-2025.4.9a476.dist-info/RECORD +67 -0
  40. sapiopycommons-2025.4.9a150.dist-info/RECORD +0 -59
  41. {sapiopycommons-2025.4.9a150.dist-info → sapiopycommons-2025.4.9a476.dist-info}/WHEEL +0 -0
  42. {sapiopycommons-2025.4.9a150.dist-info → sapiopycommons-2025.4.9a476.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,86 @@
1
+ from typing import Iterable, cast
2
+
3
+ from sapiopylib.rest.User import SapioUser
4
+ from sapiopylib.rest.pojo.CustomReport import CustomReportCriteria, CustomReport
5
+ from sapiopylib.rest.pojo.webhook.WebhookDirective import HomePageDirective, FormDirective, TableDirective, \
6
+ CustomReportDirective, ElnExperimentDirective, ExperimentEntryDirective
7
+
8
+ from sapiopycommons.general.aliases import SapioRecord, AliasUtil, ExperimentIdentifier, ExperimentEntryIdentifier, \
9
+ UserIdentifier
10
+ from sapiopycommons.general.custom_report_util import CustomReportUtil
11
+
12
+
13
+ # FR-47392: Create a DirectiveUtil class to simplify the creation of directives.
14
+ class DirectiveUtil:
15
+ """
16
+ DirectiveUtil is a class for creating webhook directives. The utility functions reduce the provided variables
17
+ down to the exact type that the directives require, removing the need for the caller to handle the conversion.
18
+ """
19
+ user: SapioUser
20
+
21
+ def __init__(self, context: UserIdentifier):
22
+ """
23
+ :param context: The current webhook context or a user object to send requests from.
24
+ """
25
+ self.user = AliasUtil.to_sapio_user(context)
26
+
27
+ @staticmethod
28
+ def homepage() -> HomePageDirective:
29
+ """
30
+ :return: A directive that sends the user back to their home page.
31
+ """
32
+ return HomePageDirective()
33
+
34
+ @staticmethod
35
+ def record_form(record: SapioRecord) -> FormDirective:
36
+ """
37
+ :param record: A record in the system.
38
+ :return: A directive that sends the user to a specific data record form.
39
+ """
40
+ return FormDirective(AliasUtil.to_data_record(record))
41
+
42
+ @staticmethod
43
+ def record_table(records: Iterable[SapioRecord]) -> TableDirective:
44
+ """
45
+ :param records: A list of records in the system.
46
+ :return: A directive that sends the user to a table of data records.
47
+ """
48
+ return TableDirective(AliasUtil.to_data_records(records))
49
+
50
+ @staticmethod
51
+ def record_adaptive(records: Iterable[SapioRecord]) -> TableDirective | FormDirective:
52
+ """
53
+ :param records: A list of records in the system.
54
+ :return: A directive that sends the user to a table of data records if there are multiple records,
55
+ or a directive that sends the user to a specific data record form if there is only one record.
56
+ """
57
+ records: list[SapioRecord] = list(records)
58
+ if len(records) == 1:
59
+ return DirectiveUtil.record_form(records[0])
60
+ return DirectiveUtil.record_table(records)
61
+
62
+ def custom_report(self, report: CustomReport | CustomReportCriteria | str) -> CustomReportDirective:
63
+ """
64
+ :param report: A custom report, the criteria for a custom report, or the name of a system report.
65
+ :return: A directive that sends the user to the results of the provided custom report.
66
+ """
67
+ if isinstance(report, str):
68
+ report: CustomReport = CustomReportUtil.get_system_report_criteria(self.user, report)
69
+ return CustomReportDirective(cast(CustomReport, report))
70
+
71
+ @staticmethod
72
+ def eln_experiment(experiment: ExperimentIdentifier) -> ElnExperimentDirective:
73
+ """
74
+ :param experiment: An identifier for an experiment.
75
+ :return: A directive that sends the user to the ELN experiment.
76
+ """
77
+ return ElnExperimentDirective(AliasUtil.to_notebook_id(experiment))
78
+
79
+ @staticmethod
80
+ def eln_entry(experiment: ExperimentIdentifier, entry: ExperimentEntryIdentifier) -> ExperimentEntryDirective:
81
+ """
82
+ :param experiment: An identifier for an experiment.
83
+ :param entry: An identifier for an entry in the experiment.
84
+ :return: A directive that sends the user to the provided experiment entry within its ELN experiment.
85
+ """
86
+ return ExperimentEntryDirective(AliasUtil.to_notebook_id(experiment), AliasUtil.to_entry_id(entry))
@@ -1,3 +1,20 @@
1
+ from enum import Enum
2
+
3
+
4
+ class MessageDisplayType(Enum):
5
+ """
6
+ An enum representing the different ways in which a message can be displayed to the user.
7
+ """
8
+ TOASTER_SUCCESS = 0
9
+ TOASTER_INFO = 1
10
+ TOASTER_WARNING = 2
11
+ TOASTER_ERROR = 3
12
+ OK_DIALOG = 4
13
+ DISPLAY_INFO = 5
14
+ DISPLAY_WARNING = 6
15
+ DISPLAY_ERROR = 7
16
+
17
+
1
18
  # FR-46064 - Initial port of PyWebhookUtils to sapiopycommons.
2
19
  class SapioException(Exception):
3
20
  """
@@ -29,7 +46,29 @@ class SapioDialogTimeoutException(SapioException):
29
46
  pass
30
47
 
31
48
 
32
- class SapioUserErrorException(SapioException):
49
+ class DisplayableException(SapioException):
50
+ """
51
+ A generic exception that promises to return a user-friendly message explaining the error that should be displayed to
52
+ the user. Note that it is up to whichever class that catches this exception to actually display the message.
53
+ """
54
+ msg: str
55
+ display_type: MessageDisplayType | None
56
+ title: str | None
57
+
58
+ def __init__(self, msg: str, display_type: MessageDisplayType | None = None, title: str | None = None):
59
+ """
60
+ :param msg: The message that should be displayed to the user.
61
+ :param display_type: The manner in which the message should be displayed. If None, then the display type should
62
+ be controlled by the class that catches this exception.
63
+ :param title: If the display type is able to have a title, this is the title that will be displayed. If None,
64
+ then the title should be controlled by the class that catches this exception.
65
+ """
66
+ self.msg = msg
67
+ self.display_type = display_type
68
+ self.title = title
69
+
70
+
71
+ class SapioUserErrorException(DisplayableException):
33
72
  """
34
73
  An exception caused by user error (e.g. user provided a CSV when an XLSX was expected), which promises to return a
35
74
  user-friendly message explaining the error that should be displayed to the user.
@@ -39,7 +78,7 @@ class SapioUserErrorException(SapioException):
39
78
  pass
40
79
 
41
80
 
42
- class SapioCriticalErrorException(SapioException):
81
+ class SapioCriticalErrorException(DisplayableException):
43
82
  """
44
83
  A critical exception caused by user error, which promises to return a user-friendly message explaining the error
45
84
  that should be displayed to the user.
@@ -311,7 +311,7 @@ class PopupUtil:
311
311
  raise SapioException("Multiple data type names encountered in records list for record table popup.")
312
312
  data_type: str = data_types.pop()
313
313
  # Get the field maps from the records.
314
- field_map_list: list[FieldMap] = AliasUtil.to_field_map_lists(records)
314
+ field_map_list: list[FieldMap] = AliasUtil.to_field_map_list(records)
315
315
  # Get the field definitions of the data type.
316
316
  type_man = DataMgmtServer.get_data_type_manager(context.user)
317
317
  type_def: DataTypeDefinition = type_man.get_data_type_definition(data_type)
@@ -366,7 +366,7 @@ class PopupUtil:
366
366
  raise SapioException("Multiple data type names encountered in records list for record table popup.")
367
367
  data_type: str = data_types.pop()
368
368
  # Get the field maps from the records.
369
- field_map_list: list[FieldMap] = AliasUtil.to_field_map_lists(records)
369
+ field_map_list: list[FieldMap] = AliasUtil.to_field_map_list(records)
370
370
  # Get the field definitions of the data type.
371
371
  type_man = DataMgmtServer.get_data_type_manager(context.user)
372
372
  type_def: DataTypeDefinition = type_man.get_data_type_definition(data_type)
@@ -6,6 +6,7 @@ from weakref import WeakValueDictionary
6
6
 
7
7
  from databind.json import dumps, loads
8
8
  from sapiopylib.rest.User import SapioUser
9
+ from sapiopylib.rest.pojo.DataRecord import DataRecord
9
10
 
10
11
  from sapiopycommons.general.exceptions import SapioException
11
12
  from sapiopycommons.multimodal.multimodal_data import *
@@ -3,12 +3,13 @@ from typing import Iterable
3
3
  from sapiopylib.rest.User import SapioUser
4
4
  from sapiopylib.rest.pojo.CustomReport import CustomReportCriteria
5
5
  from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
6
+ from sapiopylib.rest.utils.recordmodel.PyRecordModel import PyRecordModel
6
7
  from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
7
8
 
9
+ from sapiopycommons.customreport.auto_pagers import CustomReportDictAutoPager, CustomReportRecordAutoPager
8
10
  from sapiopycommons.customreport.custom_report_builder import CustomReportBuilder
9
11
  from sapiopycommons.datatype.data_fields import ProcessQueueItemFields, SystemFields, ProcessWorkflowTrackingFields
10
12
  from sapiopycommons.general.aliases import UserIdentifier, AliasUtil, SapioRecord
11
- from sapiopycommons.general.custom_report_util import CustomReportUtil
12
13
  from sapiopycommons.general.exceptions import SapioException
13
14
  from sapiopycommons.general.time_util import TimeUtil
14
15
  from sapiopycommons.recordmodel.record_handler import RecordHandler
@@ -104,24 +105,28 @@ class QueueItemHandler:
104
105
  """
105
106
  self.user = AliasUtil.to_sapio_user(context)
106
107
  self.rec_handler = RecordHandler(self.user)
107
- if isinstance(context, SapioWebhookContext):
108
+ # PR-47565: Only initialize a ProcessQueueContext if the given context object has context_data.
109
+ if isinstance(context, SapioWebhookContext) and context.context_data:
108
110
  self.context = ProcessQueueContext(context)
109
111
  else:
110
112
  self.context = None
111
113
 
112
- def get_process_queue_items_from_context(self, wrapper: type[WrappedType],
114
+ # CR-47491: Support providing a data type name string to receive PyRecordModels instead of requiring a WrapperType.
115
+ def get_process_queue_items_from_context(self, wrapper: type[WrappedType] | str,
113
116
  context: SapioWebhookContext | ProcessQueueContext | None = None) \
114
- -> list[WrappedType]:
117
+ -> list[WrappedType] | list[PyRecordModel]:
115
118
  """
116
119
  When you launch records from a custom process queue, the process queue items related to the selected records
117
120
  are provided as record IDs to the process queue context. Using these record IDs, query for the queue item
118
121
  records and wrap them as record models.
119
122
 
120
- :param wrapper: The record model wrapper for the process queue items.
123
+ :param wrapper: The record model wrapper or data type name for the process queue items.
121
124
  :param context: If this handler was not initialized with a context object, or you wish to retrieve
122
125
  data from a different context object than the initializing context, then provide the context to retrieve the
123
126
  record IDs from.
124
127
  :return: The process queue items corresponding to the record IDs from the context wrapped as record models.
128
+ If a data type name was used instead of a model wrapper, then the returned records will be PyRecordModels
129
+ instead of WrappedRecordModels.
125
130
  """
126
131
  if context is None and self.context is not None:
127
132
  record_ids: list[int] = self.context.process_queue_item_record_ids
@@ -175,46 +180,52 @@ class QueueItemHandler:
175
180
  ret_val[queue_item] = record
176
181
  return ret_val
177
182
 
178
- def get_queue_items_from_report(self, wrapper: type[WrappedType], criteria: QueueItemReportCriteria) \
179
- -> list[WrappedType]:
183
+ def get_queue_items_from_report(self, wrapper: type[WrappedType] | None, criteria: QueueItemReportCriteria) \
184
+ -> list[WrappedType] | list[PyRecordModel]:
180
185
  """
181
186
  Run a custom report that retrieves every queue item in the system for the given search criteria.
182
187
 
183
- :param wrapper: The record model wrapper for the process queue items.
188
+ :param wrapper: The record model wrapper for the process queue items. If not provided, the returned records will
189
+ be PyRecordModels instead of WrappedRecordModels.
184
190
  :param criteria: The search criteria to query for queue items with.
185
191
  :return: A list of every queue item in the system that matches the search criteria.
186
192
  """
187
193
  report = self.build_queue_item_report(criteria)
188
- return self.rec_handler.query_models_by_report(wrapper, report)
194
+ dt: type[WrappedType] | str = wrapper if wrapper else ProcessQueueItemFields.DATA_TYPE_NAME
195
+ return CustomReportRecordAutoPager(self.user, report, dt).get_all_at_once()
189
196
 
190
- def get_records_from_item_report(self, wrapper: type[WrappedType],
191
- criteria: QueueItemReportCriteria = QueueItemReportCriteria()) -> list[WrappedType]:
197
+ def get_records_from_item_report(self, wrapper: type[WrappedType] | str,
198
+ criteria: QueueItemReportCriteria = QueueItemReportCriteria()) \
199
+ -> list[WrappedType] | list[PyRecordModel]:
192
200
  """
193
201
  Run a custom report that retrieves for queue items that match the given search criteria, then query for the
194
202
  data records that those queue items refer to.
195
203
 
196
- :param wrapper: The record model wrapper for the records being queried for.
204
+ :param wrapper: The record model wrapper or data type name for the records being queried for.
197
205
  :param criteria: Additional search criteria to filter the results. This function forces the data_type_names
198
206
  parameter of the criteria to match the data type of the given record model wrapper.
199
207
  :return: A list of all records related to the queue items in the system that match the search criteria.
208
+ If a data type name was used instead of a model wrapper, then the returned records will be PyRecordModels
209
+ instead of WrappedRecordModels.
200
210
  """
201
211
  # Don't try to query for process queue items that don't match the data type of this wrapper.
202
- criteria.data_type_names = [wrapper.get_wrapper_data_type_name()]
212
+ criteria.data_type_names = [AliasUtil.to_data_type_name(wrapper)]
203
213
  criteria.not_data_type_names = None
204
214
  report = self.build_queue_item_report(criteria)
205
215
  record_ids: list[int] = [x[ProcessQueueItemFields.DATA_RECORD_ID__FIELD.field_name]
206
- for x in CustomReportUtil.run_custom_report(self.user, report)]
216
+ for x in CustomReportDictAutoPager(self.user, report)]
207
217
  return self.rec_handler.query_models_by_id(wrapper, record_ids)
208
218
 
209
- def get_queue_items_for_records(self, records: Iterable[SapioRecord], wrapper: type[WrappedType],
219
+ def get_queue_items_for_records(self, records: Iterable[SapioRecord], wrapper: type[WrappedType] | None = None,
210
220
  criteria: QueueItemReportCriteria = QueueItemReportCriteria()) \
211
- -> dict[SapioRecord, list[WrappedType]]:
221
+ -> dict[SapioRecord, list[WrappedType] | list[PyRecordModel]]:
212
222
  """
213
223
  Given a list of records, query the system for every process queue item that refers to those records and matches
214
224
  the provided search criteria.
215
225
 
216
226
  :param records: The queued records to query for the process queue items of.
217
- :param wrapper: The record model wrapper for the returned process queue item records.
227
+ :param wrapper: The record model wrapper for the returned process queue item records. If not provided, the
228
+ returned records will be PyRecordModels instead of WrappedRecordModels.
218
229
  :param criteria: Additional search criteria to filter the results. This function forces the data_record_ids and
219
230
  data_type_names parameters on the criteria to match the given records.
220
231
  :return: A dictionary mapping the input records to a list of the process queue items that refer to them. If a
@@ -226,40 +237,43 @@ class QueueItemHandler:
226
237
  criteria.not_data_record_ids = None
227
238
  criteria.data_type_names = AliasUtil.to_data_type_names(records)
228
239
  criteria.not_data_type_names = None
229
- items: list[WrappedType] = self.get_queue_items_from_report(wrapper, criteria)
240
+ items: list[WrappedType] | list[PyRecordModel] = self.get_queue_items_from_report(wrapper, criteria)
230
241
  return self.map_records_to_queue_items(records, items)
231
242
 
232
- def get_records_for_queue_items(self, queue_items: Iterable[SapioRecord], wrapper: type[WrappedType]) \
233
- -> dict[SapioRecord, WrappedType]:
243
+ def get_records_for_queue_items(self, queue_items: Iterable[SapioRecord], wrapper: type[WrappedType] | str) \
244
+ -> dict[SapioRecord, WrappedType | PyRecordModel]:
234
245
  """
235
246
  Given a list of process queue items, query the system for the records that those queue items refer to.
236
247
 
237
248
  :param queue_items: The process queue items to query for the referenced records of.
238
- :param wrapper: The record model wrapper for the records being queried.
239
- :return: A dictionary mapping the input process queue items to the record tht they refer to.
249
+ :param wrapper: The record model wrapper or data type name for the records being queried.
250
+ :return: A dictionary mapping the input process queue items to the record tht they refer to. If a data type
251
+ name was used instead of a model wrapper, then the returned records will be PyRecordModels instead of
252
+ WrappedRecordModels.
240
253
  """
241
254
  record_ids: set[int] = {x.get_field_value(ProcessQueueItemFields.DATA_RECORD_ID__FIELD) for x in queue_items}
242
- records: list[WrappedType] = self.rec_handler.query_models_by_id(wrapper, record_ids)
255
+ records: list[WrappedType] | list[PyRecordModel] = self.rec_handler.query_models_by_id(wrapper, record_ids)
243
256
  return self.map_queue_items_to_records(queue_items, records)
244
257
 
245
258
  def queue_records_for_process(self, records: Iterable[SapioRecord], process: str, step: str,
246
- wrapper: type[WrappedType]) -> dict[SapioRecord, WrappedType]:
259
+ wrapper: type[WrappedType] | None = None) -> dict[SapioRecord, WrappedType | PyRecordModel]:
247
260
  """
248
261
  Given a list of records, create process queue item records for them at the provided process and step names.
249
262
  You must store and commit using the record model manager in order for these changes to take effect.
250
263
 
251
264
  :param records: The records to create process queue items for.
252
- :param wrapper: The record model wrapper for the process queue items being created.
253
265
  :param process: The name of the process to queue for.
254
266
  :param step: The name of the step in the above process to queue for. This is the "Workflow Name" field of the
255
267
  Process Workflow record corresponding to the step you want to assign these records to. For steps that
256
268
  launch an experiment, this is the name of the template that will be launched. For the other types of custom
257
269
  process steps, this is the "Workflow Name" as defined in the process manager config.
270
+ :param wrapper: The record model wrapper for the process queue items being created. If not provided, the
271
+ returned records will be PyRecordModels instead of WrappedRecordModels.
258
272
  :return: A dictionary mapping each input record to the newly created process queue item for that record.
259
273
  """
260
274
  ret_val: dict[SapioRecord, WrappedType] = {}
261
275
  for record in records:
262
- item = self.rec_handler.add_model(wrapper)
276
+ item = self.rec_handler.add_model(wrapper if wrapper else ProcessQueueItemFields.DATA_TYPE_NAME)
263
277
  item.set_field_values({
264
278
  ProcessQueueItemFields.PROCESS_HEADER_NAME__FIELD.field_name: process,
265
279
  ProcessQueueItemFields.WORKFLOW_HEADER_NAME__FIELD.field_name: step,
@@ -271,16 +285,17 @@ class QueueItemHandler:
271
285
  ret_val[record] = item
272
286
  return ret_val
273
287
 
274
- def dequeue_records_for_process(self, records: Iterable[SapioRecord], wrapper: type[WrappedType],
288
+ def dequeue_records_for_process(self, records: Iterable[SapioRecord], wrapper: type[WrappedType] | None = None,
275
289
  criteria: QueueItemReportCriteria = QueueItemReportCriteria()) \
276
- -> dict[SapioRecord, list[WrappedType]]:
290
+ -> dict[SapioRecord, list[WrappedType] | list[PyRecordModel]]:
277
291
  """
278
292
  Given a list of records, locate the process queue items that refer to them and that match the given search
279
293
  criteria and remove them from the queue by setting the ShowInQueue field on the process queue items to false.
280
294
  You must store and commit using the record model manager in order for these changes to take effect.
281
295
 
282
296
  :param records: The records to remove from the queue.
283
- :param wrapper: The record model wrapper for the process queue items being updated.
297
+ :param wrapper: The record model wrapper for the process queue items being updated. If not provided, the
298
+ returned records will be PyRecordModels instead of WrappedRecordModels.
284
299
  :param criteria: Additional search criteria to filter the results. This function forces the show_in_queue
285
300
  parameter on the criteria to True.
286
301
  :return: A dictionary mapping each input record to the queue item records that refer to that record and were
@@ -289,7 +304,8 @@ class QueueItemHandler:
289
304
  """
290
305
  # Only locate queue items that are currently visible in the queue.
291
306
  criteria.shown_in_queue = True
292
- dequeue: dict[SapioRecord, list[WrappedType]] = self.get_queue_items_for_records(records, wrapper, criteria)
307
+ dequeue: dict[SapioRecord, list[WrappedType] | list[PyRecordModel]]
308
+ dequeue = self.get_queue_items_for_records(records, wrapper, criteria)
293
309
  for record, items in dequeue.items():
294
310
  for item in items:
295
311
  item.set_field_value(ProcessQueueItemFields.SHOW_IN_QUEUE__FIELD.field_name, False)