sapiopycommons 2024.8.15a304__py3-none-any.whl → 2024.8.19a305__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 (28) hide show
  1. sapiopycommons/callbacks/callback_util.py +122 -25
  2. sapiopycommons/customreport/__init__.py +0 -0
  3. sapiopycommons/customreport/column_builder.py +60 -0
  4. sapiopycommons/customreport/custom_report_builder.py +125 -0
  5. sapiopycommons/customreport/term_builder.py +296 -0
  6. sapiopycommons/datatype/attachment_util.py +15 -6
  7. sapiopycommons/eln/experiment_handler.py +193 -39
  8. sapiopycommons/files/complex_data_loader.py +1 -1
  9. sapiopycommons/files/file_bridge.py +1 -1
  10. sapiopycommons/files/file_bridge_handler.py +21 -0
  11. sapiopycommons/files/file_util.py +38 -5
  12. sapiopycommons/files/file_validator.py +21 -6
  13. sapiopycommons/files/file_writer.py +44 -15
  14. sapiopycommons/general/aliases.py +93 -2
  15. sapiopycommons/general/audit_log.py +200 -0
  16. sapiopycommons/general/popup_util.py +17 -0
  17. sapiopycommons/general/sapio_links.py +48 -0
  18. sapiopycommons/general/time_util.py +40 -0
  19. sapiopycommons/recordmodel/record_handler.py +114 -17
  20. sapiopycommons/rules/eln_rule_handler.py +29 -22
  21. sapiopycommons/rules/on_save_rule_handler.py +29 -28
  22. sapiopycommons/webhook/webhook_handlers.py +90 -26
  23. sapiopycommons/webhook/webservice_handlers.py +67 -0
  24. {sapiopycommons-2024.8.15a304.dist-info → sapiopycommons-2024.8.19a305.dist-info}/METADATA +1 -1
  25. sapiopycommons-2024.8.19a305.dist-info/RECORD +50 -0
  26. sapiopycommons-2024.8.15a304.dist-info/RECORD +0 -43
  27. {sapiopycommons-2024.8.15a304.dist-info → sapiopycommons-2024.8.19a305.dist-info}/WHEEL +0 -0
  28. {sapiopycommons-2024.8.15a304.dist-info → sapiopycommons-2024.8.19a305.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,296 @@
1
+ from typing import Iterable
2
+
3
+ from sapiopylib.rest.pojo.CustomReport import RawTermOperation, CompositeTermOperation, RawReportTerm, \
4
+ CompositeReportTerm, AbstractReportTerm, FieldCompareReportTerm
5
+
6
+ from sapiopycommons.general.aliases import DataTypeIdentifier, AliasUtil, FieldIdentifier
7
+
8
+ # Raw term operations, for comparing field values.
9
+ EQ = RawTermOperation.EQUAL_TO_OPERATOR
10
+ NEQ = RawTermOperation.NOT_EQUAL_TO_OPERATOR
11
+ LT = RawTermOperation.LESS_THAN_OPERATOR
12
+ LTE = RawTermOperation.LESS_THAN_OR_EQUAL_OPERATOR
13
+ GT = RawTermOperation.GREATER_THAN_OPERATOR
14
+ GTE = RawTermOperation.GREATER_THAN_OR_EQUAL_OPERATOR
15
+
16
+ # Composite term operations, for comparing two terms.
17
+ AND = CompositeTermOperation.AND_OPERATOR
18
+ OR = CompositeTermOperation.OR_OPERATOR
19
+
20
+ # Forms that field term values can take.
21
+ TermValue = str | int | float | bool | Iterable
22
+
23
+
24
+ class TermBuilder:
25
+ """
26
+ A class that allows for the easier constructions of custom report terms.
27
+ """
28
+ @staticmethod
29
+ def all_records_term(data_type: DataTypeIdentifier) -> RawReportTerm:
30
+ """
31
+ Create a raw report term that captures all records of the given data type.
32
+
33
+ :param data_type: The data type of this term.
34
+ :return: A raw report term for "data_type.RecordId >= 0".
35
+ """
36
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), "RecordId", GTE, TermBuilder.to_term_val(0))
37
+
38
+ @staticmethod
39
+ def is_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
40
+ *, trim: bool = False) -> RawReportTerm:
41
+ """
42
+ Create a raw report term for comparing a field value with an equals operation.
43
+
44
+ :param data_type: The data type of this term.
45
+ :param field: The data field of this term.
46
+ :param value: The value to compare for this term.
47
+ :param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
48
+ :return: A raw report term for "data_type.field = value".
49
+ """
50
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), EQ,
51
+ TermBuilder.to_term_val(value), trim)
52
+
53
+ @staticmethod
54
+ def not_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
55
+ *, trim: bool = False) -> RawReportTerm:
56
+ """
57
+ Create a raw report term for comparing a field value with a not equals operation.
58
+
59
+ :param data_type: The data type of this term.
60
+ :param field: The data field of this term.
61
+ :param value: The value to compare for this term.
62
+ :param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
63
+ :return: A raw report term for "data_type.field != value".
64
+ """
65
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), NEQ,
66
+ TermBuilder.to_term_val(value), trim)
67
+
68
+ @staticmethod
69
+ def lt_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
70
+ *, trim: bool = False) -> RawReportTerm:
71
+ """
72
+ Create a raw report term for comparing a field value with a less than operation.
73
+
74
+ :param data_type: The data type of this term.
75
+ :param field: The data field of this term.
76
+ :param value: The value to compare for this term.
77
+ :param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
78
+ :return: A raw report term for "data_type.field < value".
79
+ """
80
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), LT,
81
+ TermBuilder.to_term_val(value), trim)
82
+
83
+ @staticmethod
84
+ def lte_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
85
+ *, trim: bool = False) -> RawReportTerm:
86
+ """
87
+ Create a raw report term for comparing a field value with a less than or equal to operation.
88
+
89
+ :param data_type: The data type of this term.
90
+ :param field: The data field of this term.
91
+ :param value: The value to compare for this term.
92
+ :param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
93
+ :return: A raw report term for "data_type.field <= value".
94
+ """
95
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), LTE,
96
+ TermBuilder.to_term_val(value), trim)
97
+
98
+ @staticmethod
99
+ def gt_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
100
+ *, trim: bool = False) -> RawReportTerm:
101
+ """
102
+ Create a raw report term for comparing a field value with a greater than operation.
103
+
104
+ :param data_type: The data type of this term.
105
+ :param field: The data field of this term.
106
+ :param value: The value to compare for this term.
107
+ :param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
108
+ :return: A raw report term for "data_type.field > value".
109
+ """
110
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), GT,
111
+ TermBuilder.to_term_val(value), trim)
112
+
113
+ @staticmethod
114
+ def gte_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
115
+ *, trim: bool = False) -> RawReportTerm:
116
+ """
117
+ Create a raw report term for comparing a field value with a greater than or equal to operation.
118
+
119
+ :param data_type: The data type of this term.
120
+ :param field: The data field of this term.
121
+ :param value: The value to compare for this term.
122
+ :param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
123
+ :return: A raw report term for "data_type.field >= value".
124
+ """
125
+ return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), GTE,
126
+ TermBuilder.to_term_val(value), trim)
127
+
128
+ @staticmethod
129
+ def compare_is_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
130
+ data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
131
+ *, trim: bool = False) -> FieldCompareReportTerm:
132
+ """
133
+ Create a field comparison report term for comparing field values between data types with an equals operation.
134
+
135
+ :param data_type_A: The data type for the left side of this term.
136
+ :param field_A: The data field for the left side of this term.
137
+ :param data_type_B: The data type for the right side of this term.
138
+ :param field_B: The data field for the right side of this term.
139
+ :param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
140
+ :return: A field comparison report term for "data_type_A.field_A = data_type_B.field_B".
141
+ """
142
+ return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), EQ,
143
+ AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
144
+
145
+ @staticmethod
146
+ def compare_not_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
147
+ data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
148
+ *, trim: bool = False) -> FieldCompareReportTerm:
149
+ """
150
+ Create a field comparison report term for comparing field values between data types with a not equals operation.
151
+
152
+ :param data_type_A: The data type for the left side of this term.
153
+ :param field_A: The data field for the left side of this term.
154
+ :param data_type_B: The data type for the right side of this term.
155
+ :param field_B: The data field for the right side of this term.
156
+ :param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
157
+ :return: A field comparison report term for "data_type_A.field_A != data_type_B.field_B".
158
+ """
159
+ return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), NEQ,
160
+ AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
161
+
162
+ @staticmethod
163
+ def compare_lt_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
164
+ data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
165
+ *, trim: bool = False) -> FieldCompareReportTerm:
166
+ """
167
+ Create a field comparison report term for comparing field values between data types with a less than operation.
168
+
169
+ :param data_type_A: The data type for the left side of this term.
170
+ :param field_A: The data field for the left side of this term.
171
+ :param data_type_B: The data type for the right side of this term.
172
+ :param field_B: The data field for the right side of this term.
173
+ :param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
174
+ :return: A field comparison report term for "data_type_A.field_A < data_type_B.field_B".
175
+ """
176
+ return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), LT,
177
+ AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
178
+
179
+ @staticmethod
180
+ def compare_lte_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
181
+ data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
182
+ *, trim: bool = False) -> FieldCompareReportTerm:
183
+ """
184
+ Create a field comparison report term for comparing field values between data types with a less than or equal
185
+ to operation.
186
+
187
+ :param data_type_A: The data type for the left side of this term.
188
+ :param field_A: The data field for the left side of this term.
189
+ :param data_type_B: The data type for the right side of this term.
190
+ :param field_B: The data field for the right side of this term.
191
+ :param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
192
+ :return: A field comparison report term for "data_type_A.field_A <= data_type_B.field_B".
193
+ """
194
+ return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), LTE,
195
+ AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
196
+
197
+ @staticmethod
198
+ def compare_gt_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
199
+ data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
200
+ *, trim: bool = False) -> FieldCompareReportTerm:
201
+ """
202
+ Create a field comparison report term for comparing field values between data types with a greater than
203
+ operation.
204
+
205
+ :param data_type_A: The data type for the left side of this term.
206
+ :param field_A: The data field for the left side of this term.
207
+ :param data_type_B: The data type for the right side of this term.
208
+ :param field_B: The data field for the right side of this term.
209
+ :param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
210
+ :return: A field comparison report term for "data_type_A.field_A > data_type_B.field_B".
211
+ """
212
+ return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), GT,
213
+ AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
214
+
215
+ @staticmethod
216
+ def compare_gte_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
217
+ data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
218
+ *, trim: bool = False) -> FieldCompareReportTerm:
219
+ """
220
+ Create a field comparison report term for comparing field values between data types with a greater than or
221
+ equal to operation.
222
+
223
+ :param data_type_A: The data type for the left side of this term.
224
+ :param field_A: The data field for the left side of this term.
225
+ :param data_type_B: The data type for the right side of this term.
226
+ :param field_B: The data field for the right side of this term.
227
+ :param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
228
+ :return: A field comparison report term for "data_type_A.field_A >= data_type_B.field_B".
229
+ """
230
+ return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), GTE,
231
+ AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
232
+
233
+ @staticmethod
234
+ def or_terms(a: AbstractReportTerm, b: AbstractReportTerm, *, is_negated: bool = False) -> CompositeReportTerm:
235
+ """
236
+ Combine two report terms with an OR operation.
237
+
238
+ :param a: The first term in the operation.
239
+ :param b: The second term in the operation.
240
+ :param is_negated: Whether the returned term should be negated (i.e. turn this into a nor operation).
241
+ :return: A composite report term for "A or B".
242
+ """
243
+ return CompositeReportTerm(a, OR, b, is_negated)
244
+
245
+ @staticmethod
246
+ def and_terms(a: AbstractReportTerm, b: AbstractReportTerm, *, is_negated: bool = False) -> CompositeReportTerm:
247
+ """
248
+ Combine two report terms with an AND operation.
249
+
250
+ :param a: The first term in the operation.
251
+ :param b: The second term in the operation.
252
+ :param is_negated: Whether the returned term should be negated (i.e. turn this into a nand operation).
253
+ :return: A composite report term for "A and B".
254
+ """
255
+ return CompositeReportTerm(a, AND, b, is_negated)
256
+
257
+ @staticmethod
258
+ def xor_terms(a: AbstractReportTerm, b: AbstractReportTerm, *, is_negated: bool = False) -> CompositeReportTerm:
259
+ """
260
+ Combine two report terms with a XOR operation. Note that a XOR operation doesn't actually exist for custom
261
+ reports. This instead constructs a term that is "(A or B) and !(A and B)", which is equivalent to a XOR
262
+ operation.
263
+
264
+ :param a: The first term in the operation.
265
+ :param b: The second term in the operation.
266
+ :param is_negated: Whether the returned term should be negated (i.e. turn this into an xnor operation).
267
+ :return: A composite report term for "A xor B".
268
+ """
269
+ return TermBuilder.and_terms(TermBuilder.or_terms(a, b),
270
+ TermBuilder.and_terms(a, b, is_negated=True),
271
+ is_negated=is_negated)
272
+
273
+ @staticmethod
274
+ def to_term_val(value: TermValue) -> str:
275
+ """
276
+ Convert the given value to be used in a custom report term to a string. Term values may be strings, integers,
277
+ floats, booleans, or lists of values.
278
+
279
+ :param value: A value to be used in a custom report term.
280
+ :return: The provided value formatted as a string that can be used
281
+ """
282
+ # If the given value is already a string, then nothing needs to be done with it.
283
+ if not isinstance(value, str):
284
+ # If the given value is an iterable object, then the return value is the contents of that iterable
285
+ # in a comma separated list surrounded by curly braces.
286
+ if isinstance(value, Iterable):
287
+ # When converting a list of values to a string, values in the list which are already strings should be
288
+ # put in quotation marks so that strings that contain commas do not get split up. All other value
289
+ # types can be simply converted to a string, though.
290
+ def convert_list_value(val: TermValue) -> str:
291
+ return f"'{val}'" if isinstance(val, str) else str(val)
292
+ value = "{" + ",".join([convert_list_value(x) for x in value]) + "}"
293
+ else:
294
+ # Otherwise, the value is simply cast to a string.
295
+ value = str(value)
296
+ return value
@@ -1,5 +1,6 @@
1
1
  import io
