Code

Ticket #8077: send_signals_false_patch_correct.diff

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

Patch against trunk using the send_signals=False method.

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 8214)
4+++ django/db/models/base.py    (working copy)
5@@ -167,8 +167,11 @@
6 class Model(object):
7     __metaclass__ = ModelBase
8 
9-    def __init__(self, *args, **kwargs):
10-        dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
11+    def __init__(self, send_signals=True, *args, **kwargs):
12+       
13+        #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
14+        if send_signals:
15+            dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
16 
17         # There is a rather weird disparity here; if kwargs, it's set, then args
18         # overrides it. It should be one or the other; don't duplicate the work
19@@ -239,7 +242,9 @@
20                     pass
21             if kwargs:
22                 raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
23-        dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
24+        #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
25+        if send_signals:
26+            dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
27 
28     def __repr__(self):
29         return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
30@@ -268,16 +273,20 @@
31 
32     pk = property(_get_pk_val, _set_pk_val)
33 
34-    def save(self):
35+    def save(self, send_signals=True):
36         """
37         Saves the current instance. Override this in a subclass if you want to
38         control the saving process.
39         """
40-        self.save_base()
41+        #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
42+        if send_signals:
43+            self.save_base()
44+        else:
45+            self.save_base(send_signals=False)
46 
47     save.alters_data = True
48 
49-    def save_base(self, raw=False, cls=None):
50+    def save_base(self, raw=False, cls=None, send_signals=True):
51         """
52         Does the heavy-lifting involved in saving. Subclasses shouldn't need to
53         override this method. It's separate from save() in order to hide the
54@@ -288,8 +297,10 @@
55             cls = self.__class__
56             meta = self._meta
57             signal = True
58-            dispatcher.send(signal=signals.pre_save, sender=self.__class__,
59-                    instance=self, raw=raw)
60+               #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
61+            if send_signals:
62+                dispatcher.send(signal=signals.pre_save, sender=self.__class__,
63+                           instance=self, raw=raw)
64         else:
65             meta = cls._meta
66             signal = False
67@@ -351,7 +362,9 @@
68         transaction.commit_unless_managed()
69 
70         if signal:
71-            dispatcher.send(signal=signals.post_save, sender=self.__class__,
72+            #Test to see if signals are disabled for this action (send_signals=False). If not, go about as usual.
73+            if send_signals:
74+                   dispatcher.send(signal=signals.post_save, sender=self.__class__,
75                     instance=self, created=(not record_exists), raw=raw)
76 
77     save_base.alters_data = True