Ticket #4879: post_save_with_created_kw_r6184.patch

File post_save_with_created_kw_r6184.patch, 3.8 KB (added by George Vilches <gav@…>, 17 years ago)

Post save patch + unit tests against r6184

  • django/db/models/base.py

     
    245245                placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \
    246246                    (qn(self._meta.db_table), qn(self._meta.order_with_respect_to.column)))
    247247                db_values.append(getattr(self, self._meta.order_with_respect_to.attname))
     248            record_exists = False
    248249            if db_values:
    249250                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
    250251                    (qn(self._meta.db_table), ','.join(field_names),
     
    259260        transaction.commit_unless_managed()
    260261
    261262        # Run any post-save hooks.
    262         dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self)
     263        dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self, created=(not record_exists))
    263264
    264265    save.alters_data = True
    265266
  • tests/modeltests/signals/models.py

     
     1"""
     2Testing signals before/after saving and deleting.
     3"""
     4
     5from django.db import models
     6from django.dispatch import dispatcher
     7
     8class 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
     16def pre_save_test(sender, instance, **kwargs):
     17    print 'pre_save signal,', instance
     18
     19def 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
     27def pre_delete_test(sender, instance, **kwargs):
     28    print 'pre_delete signal,', instance
     29    print 'instance.id is not None: %s' % (instance.id != None) 
     30
     31def 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()
     43pre_save signal, John Smith
     44post_save signal, John Smith
     45Is created
     46
     47>>> p1.first_name = 'Tom'
     48>>> p1.save()
     49pre_save signal, Tom Smith
     50post_save signal, Tom Smith
     51Is updated
     52
     53>>> p1.delete()
     54pre_delete signal, Tom Smith
     55instance.id is not None: True
     56post_delete signal, Tom Smith
     57instance.id is None: True
     58
     59>>> p2 = Person(first_name='James', last_name='Jones')
     60>>> p2.id = 99999
     61>>> p2.save()
     62pre_save signal, James Jones
     63post_save signal, James Jones
     64Is created
     65
     66>>> p2.id = 99998
     67>>> p2.save()
     68pre_save signal, James Jones
     69post_save signal, James Jones
     70Is created
     71
     72>>> p2.delete()
     73pre_delete signal, James Jones
     74instance.id is not None: True
     75post_delete signal, James Jones
     76instance.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"""}
Back to Top