Ticket #8077: send_signals_false_patch_correct.diff

File send_signals_false_patch_correct.diff, 3.3 KB (added by brooks.travis@…, 16 years ago)

Patch against trunk using the send_signals=False method.

  • django/db/models/base.py

     
    167167class Model(object):
    168168    __metaclass__ = ModelBase
    169169
    170     def __init__(self, *args, **kwargs):
    171         dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
     170    def __init__(self, send_signals=True, *args, **kwargs):
     171       
     172        #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
     173        if send_signals:
     174            dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
    172175
    173176        # There is a rather weird disparity here; if kwargs, it's set, then args
    174177        # overrides it. It should be one or the other; don't duplicate the work
     
    239242                    pass
    240243            if kwargs:
    241244                raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
    242         dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
     245        #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
     246        if send_signals:
     247            dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
    243248
    244249    def __repr__(self):
    245250        return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
     
    268273
    269274    pk = property(_get_pk_val, _set_pk_val)
    270275
    271     def save(self):
     276    def save(self, send_signals=True):
    272277        """
    273278        Saves the current instance. Override this in a subclass if you want to
    274279        control the saving process.
    275280        """
    276         self.save_base()
     281        #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
     282        if send_signals:
     283            self.save_base()
     284        else:
     285            self.save_base(send_signals=False)
    277286
    278287    save.alters_data = True
    279288
    280     def save_base(self, raw=False, cls=None):
     289    def save_base(self, raw=False, cls=None, send_signals=True):
    281290        """
    282291        Does the heavy-lifting involved in saving. Subclasses shouldn't need to
    283292        override this method. It's separate from save() in order to hide the
     
    288297            cls = self.__class__
    289298            meta = self._meta
    290299            signal = True
    291             dispatcher.send(signal=signals.pre_save, sender=self.__class__,
    292                     instance=self, raw=raw)
     300                #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
     301            if send_signals:
     302                dispatcher.send(signal=signals.pre_save, sender=self.__class__,
     303                            instance=self, raw=raw)
    293304        else:
    294305            meta = cls._meta
    295306            signal = False
     
    351362        transaction.commit_unless_managed()
    352363
    353364        if signal:
    354             dispatcher.send(signal=signals.post_save, sender=self.__class__,
     365            #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
     366            if send_signals:
     367                    dispatcher.send(signal=signals.post_save, sender=self.__class__,
    355368                    instance=self, created=(not record_exists), raw=raw)
    356369
    357370    save_base.alters_data = True
Back to Top