Coverage for website/utils/models/signals.py: 100.00%
13 statements
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
1import functools
3from django.conf import settings
4from django.dispatch import receiver
7def suspendingreceiver(signal, **decorator_kwargs):
8 """Create wrapper around the standard django receiver that prevents the receiver from running if the setting `SUSPEND_SIGNALS` is `True`.
10 This is particularly useful if you want to prevent signals running during
11 unit testing.
13 @override_settings(SUSPEND_SIGNALS=True)
14 class MyTestCase(TestCase):
15 def test_method(self):
16 Model.objects.create() # post_save_receiver won't execute
18 @suspendingreceiver(post_save, sender=Model)
19 def post_save_receiver(sender, instance=None, **kwargs):
20 work(instance)
22 Thank you https://gist.github.com/jarshwah/5b234dbf9a0e429d71e060c247ad20ef
23 """
25 def our_wrapper(func):
26 @receiver(signal, **decorator_kwargs)
27 @functools.wraps(func)
28 def fake_receiver(**kwargs):
29 if settings.SUSPEND_SIGNALS:
30 return
31 func(**kwargs)
33 return fake_receiver
35 return our_wrapper