From 1ac9243749330a4d2b547603c749d44d667f8fee Mon Sep 17 00:00:00 2001 From: graham-dds Date: Thu, 17 Oct 2019 16:23:07 -0400 Subject: [PATCH] edit create_audit_event method of AuditableMixin - create dictonary of log data and log it. Only create insance of AuditEvent if AUDIT_LOG_FEATURE_TOGGLE is set to True --- atst/models/mixins/auditable.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/atst/models/mixins/auditable.py b/atst/models/mixins/auditable.py index acfa6b2e..b2a3f527 100644 --- a/atst/models/mixins/auditable.py +++ b/atst/models/mixins/auditable.py @@ -17,24 +17,29 @@ class AuditableMixin(object): if changed_state is None: changed_state = resource.history if action == ACTION_UPDATE else None - audit_event = AuditEvent( - user_id=user_id, - portfolio_id=resource.portfolio_id, - application_id=resource.application_id, - resource_type=resource.resource_type, - resource_id=resource.id, - display_name=resource.displayname, - action=action, - changed_state=changed_state, - event_details=resource.event_details, - ) + log_data = { + "user_id": user_id, + "portfolio_id": resource.portfolio_id, + "application_id": resource.application_id, + "resource_type": resource.resource_type, + "resource_id": resource.id, + "display_name": resource.displayname, + "action": action, + "changed_state": changed_state, + "event_details": resource.event_details, + } app.logger.info( "Audit Event {}".format(action), - extra={"audit_event": audit_event.log, "tags": ["audit_event", action]}, + extra={ + "audit_event": {key: str(value) for key, value in log_data.items()}, + "tags": ["audit_event", action], + }, ) - audit_event.save(connection) - return audit_event + + if app.config.get("AUDIT_LOG_FEATURE_TOGGLE", False): + audit_event = AuditEvent(**log_data) + audit_event.save(connection) @classmethod def __declare_last__(cls):