]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
models: Move signal to 'signals'
authorStephen Finucane <stephen@that.guru>
Tue, 18 Oct 2016 20:12:03 +0000 (21:12 +0100)
committerStephen Finucane <stephen@that.guru>
Wed, 1 Mar 2017 22:16:06 +0000 (22:16 +0000)
Additional signals are going to be added shortly and they shouldn't
pollute 'models.py'.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Tested-by: Daniel Axtens <dja@axtens.net>
patchwork/__init__.py
patchwork/apps.py [new file with mode: 0644]
patchwork/models.py
patchwork/signals.py [new file with mode: 0644]

index f82d7111a3aaf8c111703e3a55ee0e67772dd324..6aec7e721b62ad2f87d7919f7159e7481824c7e3 100644 (file)
@@ -22,3 +22,5 @@ from patchwork.version import get_latest_version
 VERSION = (2, 0, 0, 'alpha', 0)
 
 __version__ = get_latest_version(VERSION)
+
+default_app_config = 'patchwork.apps.PatchworkAppConfig'
diff --git a/patchwork/apps.py b/patchwork/apps.py
new file mode 100644 (file)
index 0000000..5bd5f53
--- /dev/null
@@ -0,0 +1,29 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2016 Stephen Finucane <stephen@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
index ae406684bb42948ec4eb897310c45c2cbf5a8611..a7a232e358897c86431171a3562c587cdeca2c51 100644 (file)
@@ -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
@@ -846,40 +847,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 (file)
index 0000000..6f7f5ea
--- /dev/null
@@ -0,0 +1,63 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2016 Stephen Finucane <stephen@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
+
+
+@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()