From 3a84d4a4028a10f4962fde85bddacf037d90aa89 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Fri, 15 Sep 2017 19:38:57 +0200
Subject: [PATCH] Upgrade production notifications

---
 production/apps.py    |  5 +++--
 production/signals.py | 19 +++++++++++++++++--
 production/views.py   |  4 ++--
 submissions/views.py  |  1 -
 4 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/production/apps.py b/production/apps.py
index 8c17f2a4c..d633dcd69 100644
--- a/production/apps.py
+++ b/production/apps.py
@@ -8,6 +8,7 @@ class ProductionConfig(AppConfig):
     def ready(self):
         super().ready()
 
-        from .models import ProductionEvent
-        from .signals import notify_new_event
+        from .models import ProductionEvent, ProductionStream
+        from .signals import notify_new_event, notify_new_stream
         post_save.connect(notify_new_event, sender=ProductionEvent)
+        post_save.connect(notify_new_stream, sender=ProductionStream)
diff --git a/production/signals.py b/production/signals.py
index 7cdd7872c..f4a03e3d6 100644
--- a/production/signals.py
+++ b/production/signals.py
@@ -1,9 +1,24 @@
+from django.contrib.auth.models import Group
+
 from notifications.signals import notify
 
 
-def notify_new_stream(sender, instance, recipient, **kwargs):
+def notify_new_stream(sender, instance, created, **kwargs):
+    """
+    Notify the production supervisors about a new Production Stream that is created.
+    """
+    if created:
+        editorial_college = Group.objects.get(name='Editorial College')
+        supervisors = Group.objects.get(name='Production Supervisor')
+        for recipient in supervisors.user_set.all():
+            notify.send(sender=sender, recipient=recipient, actor=editorial_college,
+                        verb=' accepted a Submission. A new Production Stream has started.',
+                        target=instance)
+
+
+def notify_new_stream_assignment(sender, instance, recipient, **kwargs):
     """
-    Notify the production team about a new Production Stream created.
+    Notify a production officer about its new Production Stream assignment.
     """
     notify.send(sender=sender, recipient=recipient, actor=sender,
                 verb=' assigned you to a Production Stream.', target=instance)
diff --git a/production/views.py b/production/views.py
index 4dddd7984..d83931639 100644
--- a/production/views.py
+++ b/production/views.py
@@ -14,7 +14,7 @@ from .constants import PRODUCTION_STREAM_COMPLETED
 from .models import ProductionUser, ProductionStream, ProductionEvent
 from .forms import ProductionEventForm, AssignOfficerForm, UserToOfficerForm
 from .permissions import is_production_user
-from .signals import notify_stream_completed, notify_new_stream
+from .signals import notify_stream_completed, notify_new_stream_assignment
 
 
 ######################
@@ -103,7 +103,7 @@ def add_officer(request, stream_id):
         form.save()
         officer = form.cleaned_data.get('officer')
         messages.success(request, 'Officer {officer} has been assigned.'.format(officer=officer))
-        notify_new_stream(request.user, stream, officer.user)
+        notify_new_stream_assignment(request.user, stream, officer.user)
     else:
         for key, error in form.errors.items():
             messages.warning(request, error[0])
diff --git a/submissions/views.py b/submissions/views.py
index b1418fecc..9ec89dceb 100644
--- a/submissions/views.py
+++ b/submissions/views.py
@@ -38,7 +38,6 @@ from mails.views import MailEditingSubView
 from scipost.forms import ModifyPersonalMessageForm, RemarkForm
 from scipost.models import Contributor, Remark, RegistrationInvitation
 from scipost.utils import Utils
-
 from comments.forms import CommentForm
 from production.models import ProductionStream
 
-- 
GitLab