Code

Ticket #8077: signals_patch3.diff

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

A new patch against trunk (8964) using the send_signals=False method.

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 8964)
4+++ django/db/models/base.py    (working copy)
5@@ -192,7 +192,8 @@
6     __metaclass__ = ModelBase
7 
8     def __init__(self, *args, **kwargs):
9-        signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs)
10+        if send_signals:
11+            signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs)
12 
13         # There is a rather weird disparity here; if kwargs, it's set, then args
14         # overrides it. It should be one or the other; don't duplicate the work
15@@ -263,7 +264,8 @@
16                     pass
17             if kwargs:
18                 raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
19-        signals.post_init.send(sender=self.__class__, instance=self)
20+        if send_signals:
21+            signals.post_init.send(sender=self.__class__, instance=self)
22 
23     def __repr__(self):
24         return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
25@@ -292,7 +294,7 @@
26 
27     pk = property(_get_pk_val, _set_pk_val)
28 
29-    def save(self, force_insert=False, force_update=False):
30+    def save(self, send_signals=True, force_insert=False, force_update=False):
31         """
32         Saves the current instance. Override this in a subclass if you want to
33         control the saving process.
34@@ -304,11 +306,11 @@
35         if force_insert and force_update:
36             raise ValueError("Cannot force both insert and updating in "
37                     "model saving.")
38-        self.save_base(force_insert=force_insert, force_update=force_update)
39+        self.save_base(send_signals=send_signals, force_insert=force_insert, force_update=force_update)
40 
41     save.alters_data = True
42 
43-    def save_base(self, raw=False, cls=None, force_insert=False,
44+    def save_base(self, send_signals=True, raw=False, cls=None, force_insert=False,
45             force_update=False):
46         """
47         Does the heavy-lifting involved in saving. Subclasses shouldn't need to
48@@ -321,7 +323,8 @@
49             cls = self.__class__
50             meta = self._meta
51             signal = True
52-            signals.pre_save.send(sender=self.__class__, instance=self, raw=raw)
53+            if send_signals:
54+                signals.pre_save.send(sender=self.__class__, instance=self, raw=raw)
55         else:
56             meta = cls._meta
57             signal = False
58@@ -385,7 +388,7 @@
59                 setattr(self, meta.pk.attname, result)
60         transaction.commit_unless_managed()
61 
62-        if signal:
63+        if signal and send_signals:
64             signals.post_save.send(sender=self.__class__, instance=self,
65                 created=(not record_exists), raw=raw)
66