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 /patchwork/signals.py | |
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>
Diffstat (limited to 'patchwork/signals.py')
-rw-r--r-- | patchwork/signals.py | 63 |
1 files changed, 63 insertions, 0 deletions
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() |