diff --git a/CHANGELOG.md b/CHANGELOG.md index f674852..e59dcc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Improvements #### Fixes +* fix: only fire the `post_log` signal when the log is created or when there is an error in the process. ## 3.0.0-beta.1 diff --git a/auditlog/receivers.py b/auditlog/receivers.py index 08f4a99..2b123db 100644 --- a/auditlog/receivers.py +++ b/auditlog/receivers.py @@ -105,6 +105,8 @@ def _create_log_entry( action=action, ) error = None + log_created = False + changes = None try: changes = model_instance_diff( diff_old, diff_new, fields_to_check=fields_to_check @@ -117,17 +119,21 @@ def _create_log_entry( changes=changes, force_log=force_log, ) + log_created = True except BaseException as e: error = e finally: - post_log.send( - sender, - instance=instance, - instance_old=diff_old, - action=action, - error=error, - pre_log_results=pre_log_results, - ) + if log_created or error: + post_log.send( + sender, + instance=instance, + instance_old=diff_old, + action=action, + error=error, + pre_log_results=pre_log_results, + changes=changes, + log_created=log_created, + ) if error: raise error diff --git a/auditlog/signals.py b/auditlog/signals.py index aec291a..0f992d1 100644 --- a/auditlog/signals.py +++ b/auditlog/signals.py @@ -27,6 +27,8 @@ post_log = django.dispatch.Signal() """ Whenever an audit log entry is written, this signal is sent after writing the log. +This signal is also fired when there is an error in creating the log. + Keyword arguments sent with this signal: :param class sender: @@ -39,6 +41,15 @@ Keyword arguments sent with this signal: The action on the model resulting in an audit log entry. Type: :class:`auditlog.models.LogEntry.Action` +:param Optional[dict] changes: + The changes that were logged. If there was en error while determining the changes, + this will be None. In some cases, such as when logging access to the instance, + the changes will be an empty dict. + +:param bool log_created: + Was the log actually created? + This could be false if there was an error in creating the log. + :param Optional[Exception] error: The error, if one occurred while saving the audit log entry. ``None``, otherwise