2
2
 
3
+ from sapiopylib.rest.DataMgmtService import DataMgmtServer
3
4
  from sapiopylib.rest.User import SapioUser
4
5
  from sapiopylib.rest.pojo.webhook.WebhookContext import SapioWebhookContext
5
6
  from sapiopylib.rest.utils.recordmodel.RecordModelWrapper import WrappedType
@@ -12,31 +13,35 @@ from sapiopycommons.recordmodel.record_handler import RecordHandler
12
13
  # FR-46064 - Initial port of PyWebhookUtils to sapiopycommons.
13
14
  class AttachmentUtil:
14
15
  @staticmethod
15
- def get_attachment_bytes(context: SapioWebhookContext, attachment: SapioRecord) -> bytes:
16
+ def get_attachment_bytes(context: SapioWebhookContext | SapioUser, attachment: SapioRecord) -> bytes:
16
17
  """
17
18
  Get the data bytes for the given attachment record. Makes a webservice call to retrieve the data.
18
19
 
19
- :param context: The current webhook context.
20
+ :param context: The current webhook context or a user object to send requests from.
20
21
  :param attachment: The attachment record.
21
22
  :return: The bytes for the attachment's file data.
22
23
  """
23
24
  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)
24
29
  with io.BytesIO() as data_sink:
