[PATCH v3 2/9] models: Move signal to 'signals'

Stephen Finucane stephen at that.guru
Wed Feb 8 09:12:37 AEDT 2017


Additional signals are going to be added shortly and they shouldn't
pollute 'models.py'.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/__init__.py | 20 ++++++++++++++++
 patchwork/apps.py     | 29 ++++++++++++++++++++++++
 patchwork/models.py   | 41 ++++-----------------------------
 patchwork/signals.py  | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 116 insertions(+), 37 deletions(-)
 create mode 100644 patchwork/apps.py
 create mode 100644 patchwork/signals.py

diff --git a/patchwork/__init__.py b/patchwork/__init__.py
index e69de29..a80f62c 100644
--- a/patchwork/__init__.py
+++ b/patchwork/__init__.py
@@ -0,0 +1,20 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2016 Stephen Finucane <stephen at that.guru>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+default_app_config = 'patchwork.apps.PatchworkAppConfig'
diff --git a/patchwork/apps.py b/patchwork/apps.py
new file mode 100644
index 0000000..5bd5f53
--- /dev/null
+++ b/patchwork/apps.py
@@ -0,0 +1,29 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2016 Stephen Finucane <stephen at that.guru>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+from django.apps import AppConfig
+
+
+class PatchworkAppConfig(AppConfig):
+
+    name = 'patchwork'
+    verbose_name = 'Patchwork'
+
+    def ready(self):
+        import patchwork.signals  # noqa
diff --git a/patchwork/models.py b/patchwork/models.py
index e5d844d..659414a 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -25,6 +25,7 @@ import datetime
 import random
 import re
 
+import django
 from django.contrib.auth.models import User
 from django.conf import settings
 from django.contrib.sites.models import Site
@@ -839,40 +840,6 @@ class PatchChangeNotification(models.Model):
     orig_state = models.ForeignKey(State)
 
 
-def _patch_change_callback(sender, instance, **kwargs):
-    # we only want notification of modified patches
-    if instance.pk is None:
-        return
-
-    if instance.project is None or not instance.project.send_notifications:
-        return
-
-    try:
-        orig_patch = Patch.objects.get(pk=instance.pk)
-    except Patch.DoesNotExist:
-        return
-
-    # If there's no interesting changes, abort without creating the
-    # notification
-    if orig_patch.state == instance.state:
-        return
-
-    notification = None
-    try:
-        notification = PatchChangeNotification.objects.get(patch=instance)
-    except PatchChangeNotification.DoesNotExist:
-        pass
-
-    if notification is None:
-        notification = PatchChangeNotification(patch=instance,
-                                               orig_state=orig_patch.state)
-    elif notification.orig_state == instance.state:
-        # If we're back at the original state, there is no need to notify
-        notification.delete()
-        return
-
-    notification.last_modified = datetime.datetime.now()
-    notification.save()
-
-
-models.signals.pre_save.connect(_patch_change_callback, sender=Patch)
+if django.VERSION < (1, 7):
+    # We don't have support for AppConfig in Django 1.6.x
+    import patchwork.signals  # noqa
diff --git a/patchwork/signals.py b/patchwork/signals.py
new file mode 100644
index 0000000..6f7f5ea
--- /dev/null
+++ b/patchwork/signals.py
@@ -0,0 +1,63 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2016 Stephen Finucane <stephen at that.guru>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+from datetime import datetime as dt
+
+from django.db.models.signals import pre_save
+from django.dispatch import receiver
+
+from patchwork.models import Patch
+from patchwork.models import PatchChangeNotification
+
+
+ at receiver(pre_save, sender=Patch)
+def patch_change_callback(sender, instance, **kwargs):
+    # we only want notification of modified patches
+    if instance.pk is None:
+        return
+
+    if instance.project is None or not instance.project.send_notifications:
+        return
+
+    try:
+        orig_patch = Patch.objects.get(pk=instance.pk)
+    except Patch.DoesNotExist:
+        return
+
+    # If there's no interesting changes, abort without creating the
+    # notification
+    if orig_patch.state == instance.state:
+        return
+
+    notification = None
+    try:
+        notification = PatchChangeNotification.objects.get(patch=instance)
+    except PatchChangeNotification.DoesNotExist:
+        pass
+
+    if notification is None:
+        notification = PatchChangeNotification(patch=instance,
+                                               orig_state=orig_patch.state)
+    elif notification.orig_state == instance.state:
+        # If we're back at the original state, there is no need to notify
+        notification.delete()
+        return
+
+    notification.last_modified = dt.now()
+    notification.save()
-- 
2.9.3



More information about the Patchwork mailing list