[PATCH 3/6] signals: Don't call event creation code for fixtures

Stephen Finucane stephen at that.guru
Wed Jan 10 11:05:23 AEDT 2018


If loading fixtures via the 'loaddata' management command, then it is
unlikely that the signal handling code should execute. Disable it.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/signals.py | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/patchwork/signals.py b/patchwork/signals.py
index 208685c0..e31ac2f3 100644
--- a/patchwork/signals.py
+++ b/patchwork/signals.py
@@ -33,9 +33,9 @@ from patchwork.models import SeriesPatch
 
 
 @receiver(pre_save, sender=Patch)
-def patch_change_callback(sender, instance, **kwargs):
+def patch_change_callback(sender, instance, raw, **kwargs):
     # we only want notification of modified patches
-    if instance.pk is None:
+    if raw or instance.pk is None:
         return
 
     if instance.project is None or not instance.project.send_notifications:
@@ -70,7 +70,7 @@ def patch_change_callback(sender, instance, **kwargs):
 
 
 @receiver(post_save, sender=CoverLetter)
-def create_cover_created_event(sender, instance, created, **kwargs):
+def create_cover_created_event(sender, instance, created, raw, **kwargs):
 
     def create_event(cover):
         return Event.objects.create(
@@ -78,14 +78,15 @@ def create_cover_created_event(sender, instance, created, **kwargs):
             cover=cover,
             category=Event.CATEGORY_COVER_CREATED)
 
-    if not created:
+    # don't trigger for items loaded from fixtures or new items
+    if raw or not created:
         return
 
     create_event(instance)
 
 
 @receiver(post_save, sender=Patch)
-def create_patch_created_event(sender, instance, created, **kwargs):
+def create_patch_created_event(sender, instance, created, raw, **kwargs):
 
     def create_event(patch):
         return Event.objects.create(
@@ -93,14 +94,15 @@ def create_patch_created_event(sender, instance, created, **kwargs):
             patch=patch,
             category=Event.CATEGORY_PATCH_CREATED)
 
-    if not created:
+    # don't trigger for items loaded from fixtures or new items
+    if raw or not created:
         return
 
     create_event(instance)
 
 
 @receiver(pre_save, sender=Patch)
-def create_patch_state_changed_event(sender, instance, **kwargs):
+def create_patch_state_changed_event(sender, instance, raw, **kwargs):
 
     def create_event(patch, before, after):
         return Event.objects.create(
@@ -110,8 +112,8 @@ def create_patch_state_changed_event(sender, instance, **kwargs):
             previous_state=before,
             current_state=after)
 
-    # only trigger on updated items
-    if not instance.pk:
+    # don't trigger for items loaded from fixtures or new items
+    if raw or not instance.pk:
         return
 
     orig_patch = Patch.objects.get(pk=instance.pk)
@@ -123,7 +125,7 @@ def create_patch_state_changed_event(sender, instance, **kwargs):
 
 
 @receiver(pre_save, sender=Patch)
-def create_patch_delegated_event(sender, instance, **kwargs):
+def create_patch_delegated_event(sender, instance, raw, **kwargs):
 
     def create_event(patch, before, after):
         return Event.objects.create(
@@ -133,8 +135,8 @@ def create_patch_delegated_event(sender, instance, **kwargs):
             previous_delegate=before,
             current_delegate=after)
 
-    # only trigger on updated items
-    if not instance.pk:
+    # don't trigger for items loaded from fixtures or new items
+    if raw or not instance.pk:
         return
 
     orig_patch = Patch.objects.get(pk=instance.pk)
@@ -146,7 +148,7 @@ def create_patch_delegated_event(sender, instance, **kwargs):
 
 
 @receiver(post_save, sender=SeriesPatch)
-def create_patch_completed_event(sender, instance, created, **kwargs):
+def create_patch_completed_event(sender, instance, created, raw, **kwargs):
     """Create patch completed event for patches with series."""
 
     def create_event(patch, series):
@@ -156,7 +158,8 @@ def create_patch_completed_event(sender, instance, created, **kwargs):
             series=series,
             category=Event.CATEGORY_PATCH_COMPLETED)
 
-    if not created:
+    # don't trigger for items loaded from fixtures or existing items
+    if raw or not created:
         return
 
     # if dependencies not met, don't raise event. There's also no point raising
@@ -181,7 +184,7 @@ def create_patch_completed_event(sender, instance, created, **kwargs):
 
 
 @receiver(post_save, sender=Check)
-def create_check_created_event(sender, instance, created, **kwargs):
+def create_check_created_event(sender, instance, created, raw, **kwargs):
 
     def create_event(check):
         # TODO(stephenfin): It might make sense to add a 'project' field to
@@ -192,14 +195,15 @@ def create_check_created_event(sender, instance, created, **kwargs):
             created_check=check,
             category=Event.CATEGORY_CHECK_CREATED)
 
-    if not created:
+    # don't trigger for items loaded from fixtures or existing items
+    if raw or not created:
         return
 
     create_event(instance)
 
 
 @receiver(post_save, sender=Series)
-def create_series_created_event(sender, instance, created, **kwargs):
+def create_series_created_event(sender, instance, created, raw, **kwargs):
 
     def create_event(series):
         return Event.objects.create(
@@ -207,14 +211,15 @@ def create_series_created_event(sender, instance, created, **kwargs):
             series=series,
             category=Event.CATEGORY_SERIES_CREATED)
 
-    if not created:
+    # don't trigger for items loaded from fixtures or existing items
+    if raw or not created:
         return
 
     create_event(instance)
 
 
 @receiver(post_save, sender=SeriesPatch)
-def create_series_completed_event(sender, instance, created, **kwargs):
+def create_series_completed_event(sender, instance, created, raw, **kwargs):
 
     # NOTE(stephenfin): We subscribe to the SeriesPatch.post_save signal
     # instead of Series.m2m_changed to minimize the amount of times this is
@@ -234,7 +239,8 @@ def create_series_completed_event(sender, instance, created, **kwargs):
             series=series,
             category=Event.CATEGORY_SERIES_COMPLETED)
 
-    if not created:
+    # don't trigger for items loaded from fixtures or existing items
+    if raw or not created:
         return
 
     if instance.series.received_all:
-- 
2.14.3



More information about the Patchwork mailing list