diff options
author | Stephen Finucane <stephen@that.guru> | 2016-10-18 21:12:03 +0100 |
---|---|---|
committer | Stephen Finucane <stephen@that.guru> | 2017-03-01 22:16:06 +0000 |
commit | 837c5fd93595ef2d5f142642ea7248e08ab40c26 (patch) | |
tree | 53d7b7ad1ec0047826342e9e13d8fa11f4512851 | |
parent | 66a88a4669bc43447b9bae08606e262cf739c35b (diff) | |
download | patchwork-837c5fd93595ef2d5f142642ea7248e08ab40c26.tar patchwork-837c5fd93595ef2d5f142642ea7248e08ab40c26.tar.gz |
models: Move signal to 'signals'
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>
-rw-r--r-- | patchwork/__init__.py | 2 | ||||
-rw-r--r-- | patchwork/apps.py | 29 | ||||
-rw-r--r-- | patchwork/models.py | 41 | ||||
-rw-r--r-- | patchwork/signals.py | 63 |
4 files changed, 98 insertions, 37 deletions
diff --git a/patchwork/__init__.py b/patchwork/__init__.py index f82d711..6aec7e7 100644 --- a/patchwork/__init__.py +++ b/patchwork/__init__.py @@ -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 index 0000000..5bd5f53 --- /dev/null +++ b/patchwork/apps.py @@ -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 diff --git a/patchwork/models.py b/patchwork/models.py index ae40668..a7a232e 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 @@ -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 index 0000000..6f7f5ea --- /dev/null +++ b/patchwork/signals.py @@ -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() |