Skip to content

Issue 48490: Add auditing behavior param via new options param #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ What's New in the LabKey 3.0.0 package
- Query API - WAF encode "sql" parameter for execute_sql
- WAF encoding of parameters is initially supported with LabKey Server v23.09
- WAF encoding can be opted out of on execute_sql calls by specifying waf_encode_sql=False
- Query API - add optional parameters to insert_rows, update_rows, and delete_rows

What's New in the LabKey 2.6.1 package
==============================
Expand Down
102 changes: 99 additions & 3 deletions labkey/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,25 @@ def __repr__(self):
return "<QueryFilter [{} {} {}]>".format(self.column_name, self.filter_type, self.value)


class AuditBehavior:
"""
Enum of different auditing levels
"""

DETAILED = "DETAILED"
NONE = "NONE"
SUMMARY = "SUMMARY"


def delete_rows(
server_context: ServerContext,
schema_name: str,
query_name: str,
rows: any,
container_path: str = None,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
"""
Expand All @@ -180,12 +193,25 @@ def delete_rows(
:param query_name: table name to delete from
:param rows: Set of rows to delete
:param container_path: labkey container path if not already set in context
:param transacted: whether all of the updates should be done in a single transaction
:param audit_behavior: used to override the audit behavior for the update. See class query.AuditBehavior
:param audit_user_comment: used to provide a comment that will be attached to certain detailed audit log records
:param timeout: timeout of request in seconds (defaults to 30s)
:return:
"""
url = server_context.build_url("query", "deleteRows.api", container_path=container_path)

payload = {"schemaName": schema_name, "queryName": query_name, "rows": rows}

if transacted is False:
payload["transacted"] = transacted

if audit_behavior is not None:
payload["auditBehavior"] = audit_behavior

if audit_user_comment is not None:
payload["auditUserComment"] = audit_user_comment

return server_context.make_request(
url,
json=payload,
Expand Down Expand Up @@ -288,6 +314,10 @@ def insert_rows(
query_name: str,
rows: List[any],
container_path: str = None,
skip_reselect_rows: bool = False,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
"""
Expand All @@ -297,13 +327,29 @@ def insert_rows(
:param query_name: table name to insert into
:param rows: set of rows to insert
:param container_path: labkey container path if not already set in context
:param skip_reselect_rows: whether the full detailed response for the insert can be skipped
:param transacted: whether all of the updates should be done in a single transaction
:param audit_behavior: used to override the audit behavior for the update. See class query.AuditBehavior
:param audit_user_comment: used to provide a comment that will be attached to certain detailed audit log records
:param timeout: timeout of request in seconds (defaults to 30s)
:return:
"""
url = server_context.build_url("query", "insertRows.api", container_path=container_path)

payload = {"schemaName": schema_name, "queryName": query_name, "rows": rows}

if skip_reselect_rows is True:
payload["skipReselectRows"] = skip_reselect_rows

if transacted is False:
payload["transacted"] = transacted

if audit_behavior is not None:
payload["auditBehavior"] = audit_behavior

if audit_user_comment is not None:
payload["auditUserComment"] = audit_user_comment

return server_context.make_request(
url,
json=payload,
Expand Down Expand Up @@ -422,6 +468,9 @@ def update_rows(
query_name: str,
rows: List[any],
container_path: str = None,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
"""
Expand All @@ -432,13 +481,25 @@ def update_rows(
:param query_name: table name to update
:param rows: Set of rows to update
:param container_path: labkey container path if not already set in context
:param transacted: whether all of the updates should be done in a single transaction
:param audit_behavior: used to override the audit behavior for the update. See class query.AuditBehavior
:param audit_user_comment: used to provide a comment that will be attached to certain detailed audit log records
:param timeout: timeout of request in seconds (defaults to 30s)
:return:
"""
url = server_context.build_url("query", "updateRows.api", container_path=container_path)

payload = {"schemaName": schema_name, "queryName": query_name, "rows": rows}

if transacted is False:
payload["transacted"] = transacted

if audit_behavior is not None:
payload["auditBehavior"] = audit_behavior

if audit_user_comment is not None:
payload["auditUserComment"] = audit_user_comment

return server_context.make_request(
url,
json=payload,
Expand All @@ -461,10 +522,21 @@ def delete_rows(
query_name: str,
rows: any,
container_path: str = None,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
return delete_rows(
self.server_context, schema_name, query_name, rows, container_path, timeout
self.server_context,
schema_name,
query_name,
rows,
container_path,
transacted,
audit_behavior,
audit_user_comment,
timeout
)

@functools.wraps(truncate_table)
Expand Down Expand Up @@ -512,10 +584,23 @@ def insert_rows(
query_name: str,
rows: List[any],
container_path: str = None,
skip_reselect_rows: bool = False,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
return insert_rows(
self.server_context, schema_name, query_name, rows, container_path, timeout
self.server_context,
schema_name,
query_name,
rows,
container_path,
skip_reselect_rows,
transacted,
audit_behavior,
audit_user_comment,
timeout
)

@functools.wraps(select_rows)
Expand Down Expand Up @@ -571,8 +656,19 @@ def update_rows(
query_name: str,
rows: List[any],
container_path: str = None,
transacted: bool = True,
audit_behavior: AuditBehavior = None,
audit_user_comment: str = None,
timeout: int = _default_timeout,
):
return update_rows(
self.server_context, schema_name, query_name, rows, container_path, timeout
self.server_context,
schema_name,
query_name,
rows,
container_path,
transacted,
audit_behavior,
audit_user_comment,
timeout
)