Connect receiver to sender for signal. Arguments: receiver A function or an instance method which is to receive signals. Receivers must be hashable objects. Receivers can be asynchronous. If weak is True,
(self, receiver, sender=None, weak=True, dispatch_uid=None)
| 100 | self._dead_receivers = False |
| 101 | |
| 102 | def connect(self, receiver, sender=None, weak=True, dispatch_uid=None): |
| 103 | """ |
| 104 | Connect receiver to sender for signal. |
| 105 | |
| 106 | Arguments: |
| 107 | |
| 108 | receiver |
| 109 | A function or an instance method which is to receive signals. |
| 110 | Receivers must be hashable objects. Receivers can be |
| 111 | asynchronous. |
| 112 | |
| 113 | If weak is True, then receiver must be weak referenceable. |
| 114 | |
| 115 | Receivers must be able to accept keyword arguments. |
| 116 | |
| 117 | If a receiver is connected with a dispatch_uid argument, it |
| 118 | will not be added if another receiver was already connected |
| 119 | with that dispatch_uid. |
| 120 | |
| 121 | sender |
| 122 | The sender to which the receiver should respond. Must either be |
| 123 | a Python object, or None to receive events from any sender. |
| 124 | |
| 125 | weak |
| 126 | Whether to use weak references to the receiver. By default, the |
| 127 | module will attempt to use weak references to the receiver |
| 128 | objects. If this parameter is false, then strong references |
| 129 | will be used. |
| 130 | |
| 131 | dispatch_uid |
| 132 | An identifier used to uniquely identify a particular instance |
| 133 | of a receiver. This will usually be a string, though it may be |
| 134 | anything hashable. |
| 135 | """ |
| 136 | from django.conf import settings |
| 137 | |
| 138 | # If DEBUG is on, check that we got a good receiver |
| 139 | if settings.configured and settings.DEBUG: |
| 140 | if not callable(receiver): |
| 141 | raise TypeError("Signal receivers must be callable.") |
| 142 | # Check for **kwargs |
| 143 | if not func_accepts_kwargs(receiver): |
| 144 | raise ValueError( |
| 145 | "Signal receivers must accept keyword arguments (**kwargs)." |
| 146 | ) |
| 147 | |
| 148 | if dispatch_uid: |
| 149 | lookup_key = (dispatch_uid, _make_id(sender)) |
| 150 | else: |
| 151 | lookup_key = (_make_id(receiver), _make_id(sender)) |
| 152 | |
| 153 | is_async = iscoroutinefunction(receiver) |
| 154 | |
| 155 | if weak: |
| 156 | ref = weakref.ref |
| 157 | # Check for bound methods |
| 158 | if hasattr(receiver, "__self__") and hasattr(receiver, "__func__"): |
| 159 | ref = weakref.WeakMethod |