2

in a Django shop application there gets registered signal handler to some action like adding an item to the cart.

I'd like to replace this handler with my own version in localsite/models.py, ie. without touching original sources.

If just calling connect method

signals.satchmo_cart_add_verify.connect(my_veto_out_of_stock)

the custom handler appends to the list of current recievers and the original still gets an action:

print signals.satchmo_cart_add_verify.receivers
"""
[((140073113515864, 140073319632416), <weakref at 0x7f65502c1aa0;
to 'function' at 0x7f65502c7758 (veto_out_of_stock)>),
((140073114981632, 140073319632416), <weakref at 0x7f65504295d0;
to 'function' at 0x7f655042d500 (my_veto_out_of_stock)>)]
"""

I can in an advance remove the original handlers with

for hnd in signals.satchmo_cart_add_verify.receivers:
    del hnd

but find it ugly and hackish.

So what's the proper way to replace the signal handler ?

Thanks

David Unric
  • 7,421
  • 1
  • 37
  • 65
  • 1
    I've just found one way. Importing the original handler function and passing it to `signals..disconnect` but is there some more generic way even when importing the original function is not possible ? – David Unric Mar 25 '13 at 15:09
  • well, just typing the link... – okm Mar 25 '13 at 15:11
  • If you cannot import the original function (because you don't know where it is when programming?), you could get the receiver func by something like `signals.satchmo_cart_add_verify.receivers[0][1]()` – okm Mar 25 '13 at 15:17

1 Answers1

2

Have your tried Signal.disconnect?

signals.satchmo_cart_add_verify.disconnect(
    signals.satchmo_cart_add_verify.receivers[0][1]())

This way is clear IMO.

okm
  • 23,575
  • 5
  • 83
  • 90