2016-06-01 00:17:25 +08:00
|
|
|
from django.apps.registry import Apps
|
2013-11-05 12:11:51 +08:00
|
|
|
from django.db import models
|
2009-05-21 00:13:55 +08:00
|
|
|
from django.db.models import signals
|
2010-10-11 02:23:39 +08:00
|
|
|
from django.dispatch import receiver
|
2016-06-19 08:45:38 +08:00
|
|
|
from django.test import TestCase, mock
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test.utils import isolate_apps
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2010-10-11 02:23:39 +08:00
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
from .models import Author, Book, Car, Person
|
2009-05-21 00:13:55 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
class BaseSignalTest(TestCase):
|
2013-11-19 01:27:09 +08:00
|
|
|
def setUp(self):
|
2010-10-11 02:23:39 +08:00
|
|
|
# Save up the number of connected signals so that we can check at the
|
|
|
|
# end that all the signals we register get properly unregistered (#9989)
|
2013-11-19 01:27:09 +08:00
|
|
|
self.pre_signals = (
|
2010-10-11 02:23:39 +08:00
|
|
|
len(signals.pre_save.receivers),
|
|
|
|
len(signals.post_save.receivers),
|
|
|
|
len(signals.pre_delete.receivers),
|
|
|
|
len(signals.post_delete.receivers),
|
|
|
|
)
|
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
def tearDown(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# All our signals got disconnected properly.
|
2013-11-19 01:27:09 +08:00
|
|
|
post_signals = (
|
|
|
|
len(signals.pre_save.receivers),
|
|
|
|
len(signals.post_save.receivers),
|
|
|
|
len(signals.pre_delete.receivers),
|
|
|
|
len(signals.post_delete.receivers),
|
|
|
|
)
|
|
|
|
self.assertEqual(self.pre_signals, post_signals)
|
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
|
|
|
|
class SignalTests(BaseSignalTest):
|
2014-11-21 19:04:19 +08:00
|
|
|
def test_model_pre_init_and_post_init(self):
|
|
|
|
data = []
|
|
|
|
|
|
|
|
def pre_init_callback(sender, args, **kwargs):
|
|
|
|
data.append(kwargs['kwargs'])
|
|
|
|
signals.pre_init.connect(pre_init_callback)
|
|
|
|
|
|
|
|
def post_init_callback(sender, instance, **kwargs):
|
|
|
|
data.append(instance)
|
|
|
|
signals.post_init.connect(post_init_callback)
|
|
|
|
|
|
|
|
p1 = Person(first_name="John", last_name="Doe")
|
|
|
|
self.assertEqual(data, [{}, p1])
|
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
def test_save_signals(self):
|
2010-10-11 02:23:39 +08:00
|
|
|
data = []
|
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
def pre_save_handler(signal, sender, instance, **kwargs):
|
2010-10-11 02:23:39 +08:00
|
|
|
data.append(
|
2016-09-21 18:32:24 +08:00
|
|
|
(instance, sender, kwargs.get("raw", False))
|
2010-10-11 02:23:39 +08:00
|
|
|
)
|
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
def post_save_handler(signal, sender, instance, **kwargs):
|
2010-10-11 02:23:39 +08:00
|
|
|
data.append(
|
2016-09-21 18:32:24 +08:00
|
|
|
(instance, sender, kwargs.get("created"), kwargs.get("raw", False))
|
2010-10-11 02:23:39 +08:00
|
|
|
)
|
|
|
|
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_save.connect(pre_save_handler, weak=False)
|
|
|
|
signals.post_save.connect(post_save_handler, weak=False)
|
2013-11-19 01:27:09 +08:00
|
|
|
try:
|
|
|
|
p1 = Person.objects.create(first_name="John", last_name="Smith")
|
|
|
|
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p1, Person, False),
|
|
|
|
(p1, Person, True, False),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
p1.first_name = "Tom"
|
|
|
|
p1.save()
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p1, Person, False),
|
|
|
|
(p1, Person, False, False),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
# Calling an internal method purely so that we can trigger a "raw" save.
|
|
|
|
p1.save_base(raw=True)
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p1, Person, True),
|
|
|
|
(p1, Person, False, True),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
p2 = Person(first_name="James", last_name="Jones")
|
|
|
|
p2.id = 99999
|
|
|
|
p2.save()
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p2, Person, False),
|
|
|
|
(p2, Person, True, False),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
p2.id = 99998
|
|
|
|
p2.save()
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p2, Person, False),
|
|
|
|
(p2, Person, True, False),
|
|
|
|
])
|
|
|
|
|
|
|
|
# The sender should stay the same when using defer().
|
|
|
|
data[:] = []
|
|
|
|
p3 = Person.objects.defer('first_name').get(pk=p1.pk)
|
|
|
|
p3.last_name = 'Reese'
|
|
|
|
p3.save()
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p3, Person, False),
|
|
|
|
(p3, Person, False, False),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
finally:
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_save.disconnect(pre_save_handler)
|
|
|
|
signals.post_save.disconnect(post_save_handler)
|
2013-11-19 01:27:09 +08:00
|
|
|
|
|
|
|
def test_delete_signals(self):
|
|
|
|
data = []
|
|
|
|
|
|
|
|
def pre_delete_handler(signal, sender, instance, **kwargs):
|
2010-10-11 02:23:39 +08:00
|
|
|
data.append(
|
2016-09-21 18:32:24 +08:00
|
|
|
(instance, sender, instance.id is None)
|
2010-10-11 02:23:39 +08:00
|
|
|
)
|
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
# #8285: signals can be any callable
|
|
|
|
class PostDeleteHandler(object):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def __call__(self, signal, sender, instance, **kwargs):
|
|
|
|
self.data.append(
|
2016-09-21 18:32:24 +08:00
|
|
|
(instance, sender, instance.id is None)
|
2013-11-19 01:27:09 +08:00
|
|
|
)
|
|
|
|
post_delete_handler = PostDeleteHandler(data)
|
|
|
|
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_delete.connect(pre_delete_handler, weak=False)
|
|
|
|
signals.post_delete.connect(post_delete_handler, weak=False)
|
2013-11-19 01:27:09 +08:00
|
|
|
try:
|
|
|
|
p1 = Person.objects.create(first_name="John", last_name="Smith")
|
|
|
|
p1.delete()
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p1, Person, False),
|
|
|
|
(p1, Person, False),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
p2 = Person(first_name="James", last_name="Jones")
|
|
|
|
p2.id = 99999
|
|
|
|
p2.save()
|
|
|
|
p2.id = 99998
|
|
|
|
p2.save()
|
|
|
|
p2.delete()
|
|
|
|
self.assertEqual(data, [
|
2016-09-21 18:32:24 +08:00
|
|
|
(p2, Person, False),
|
|
|
|
(p2, Person, False),
|
2013-11-19 01:27:09 +08:00
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.all(), [
|
|
|
|
"James Jones",
|
|
|
|
],
|
|
|
|
six.text_type
|
|
|
|
)
|
|
|
|
finally:
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_delete.disconnect(pre_delete_handler)
|
|
|
|
signals.post_delete.disconnect(post_delete_handler)
|
2013-11-19 01:27:09 +08:00
|
|
|
|
|
|
|
def test_decorators(self):
|
|
|
|
data = []
|
2010-10-11 02:23:39 +08:00
|
|
|
|
2013-11-19 02:59:39 +08:00
|
|
|
@receiver(signals.pre_save, weak=False)
|
2013-11-19 01:27:09 +08:00
|
|
|
def decorated_handler(signal, sender, instance, **kwargs):
|
2010-10-11 02:23:39 +08:00
|
|
|
data.append(instance)
|
|
|
|
|
2013-11-19 02:59:39 +08:00
|
|
|
@receiver(signals.pre_save, sender=Car, weak=False)
|
2013-11-19 01:27:09 +08:00
|
|
|
def decorated_handler_with_sender_arg(signal, sender, instance, **kwargs):
|
2010-10-11 02:23:39 +08:00
|
|
|
data.append(instance)
|
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
try:
|
2015-12-03 07:55:50 +08:00
|
|
|
c1 = Car.objects.create(make="Volkswagen", model="Passat")
|
2013-11-19 01:27:09 +08:00
|
|
|
self.assertEqual(data, [c1, c1])
|
|
|
|
finally:
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_save.disconnect(decorated_handler)
|
|
|
|
signals.pre_save.disconnect(decorated_handler_with_sender_arg, sender=Car)
|
2010-10-11 02:23:39 +08:00
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
def test_save_and_delete_signals_with_m2m(self):
|
|
|
|
data = []
|
2010-10-11 02:23:39 +08:00
|
|
|
|
2013-11-19 01:27:09 +08:00
|
|
|
def pre_save_handler(signal, sender, instance, **kwargs):
|
|
|
|
data.append('pre_save signal, %s' % instance)
|
|
|
|
if kwargs.get('raw'):
|
|
|
|
data.append('Is raw')
|
|
|
|
|
|
|
|
def post_save_handler(signal, sender, instance, **kwargs):
|
|
|
|
data.append('post_save signal, %s' % instance)
|
|
|
|
if 'created' in kwargs:
|
|
|
|
if kwargs['created']:
|
|
|
|
data.append('Is created')
|
|
|
|
else:
|
|
|
|
data.append('Is updated')
|
|
|
|
if kwargs.get('raw'):
|
|
|
|
data.append('Is raw')
|
|
|
|
|
|
|
|
def pre_delete_handler(signal, sender, instance, **kwargs):
|
2013-11-19 02:59:39 +08:00
|
|
|
data.append('pre_delete signal, %s' % instance)
|
2013-11-19 01:27:09 +08:00
|
|
|
data.append('instance.id is not None: %s' % (instance.id is not None))
|
|
|
|
|
|
|
|
def post_delete_handler(signal, sender, instance, **kwargs):
|
|
|
|
data.append('post_delete signal, %s' % instance)
|
|
|
|
data.append('instance.id is not None: %s' % (instance.id is not None))
|
|
|
|
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_save.connect(pre_save_handler, weak=False)
|
|
|
|
signals.post_save.connect(post_save_handler, weak=False)
|
|
|
|
signals.pre_delete.connect(pre_delete_handler, weak=False)
|
|
|
|
signals.post_delete.connect(post_delete_handler, weak=False)
|
2013-11-19 01:27:09 +08:00
|
|
|
try:
|
|
|
|
a1 = Author.objects.create(name='Neal Stephenson')
|
|
|
|
self.assertEqual(data, [
|
|
|
|
"pre_save signal, Neal Stephenson",
|
|
|
|
"post_save signal, Neal Stephenson",
|
|
|
|
"Is created"
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
b1 = Book.objects.create(name='Snow Crash')
|
|
|
|
self.assertEqual(data, [
|
|
|
|
"pre_save signal, Snow Crash",
|
|
|
|
"post_save signal, Snow Crash",
|
|
|
|
"Is created"
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
# Assigning and removing to/from m2m shouldn't generate an m2m signal.
|
2015-10-09 05:17:10 +08:00
|
|
|
b1.authors.set([a1])
|
2013-11-19 01:27:09 +08:00
|
|
|
self.assertEqual(data, [])
|
2015-10-09 05:17:10 +08:00
|
|
|
b1.authors.set([])
|
2013-11-19 01:27:09 +08:00
|
|
|
self.assertEqual(data, [])
|
|
|
|
finally:
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.pre_save.disconnect(pre_save_handler)
|
|
|
|
signals.post_save.disconnect(post_save_handler)
|
|
|
|
signals.pre_delete.disconnect(pre_delete_handler)
|
|
|
|
signals.post_delete.disconnect(post_delete_handler)
|
2010-10-11 02:23:39 +08:00
|
|
|
|
2009-05-21 00:13:55 +08:00
|
|
|
def test_disconnect_in_dispatch(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Signals that disconnect when being called don't mess future
|
2009-05-21 00:13:55 +08:00
|
|
|
dispatching.
|
|
|
|
"""
|
2013-11-19 01:27:09 +08:00
|
|
|
|
|
|
|
class Handler(object):
|
|
|
|
def __init__(self, param):
|
|
|
|
self.param = param
|
|
|
|
self._run = False
|
|
|
|
|
|
|
|
def __call__(self, signal, sender, **kwargs):
|
|
|
|
self._run = True
|
|
|
|
signal.disconnect(receiver=self, sender=sender)
|
|
|
|
|
|
|
|
a, b = Handler(1), Handler(2)
|
2013-11-19 02:59:39 +08:00
|
|
|
signals.post_save.connect(a, sender=Person, weak=False)
|
|
|
|
signals.post_save.connect(b, sender=Person, weak=False)
|
2013-10-19 20:31:38 +08:00
|
|
|
Person.objects.create(first_name='John', last_name='Smith')
|
2010-10-11 02:23:39 +08:00
|
|
|
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(a._run)
|
|
|
|
self.assertTrue(b._run)
|
2009-05-21 00:13:55 +08:00
|
|
|
self.assertEqual(signals.post_save.receivers, [])
|
2013-11-05 12:11:51 +08:00
|
|
|
|
2016-06-19 08:45:38 +08:00
|
|
|
@mock.patch('weakref.ref')
|
|
|
|
def test_lazy_model_signal(self, ref):
|
|
|
|
def callback(sender, args, **kwargs):
|
|
|
|
pass
|
|
|
|
signals.pre_init.connect(callback)
|
|
|
|
signals.pre_init.disconnect(callback)
|
|
|
|
self.assertTrue(ref.called)
|
|
|
|
ref.reset_mock()
|
|
|
|
|
|
|
|
signals.pre_init.connect(callback, weak=False)
|
|
|
|
signals.pre_init.disconnect(callback)
|
|
|
|
ref.assert_not_called()
|
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
|
|
|
|
class LazyModelRefTest(BaseSignalTest):
|
|
|
|
def setUp(self):
|
|
|
|
super(LazyModelRefTest, self).setUp()
|
|
|
|
self.received = []
|
|
|
|
|
|
|
|
def receiver(self, **kwargs):
|
|
|
|
self.received.append(kwargs)
|
|
|
|
|
|
|
|
def test_invalid_sender_model_name(self):
|
2016-03-30 16:34:44 +08:00
|
|
|
msg = "Invalid model reference 'invalid'. String model references must be of the form 'app_label.ModelName'."
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2013-11-05 12:11:51 +08:00
|
|
|
signals.post_init.connect(self.receiver, sender='invalid')
|
|
|
|
|
|
|
|
def test_already_loaded_model(self):
|
|
|
|
signals.post_init.connect(
|
|
|
|
self.receiver, sender='signals.Book', weak=False
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
instance = Book()
|
|
|
|
self.assertEqual(self.received, [{
|
|
|
|
'signal': signals.post_init,
|
|
|
|
'sender': Book,
|
|
|
|
'instance': instance
|
|
|
|
}])
|
|
|
|
finally:
|
|
|
|
signals.post_init.disconnect(self.receiver, sender=Book)
|
|
|
|
|
2016-03-30 16:34:44 +08:00
|
|
|
@isolate_apps('signals', kwarg_name='apps')
|
|
|
|
def test_not_loaded_model(self, apps):
|
2013-11-05 12:11:51 +08:00
|
|
|
signals.post_init.connect(
|
2016-03-30 16:34:44 +08:00
|
|
|
self.receiver, sender='signals.Created', weak=False, apps=apps
|
2013-11-05 12:11:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
class Created(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
instance = Created()
|
|
|
|
self.assertEqual(self.received, [{
|
|
|
|
'signal': signals.post_init, 'sender': Created, 'instance': instance
|
|
|
|
}])
|
|
|
|
finally:
|
|
|
|
signals.post_init.disconnect(self.receiver, sender=Created)
|
2016-05-20 16:21:20 +08:00
|
|
|
|
|
|
|
@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, [])
|
2016-06-01 00:17:25 +08:00
|
|
|
|
|
|
|
def test_register_model_class_senders_immediately(self):
|
|
|
|
"""
|
|
|
|
Model signals registered with model classes as senders don't use the
|
|
|
|
Apps.lazy_model_operation() mechanism.
|
|
|
|
"""
|
|
|
|
# Book isn't registered with apps2, so it will linger in
|
|
|
|
# apps2._pending_operations if ModelSignal does the wrong thing.
|
|
|
|
apps2 = Apps()
|
|
|
|
signals.post_init.connect(self.receiver, sender=Book, apps=apps2)
|
|
|
|
self.assertEqual(list(apps2._pending_operations), [])
|