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

1import functools 

2 

3from django.conf import settings 

4from django.dispatch import receiver 

5 

6 

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`. 

9 

10 This is particularly useful if you want to prevent signals running during 

11 unit testing. 

12 

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 

17 

18 @suspendingreceiver(post_save, sender=Model) 

19 def post_save_receiver(sender, instance=None, **kwargs): 

20 work(instance) 

21 

22 Thank you https://gist.github.com/jarshwah/5b234dbf9a0e429d71e060c247ad20ef 

23 """ 

24 

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) 

32 

33 return fake_receiver 

34 

35 return our_wrapper