2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2009-05-20 16:13:55 +00:00
|
|
|
from django.db.models import signals
|
2010-10-10 18:23:39 +00:00
|
|
|
from django.dispatch import receiver
|
2009-05-20 16:13:55 +00:00
|
|
|
from django.test import TestCase
|
2012-07-20 12:48:51 +00:00
|
|
|
from django.utils import six
|
2010-10-10 18:23:39 +00:00
|
|
|
|
2013-11-18 17:27:09 +00:00
|
|
|
from .models import Author, Book, Car, Person
|
2009-05-20 16:13:55 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2009-05-20 16:13:55 +00:00
|
|
|
class SignalTests(TestCase):
|
2013-11-18 17:27:09 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2010-10-10 18:23:39 +00: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-18 17:27:09 +00:00
|
|
|
self.pre_signals = (
|
2010-10-10 18:23:39 +00:00
|
|
|
len(signals.pre_save.receivers),
|
|
|
|
len(signals.post_save.receivers),
|
|
|
|
len(signals.pre_delete.receivers),
|
|
|
|
len(signals.post_delete.receivers),
|
|
|
|
)
|
|
|
|
|
2013-11-18 17:27:09 +00:00
|
|
|
def tearDown(self):
|
|
|
|
# Check that all our signals got disconnected properly.
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_save_signals(self):
|
2010-10-10 18:23:39 +00:00
|
|
|
data = []
|
|
|
|
|
2013-11-18 17:27:09 +00:00
|
|
|
def pre_save_handler(signal, sender, instance, **kwargs):
|
2010-10-10 18:23:39 +00:00
|
|
|
data.append(
|
|
|
|
(instance, kwargs.get("raw", False))
|
|
|
|
)
|
|
|
|
|
2013-11-18 17:27:09 +00:00
|
|
|
def post_save_handler(signal, sender, instance, **kwargs):
|
2010-10-10 18:23:39 +00:00
|
|
|
data.append(
|
|
|
|
(instance, kwargs.get("created"), kwargs.get("raw", False))
|
|
|
|
)
|
|
|
|
|
2013-11-18 18:59:39 +00:00
|
|
|
signals.pre_save.connect(pre_save_handler, weak=False)
|
|
|
|
signals.post_save.connect(post_save_handler, weak=False)
|
2013-11-18 17:27:09 +00:00
|
|
|
try:
|
|
|
|
p1 = Person.objects.create(first_name="John", last_name="Smith")
|
|
|
|
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p1, False),
|
|
|
|
(p1, True, False),
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
p1.first_name = "Tom"
|
|
|
|
p1.save()
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p1, False),
|
|
|
|
(p1, False, False),
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
# Calling an internal method purely so that we can trigger a "raw" save.
|
|
|
|
p1.save_base(raw=True)
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p1, True),
|
|
|
|
(p1, False, True),
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
p2 = Person(first_name="James", last_name="Jones")
|
|
|
|
p2.id = 99999
|
|
|
|
p2.save()
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p2, False),
|
|
|
|
(p2, True, False),
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
p2.id = 99998
|
|
|
|
p2.save()
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p2, False),
|
|
|
|
(p2, True, False),
|
|
|
|
])
|
|
|
|
finally:
|
2013-11-18 18:59:39 +00:00
|
|
|
signals.pre_save.disconnect(pre_save_handler)
|
|
|
|
signals.post_save.disconnect(post_save_handler)
|
2013-11-18 17:27:09 +00:00
|
|
|
|
|
|
|
def test_delete_signals(self):
|
|
|
|
data = []
|
|
|
|
|
|
|
|
def pre_delete_handler(signal, sender, instance, **kwargs):
|
2010-10-10 18:23:39 +00:00
|
|
|
data.append(
|
|
|
|
(instance, instance.id is None)
|
|
|
|
)
|
|
|
|
|
2013-11-18 17:27:09 +00: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(
|
|
|
|
(instance, instance.id is None)
|
|
|
|
)
|
|
|
|
post_delete_handler = PostDeleteHandler(data)
|
|
|
|
|
2013-11-18 18:59:39 +00:00
|
|
|
signals.pre_delete.connect(pre_delete_handler, weak=False)
|
|
|
|
signals.post_delete.connect(post_delete_handler, weak=False)
|
2013-11-18 17:27:09 +00:00
|
|
|
try:
|
|
|
|
p1 = Person.objects.create(first_name="John", last_name="Smith")
|
|
|
|
p1.delete()
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p1, False),
|
|
|
|
(p1, False),
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
p2 = Person(first_name="James", last_name="Jones")
|
|
|
|
p2.id = 99999
|
|
|
|
p2.save()
|
|
|
|
p2.id = 99998
|
|
|
|
p2.save()
|
|
|
|
p2.delete()
|
|
|
|
self.assertEqual(data, [
|
|
|
|
(p2, False),
|
|
|
|
(p2, False)
|
|
|
|
])
|
|
|
|
data[:] = []
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.all(), [
|
|
|
|
"James Jones",
|
|
|
|
],
|
|
|
|
six.text_type
|
|
|
|
)
|
|
|
|
finally:
|
2013-11-18 18:59:39 +00:00
|
|
|
signals.pre_delete.disconnect(pre_delete_handler)
|
|
|
|
signals.post_delete.disconnect(post_delete_handler)
|
2013-11-18 17:27:09 +00:00
|
|
|
|
|
|
|
def test_decorators(self):
|
|
|
|
data = []
|
2010-10-10 18:23:39 +00:00
|
|
|
|
2013-11-18 18:59:39 +00:00
|
|
|
@receiver(signals.pre_save, weak=False)
|
2013-11-18 17:27:09 +00:00
|
|
|
def decorated_handler(signal, sender, instance, **kwargs):
|
2010-10-10 18:23:39 +00:00
|
|
|
data.append(instance)
|
|
|
|
|
2013-11-18 18:59:39 +00:00
|
|
|
@receiver(signals.pre_save, sender=Car, weak=False)
|
2013-11-18 17:27:09 +00:00
|
|
|
def decorated_handler_with_sender_arg(signal, sender, instance, **kwargs):
|
2010-10-10 18:23:39 +00:00
|
|
|
data.append(instance)
|
|
|
|
|
2013-11-18 17:27:09 +00:00
|
|
|
try:
|
|
|
|
c1 = Car.objects.create(make="Volkswagon", model="Passat")
|
|
|
|
self.assertEqual(data, [c1, c1])
|
|
|
|
finally:
|
2013-11-18 18:59:39 +00:00
|
|
|
signals.pre_save.disconnect(decorated_handler)
|
|
|
|
signals.pre_save.disconnect(decorated_handler_with_sender_arg, sender=Car)
|
2010-10-10 18:23:39 +00:00
|
|
|
|
2013-11-18 17:27:09 +00:00
|
|
|
def test_save_and_delete_signals_with_m2m(self):
|
|
|
|
data = []
|
2010-10-10 18:23:39 +00:00
|
|
|
|
2013-11-18 17:27:09 +00: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-18 18:59:39 +00:00
|
|
|
data.append('pre_delete signal, %s' % instance)
|
2013-11-18 17:27:09 +00: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-18 18:59:39 +00: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-18 17:27:09 +00: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.
|
|
|
|
b1.authors = [a1]
|
|
|
|
self.assertEqual(data, [])
|
|
|
|
b1.authors = []
|
|
|
|
self.assertEqual(data, [])
|
|
|
|
finally:
|
2013-11-18 18:59:39 +00: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-10 18:23:39 +00:00
|
|
|
|
2009-05-20 16:13:55 +00:00
|
|
|
def test_disconnect_in_dispatch(self):
|
|
|
|
"""
|
|
|
|
Test that signals that disconnect when being called don't mess future
|
|
|
|
dispatching.
|
|
|
|
"""
|
2013-11-18 17:27:09 +00: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-18 18:59:39 +00:00
|
|
|
signals.post_save.connect(a, sender=Person, weak=False)
|
|
|
|
signals.post_save.connect(b, sender=Person, weak=False)
|
2013-10-19 12:31:38 +00:00
|
|
|
Person.objects.create(first_name='John', last_name='Smith')
|
2010-10-10 18:23:39 +00:00
|
|
|
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertTrue(a._run)
|
|
|
|
self.assertTrue(b._run)
|
2009-05-20 16:13:55 +00:00
|
|
|
self.assertEqual(signals.post_save.receivers, [])
|