From: Stephen Finucane Date: Tue, 18 Oct 2016 20:12:03 +0000 (+0100) Subject: models: Move signal to 'signals' X-Git-Tag: v2.0.0-rc1~78 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=837c5fd93595ef2d5f142642ea7248e08ab40c26;p=thirdparty%2Fpatchwork.git 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 Tested-by: Daniel Axtens --- diff --git a/patchwork/__init__.py b/patchwork/__init__.py index f82d7111..6aec7e72 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 00000000..5bd5f53a --- /dev/null +++ b/patchwork/apps.py @@ -0,0 +1,29 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2016 Stephen Finucane +# +# 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 ae406684..a7a232e3 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 00000000..6f7f5ea0 --- /dev/null +++ b/patchwork/signals.py @@ -0,0 +1,63 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2016 Stephen Finucane +# +# 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()