25
30
  def consume_data(chunk: bytes):
26
31
  data_sink.write(chunk)
27
- context.data_record_manager.get_attachment_data(attachment, consume_data)
32
+ dr_man.get_attachment_data(attachment, consume_data)
28
33
  data_sink.flush()
29
34
  data_sink.seek(0)
30
35
  file_bytes = data_sink.read()
31
36
  return file_bytes
32
37
 
33
38
  @staticmethod
34
- def set_attachment_bytes(context: SapioWebhookContext, attachment: SapioRecord,
39
+ def set_attachment_bytes(context: SapioWebhookContext | SapioUser, attachment: SapioRecord,
35
40
  file_name: str, file_bytes: bytes) -> None:
36
41
  """
37
42
  Set the attachment data for a given attachment record. Makes a webservice call to set the data.
38
43
 
39
- :param context: The current webhook context.
44
+ :param context: The current webhook context or a user object to send requests from.
40
45
  :param attachment: The attachment record. Must be an existing data record that is an attachment type.
41
46
  :param file_name: The name of the attachment.
42
47
  :param file_bytes: The bytes of the attachment data.
@@ -45,8 +50,12 @@ class AttachmentUtil:
45
50
  raise SapioException("Provided record cannot have its attachment data set, as it does not exist in the "
46
51
  "system yet.")
47
52
  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)
48
57
  with io.BytesIO(file_bytes) as stream:
49
- context.data_record_manager.set_attachment_data(attachment, file_name, stream)
58
+ dr_man.set_attachment_data(attachment, file_name, stream)
50
59
 
51
60
  @staticmethod
52
61
  def create_attachment(context: SapioWebhookContext | SapioUser, file_name: str, file_bytes: bytes,