From f3238c9bdbd0d3979bcd218ac114611e7400affa Mon Sep 17 00:00:00 2001 From: Pascal Mathis Date: Thu, 8 Feb 2024 21:55:46 +0100 Subject: [PATCH] feat: include `LogEntry` instance in `post_log` signal (#605) --- auditlog/receivers.py | 10 +++++----- auditlog/signals.py | 4 ++++ auditlog_tests/tests.py | 11 ++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/auditlog/receivers.py b/auditlog/receivers.py index a0675808..02405d53 100644 --- a/auditlog/receivers.py +++ b/auditlog/receivers.py @@ -113,7 +113,7 @@ def _create_log_entry( return error = None - log_created = False + log_entry = None changes = None try: changes = model_instance_diff( @@ -121,17 +121,16 @@ def _create_log_entry( ) 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, @@ -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 diff --git a/auditlog/signals.py b/auditlog/signals.py index 8c5ca339..370303f6 100644 --- a/auditlog/signals.py +++ b/auditlog/signals.py @@ -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. diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index bcb414c7..c16251d2 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -2380,6 +2380,7 @@ def setUp(self): "my_instance": None, "my_action": None, "my_error": None, + "my_log_entry": None, } def assertSignals(self, action): @@ -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) @@ -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) @@ -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) @@ -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)