Skip to content

Commit

Permalink
Fixed #26642 -- Made ModelSignal.disconnect() work with lazy references.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHill authored and timgraham committed May 27, 2016
1 parent 9bb1b4b commit ff6c6fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
28 changes: 21 additions & 7 deletions django/db/models/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import warnings
from functools import partial

from django.db.models.utils import make_model_tuple
from django.dispatch import Signal
from django.utils.deprecation import RemovedInDjango20Warning


class_prepared = Signal(providing_args=["class"])
Expand All @@ -12,14 +14,26 @@ class ModelSignal(Signal):
Signal subclass that allows the sender to be lazily specified as a string
of the `app_label.ModelName` form.
"""
def _lazy_method(self, method, apps, receiver, sender, **kwargs):
# This partial takes a single optional argument named "sender".
partial_method = partial(method, receiver, **kwargs)
# import models here to avoid a circular import
from django.db import models
if isinstance(sender, models.Model) or sender is None:
# Skip lazy_model_operation to get a return value for disconnect()
return partial_method(sender)
apps = apps or models.base.Options.default_apps
apps.lazy_model_operation(partial_method, make_model_tuple(sender))

def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, apps=None):
# Takes a single optional argument named "sender"
connect = partial(super(ModelSignal, self).connect, receiver, weak=weak, dispatch_uid=dispatch_uid)
models = [make_model_tuple(sender)] if sender else []
if not apps:
from django.db.models.base import Options
apps = sender._meta.apps if hasattr(sender, '_meta') else Options.default_apps
apps.lazy_model_operation(connect, *models)
self._lazy_method(super(ModelSignal, self).connect, apps, receiver, sender, dispatch_uid=dispatch_uid)

def disconnect(self, receiver=None, sender=None, weak=None, dispatch_uid=None, apps=None):
if weak is not None:
warnings.warn("Passing `weak` to disconnect has no effect.", RemovedInDjango20Warning, stacklevel=2)
return self._lazy_method(
super(ModelSignal, self).disconnect, apps, receiver, sender, dispatch_uid=dispatch_uid
)


pre_init = ModelSignal(providing_args=["instance", "args", "kwargs"], use_caching=True)
Expand Down
16 changes: 16 additions & 0 deletions tests/signals/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,19 @@ class Created(models.Model):
}])
finally:
signals.post_init.disconnect(self.receiver, sender=Created)

@isolate_apps('signals', kwarg_name='apps')
def test_disconnect(self, apps):
received = []

def receiver(**kwargs):
received.append(kwargs)

signals.post_init.connect(receiver, sender='signals.Created', apps=apps)
signals.post_init.disconnect(receiver, sender='signals.Created', apps=apps)

class Created(models.Model):
pass

Created()
self.assertEqual(received, [])

0 comments on commit ff6c6fe

Please sign in to comment.