Code

Ticket #8077: send_signals_false_patch.diff

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

Patch against trunk using the send_signals=False method.

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