diff options
Diffstat (limited to 'tests/modeltests/signals/models.py')
-rw-r--r-- | tests/modeltests/signals/models.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/modeltests/signals/models.py b/tests/modeltests/signals/models.py index ea8137f657..8a500752be 100644 --- a/tests/modeltests/signals/models.py +++ b/tests/modeltests/signals/models.py @@ -3,6 +3,7 @@ Testing signals before/after saving and deleting. """ from django.db import models +from django.dispatch import receiver class Person(models.Model): first_name = models.CharField(max_length=20) @@ -11,6 +12,13 @@ class Person(models.Model): def __unicode__(self): return u"%s %s" % (self.first_name, self.last_name) +class Car(models.Model): + make = models.CharField(max_length=20) + model = models.CharField(max_length=20) + + def __unicode__(self): + return u"%s %s" % (self.make, self.model) + def pre_save_test(signal, sender, instance, **kwargs): print 'pre_save signal,', instance if kwargs.get('raw'): @@ -52,22 +60,44 @@ __test__ = {'API_TESTS':""" >>> models.signals.pre_delete.connect(pre_delete_test) >>> models.signals.post_delete.connect(post_delete_test) +# throw a decorator syntax receiver into the mix +>>> @receiver(models.signals.pre_save) +... def pre_save_decorator_test(signal, sender, instance, **kwargs): +... print "pre_save signal decorator,", instance + +# throw a decorator syntax receiver into the mix +>>> @receiver(models.signals.pre_save, sender=Car) +... def pre_save_decorator_sender_test(signal, sender, instance, **kwargs): +... print "pre_save signal decorator sender,", instance + >>> p1 = Person(first_name='John', last_name='Smith') >>> p1.save() pre_save signal, John Smith +pre_save signal decorator, John Smith post_save signal, John Smith Is created >>> p1.first_name = 'Tom' >>> p1.save() pre_save signal, Tom Smith +pre_save signal decorator, Tom Smith post_save signal, Tom Smith Is updated +# Car signal (sender defined) +>>> c1 = Car(make="Volkswagon", model="Passat") +>>> c1.save() +pre_save signal, Volkswagon Passat +pre_save signal decorator, Volkswagon Passat +pre_save signal decorator sender, Volkswagon Passat +post_save signal, Volkswagon Passat +Is created + # Calling an internal method purely so that we can trigger a "raw" save. >>> p1.save_base(raw=True) pre_save signal, Tom Smith Is raw +pre_save signal decorator, Tom Smith post_save signal, Tom Smith Is updated Is raw @@ -82,12 +112,14 @@ instance.id is None: False >>> p2.id = 99999 >>> p2.save() pre_save signal, James Jones +pre_save signal decorator, James Jones post_save signal, James Jones Is created >>> p2.id = 99998 >>> p2.save() pre_save signal, James Jones +pre_save signal decorator, James Jones post_save signal, James Jones Is created @@ -104,6 +136,8 @@ instance.id is None: False >>> models.signals.pre_delete.disconnect(pre_delete_test) >>> models.signals.post_save.disconnect(post_save_test) >>> models.signals.pre_save.disconnect(pre_save_test) +>>> models.signals.pre_save.disconnect(pre_save_decorator_test) +>>> models.signals.pre_save.disconnect(pre_save_decorator_sender_test, sender=Car) # Check that all our signals got disconnected properly. >>> post_signals = (len(models.signals.pre_save.receivers), |