def connect_signal(signal, listener, *args, **kw): """helper function for connecting django signals to listeners with a simple check to avoid it to happen when running tests. Use where you would use signal.connect. For example, instead of doing: >>> from deal.models import Deal >>> from django.db.models.signals import post_save >>> def my_listener(sender, instance, **kw): ... redis.sadd('yipit:deals', instance.id) ... >>> post_save.connect(my_listener, sender=Deal) you should rather use: >>> import truck >>> truck.connect_signal(post_save, my_listener, sender=Deal) It will protect the listener to be triggered, for instance, within unit and functional tests """ if LISTENERS_OFF_BY_DEFAULT: signal_registry.append((signal, listener, args, kw)) # never connect signals in tests return return signal.connect(listener, *args, **kw)
def wrapper(function): if 'sender' in kw: raise TypeError( "Listeners to custom signals should not specify sender. {}, line: {}" .format(function.func_code.co_filename, function.func_code.co_firstlineno)) if not LISTENERS_OFF_BY_DEFAULT: connected = signal_receiver(signal, *args, **kw) return connected(function) else: signal_registry.append((signal, function, args, kw)) return function
def wrapper(function): if 'sender' in kw: raise TypeError( "Listeners to custom signals should not specify sender. {}, line: {}".format( function.func_code.co_filename, function.func_code.co_firstlineno ) ) if not LISTENERS_OFF_BY_DEFAULT: connected = signal_receiver(signal, *args, **kw) return connected(function) else: signal_registry.append((signal, function, args, kw)) return function