| 1 | """ |
| 2 | Testing signals before/after saving and deleting. |
| 3 | """ |
| 4 | |
| 5 | from django.db import models |
| 6 | from django.dispatch import dispatcher |
| 7 | |
| 8 | class Person(models.Model): |
| 9 | first_name = models.CharField(max_length=20) |
| 10 | last_name = models.CharField(max_length=20) |
| 11 | |
| 12 | def __unicode__(self): |
| 13 | return u"%s %s" % (self.first_name, self.last_name) |
| 14 | |
| 15 | |
| 16 | def pre_save_test(sender, instance, **kwargs): |
| 17 | print 'pre_save signal,', instance |
| 18 | |
| 19 | def post_save_test(sender, instance, **kwargs): |
| 20 | print 'post_save signal,', instance |
| 21 | if 'created' in kwargs: |
| 22 | if kwargs['created']: |
| 23 | print 'Is created' |
| 24 | else: |
| 25 | print 'Is updated' |
| 26 | |
| 27 | def pre_delete_test(sender, instance, **kwargs): |
| 28 | print 'pre_delete signal,', instance |
| 29 | print 'instance.id is not None: %s' % (instance.id != None) |
| 30 | |
| 31 | def post_delete_test(sender, instance, **kwargs): |
| 32 | print 'post_delete signal,', instance |
| 33 | print 'instance.id is None: %s' % (instance.id == None) |
| 34 | |
| 35 | __test__ = {'API_TESTS':""" |
| 36 | |
| 37 | >>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save) |
| 38 | >>> dispatcher.connect(post_save_test, signal=models.signals.post_save) |
| 39 | >>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete) |
| 40 | >>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete) |
| 41 | >>> p1 = Person(first_name='John', last_name='Smith') |
| 42 | >>> p1.save() |
| 43 | pre_save signal, John Smith |
| 44 | post_save signal, John Smith |
| 45 | Is created |
| 46 | |
| 47 | >>> p1.first_name = 'Tom' |
| 48 | >>> p1.save() |
| 49 | pre_save signal, Tom Smith |
| 50 | post_save signal, Tom Smith |
| 51 | Is updated |
| 52 | |
| 53 | >>> p1.delete() |
| 54 | pre_delete signal, Tom Smith |
| 55 | instance.id is not None: True |
| 56 | post_delete signal, Tom Smith |
| 57 | instance.id is None: True |
| 58 | |
| 59 | >>> p2 = Person(first_name='James', last_name='Jones') |
| 60 | >>> p2.id = 99999 |
| 61 | >>> p2.save() |
| 62 | pre_save signal, James Jones |
| 63 | post_save signal, James Jones |
| 64 | Is created |
| 65 | |
| 66 | >>> p2.id = 99998 |
| 67 | >>> p2.save() |
| 68 | pre_save signal, James Jones |
| 69 | post_save signal, James Jones |
| 70 | Is created |
| 71 | |
| 72 | >>> p2.delete() |
| 73 | pre_delete signal, James Jones |
| 74 | instance.id is not None: True |
| 75 | post_delete signal, James Jones |
| 76 | instance.id is None: True |
| 77 | |
| 78 | >>> Person.objects.all() |
| 79 | [<Person: James Jones>] |
| 80 | |
| 81 | >>> dispatcher.disconnect(post_delete_test, signal=models.signals.post_delete) |
| 82 | >>> dispatcher.disconnect(pre_delete_test, signal=models.signals.pre_delete) |
| 83 | >>> dispatcher.disconnect(post_save_test, signal=models.signals.post_save) |
| 84 | >>> dispatcher.disconnect(pre_save_test, signal=models.signals.pre_save) |
| 85 | """} |