fix: only fire the post_log signal when the log is created or when there is an error (#561)

This commit is contained in:
Abdullah Alaqeel 2023-09-07 15:28:47 +03:00 committed by GitHub
parent 858034b0c1
commit af31261946
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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