mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Conditionally disable / enable logging (#590)
* Update receivers.py * Update signals.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update signals.py Removed trailing whitespace... * Update tests.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
This commit is contained in:
parent
c581a6e647
commit
140719eeb5
4 changed files with 37 additions and 1 deletions
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
## Next Release
|
||||
|
||||
#### Improvements
|
||||
- feat: If any receiver returns False, no logging will be made. This can be useful if logging should be conditionally enabled / disabled ([#590](https://github.com/jazzband/django-auditlog/pull/590))
|
||||
|
||||
## 3.0.0-beta.3 (2023-11-13)
|
||||
|
||||
#### Improvements
|
||||
|
|
|
|||
|
|
@ -108,6 +108,10 @@ def _create_log_entry(
|
|||
instance=instance,
|
||||
action=action,
|
||||
)
|
||||
|
||||
if any(item[1] is False for item in pre_log_results):
|
||||
return
|
||||
|
||||
error = None
|
||||
log_created = False
|
||||
changes = None
|
||||
|
|
|
|||
|
|
@ -20,7 +20,9 @@ Keyword arguments sent with this signal:
|
|||
audit log entry. Type: :class:`auditlog.models.LogEntry.Action`
|
||||
|
||||
The receivers' return values are sent to any :func:`post_log`
|
||||
signal receivers.
|
||||
signal receivers, with one exception: if any receiver returns False,
|
||||
no logging will be made. This can be useful if logging should be
|
||||
conditionally enabled / disabled
|
||||
"""
|
||||
|
||||
post_log = django.dispatch.Signal()
|
||||
|
|
|
|||
|
|
@ -2420,6 +2420,33 @@ class SignalTests(TestCase):
|
|||
|
||||
self.assertSignals(LogEntry.Action.CREATE)
|
||||
|
||||
def test_disabled_logging(self):
|
||||
log_count = LogEntry.objects.count()
|
||||
|
||||
def pre_log_receiver(sender, instance, action, **_kwargs):
|
||||
return True
|
||||
|
||||
def pre_log_receiver_extra(*_args, **_kwargs):
|
||||
pass
|
||||
|
||||
def pre_log_receiver_disable(*_args, **_kwargs):
|
||||
return False
|
||||
|
||||
pre_log.connect(pre_log_receiver)
|
||||
pre_log.connect(pre_log_receiver_extra)
|
||||
|
||||
self.obj = SimpleModel.objects.create(text="I am not difficult.")
|
||||
|
||||
self.assertEqual(LogEntry.objects.count(), log_count + 1)
|
||||
|
||||
log_count = LogEntry.objects.count()
|
||||
|
||||
pre_log.connect(pre_log_receiver_disable)
|
||||
|
||||
self.obj = SimpleModel.objects.create(text="I am not difficult.")
|
||||
|
||||
self.assertEqual(LogEntry.objects.count(), log_count)
|
||||
|
||||
def test_custom_signals_update(self):
|
||||
def pre_log_receiver(sender, instance, action, **_kwargs):
|
||||
self.my_pre_log_data["is_called"] = True
|
||||
|
|
|
|||
Loading…
Reference in a new issue