Skip to content

Commit

Permalink
feat: include LogEntry instance in post_log signal (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppmathis committed Feb 8, 2024
1 parent 4473477 commit f3238c9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
10 changes: 5 additions & 5 deletions auditlog/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,24 @@ def _create_log_entry(
return

error = None
log_created = False
log_entry = None
changes = None
try:
changes = model_instance_diff(
diff_old, diff_new, fields_to_check=fields_to_check
)

if force_log or changes:
LogEntry.objects.log_create(
log_entry = LogEntry.objects.log_create(
instance,
action=action,
changes=changes,
force_log=force_log,
)
log_created = True
except BaseException as e:
error = e
finally:
if log_created or error:
if log_entry or error:
post_log.send(
sender,
instance=instance,
Expand All @@ -140,7 +139,8 @@ def _create_log_entry(
error=error,
pre_log_results=pre_log_results,
changes=changes,
log_created=log_created,
log_entry=log_entry,
log_created=log_entry is not None,
)
if error:
raise error
Expand Down
4 changes: 4 additions & 0 deletions auditlog/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
this will be None. In some cases, such as when logging access to the instance,
the changes will be an empty dict.
:param Optional[LogEntry] log_entry:
The log entry that was created and stored in the database. If there was an error,
this will be None.
:param bool log_created:
Was the log actually created?
This could be false if there was an error in creating the log.
Expand Down
11 changes: 8 additions & 3 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,7 @@ def setUp(self):
"my_instance": None,
"my_action": None,
"my_error": None,
"my_log_entry": None,
}

def assertSignals(self, action):
Expand All @@ -2397,6 +2398,7 @@ def assertSignals(self, action):
self.assertIs(self.my_post_log_data["my_instance"], self.obj)
self.assertEqual(self.my_post_log_data["my_action"], action)
self.assertIsNone(self.my_post_log_data["my_error"])
self.assertIsNotNone(self.my_post_log_data["my_log_entry"])

def test_custom_signals(self):
my_ret_val = random.randint(0, 10000)
Expand All @@ -2413,13 +2415,14 @@ def pre_log_receiver_extra(*_args, **_kwargs):
return my_other_ret_val

def post_log_receiver(
sender, instance, action, error, pre_log_results, **_kwargs
sender, instance, action, error, log_entry, pre_log_results, **_kwargs
):
self.my_post_log_data["is_called"] = True
self.my_post_log_data["my_sender"] = sender
self.my_post_log_data["my_instance"] = instance
self.my_post_log_data["my_action"] = action
self.my_post_log_data["my_error"] = error
self.my_post_log_data["my_log_entry"] = log_entry

self.assertEqual(len(pre_log_results), 2)

Expand Down Expand Up @@ -2482,12 +2485,13 @@ def pre_log_receiver(sender, instance, action, **_kwargs):
self.my_pre_log_data["my_instance"] = instance
self.my_pre_log_data["my_action"] = action

def post_log_receiver(sender, instance, action, error, **_kwargs):
def post_log_receiver(sender, instance, action, error, log_entry, **_kwargs):
self.my_post_log_data["is_called"] = True
self.my_post_log_data["my_sender"] = sender
self.my_post_log_data["my_instance"] = instance
self.my_post_log_data["my_action"] = action
self.my_post_log_data["my_error"] = error
self.my_post_log_data["my_log_entry"] = log_entry

pre_log.connect(pre_log_receiver)
post_log.connect(post_log_receiver)
Expand All @@ -2504,12 +2508,13 @@ def pre_log_receiver(sender, instance, action, **_kwargs):
self.my_pre_log_data["my_instance"] = instance
self.my_pre_log_data["my_action"] = action

def post_log_receiver(sender, instance, action, error, **_kwargs):
def post_log_receiver(sender, instance, action, error, log_entry, **_kwargs):
self.my_post_log_data["is_called"] = True
self.my_post_log_data["my_sender"] = sender
self.my_post_log_data["my_instance"] = instance
self.my_post_log_data["my_action"] = action
self.my_post_log_data["my_error"] = error
self.my_post_log_data["my_log_entry"] = log_entry

pre_log.connect(pre_log_receiver)
post_log.connect(post_log_receiver)
Expand Down

0 comments on commit f3238c9

Please sign in to comment.