From 18df2f2bac8c515632068ec6ec7f4a58482881fe Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sat, 16 Sep 2017 20:58:47 +0200
Subject: [PATCH] Add new notifications (submissions app)

---
 submissions/__init__.py                       |  1 +
 submissions/apps.py                           | 19 ++++++++
 submissions/models.py                         | 13 +++++-
 submissions/signals.py                        | 46 +++++++++++++++++++
 .../submissions/pool/assignment_request.html  |  2 +-
 5 files changed, 78 insertions(+), 3 deletions(-)
 create mode 100644 submissions/apps.py
 create mode 100644 submissions/signals.py

diff --git a/submissions/__init__.py b/submissions/__init__.py
index e69de29bb..1c2ca927d 100644
--- a/submissions/__init__.py
+++ b/submissions/__init__.py
@@ -0,0 +1 @@
+default_app_config = 'submissions.apps.SubmissionsConfig'
diff --git a/submissions/apps.py b/submissions/apps.py
new file mode 100644
index 000000000..51d7411cf
--- /dev/null
+++ b/submissions/apps.py
@@ -0,0 +1,19 @@
+from django.apps import AppConfig
+from django.db.models.signals import post_save
+
+
+class SubmissionsConfig(AppConfig):
+    name = 'submissions'
+
+    def ready(self):
+        super().ready()
+
+        from . import models, signals
+        post_save.connect(signals.notify_new_manuscript_submitted,
+                          sender=models.Submission)
+        post_save.connect(signals.notify_new_editorial_recommendation,
+                          sender=models.EICRecommendation)
+        post_save.connect(signals.notify_new_editorial_assignment,
+                          sender=models.EditorialAssignment)
+        post_save.connect(signals.notify_new_referee_invitation,
+                          sender=models.RefereeInvitation)
diff --git a/submissions/models.py b/submissions/models.py
index c7b1dee29..08983d70a 100644
--- a/submissions/models.py
+++ b/submissions/models.py
@@ -275,6 +275,9 @@ class EditorialAssignment(SubmissionRelatedObjectMixin, models.Model):
                 self.submission.title[:30] + ' by ' + self.submission.author_list[:30] +
                 ', requested on ' + self.date_created.strftime('%Y-%m-%d'))
 
+    def get_absolute_url(self):
+        return reverse('submissions:assignment_request', args=(self.id,))
+
 
 class RefereeInvitation(SubmissionRelatedObjectMixin, models.Model):
     submission = models.ForeignKey('submissions.Submission', on_delete=models.CASCADE,
@@ -503,8 +506,10 @@ class EICRecommendation(SubmissionRelatedObjectMixin, models.Model):
     eligible_to_vote = models.ManyToManyField('scipost.Contributor', blank=True,
                                               related_name='eligible_to_vote')
     voted_for = models.ManyToManyField('scipost.Contributor', blank=True, related_name='voted_for')
-    voted_against = models.ManyToManyField('scipost.Contributor', blank=True, related_name='voted_against')
-    voted_abstain = models.ManyToManyField('scipost.Contributor', blank=True, related_name='voted_abstain')
+    voted_against = models.ManyToManyField('scipost.Contributor', blank=True,
+                                           related_name='voted_against')
+    voted_abstain = models.ManyToManyField('scipost.Contributor', blank=True,
+                                           related_name='voted_abstain')
     voting_deadline = models.DateTimeField('date submitted', default=timezone.now)
 
     objects = EICRecommendationManager()
@@ -513,6 +518,10 @@ class EICRecommendation(SubmissionRelatedObjectMixin, models.Model):
         return (self.submission.title[:20] + ' by ' + self.submission.author_list[:30] +
                 ', ' + self.get_recommendation_display())
 
+    def get_absolute_url(self):
+        # TODO: Fix this weird redirect, but it's neccesary for the notifications to have one.
+        return self.submission.get_absolute_url()
+
     @property
     def nr_for(self):
         return self.voted_for.count()
diff --git a/submissions/signals.py b/submissions/signals.py
new file mode 100644
index 000000000..53461d84c
--- /dev/null
+++ b/submissions/signals.py
@@ -0,0 +1,46 @@
+from django.contrib.auth.models import User, Group
+
+from notifications.signals import notify
+
+
+def notify_new_manuscript_submitted(sender, instance, created, **kwargs):
+    """
+    Notify the Editorial Administration about a new Submission submitted.
+    """
+    if created:
+        administrators = User.objects.filter(groups__name='Editorial Administrators')
+        for user in administrators:
+            notify.send(sender=sender, recipient=user, actor=instance.submitted_by,
+                        verb=' submitted a new manuscript.', target=instance)
+
+
+def notify_new_editorial_recommendation(sender, instance, created, **kwargs):
+    """
+    Notify the Editorial Recommendation about a new Submission submitted.
+    """
+    if created:
+        administrators = User.objects.filter(groups__name='Editorial Administrators')
+        editor_in_charge = instance.submission.editor_in_charge
+        for user in administrators:
+            notify.send(sender=sender, recipient=user, actor=editor_in_charge,
+                        verb=' formulated a new Editorial Recommendation.', target=instance)
+
+
+def notify_new_editorial_assignment(sender, instance, created, **kwargs):
+    """
+    Notify a College Fellow about a new EIC invitation.
+    """
+    if created:
+        administration = Group.objects.get(name='Editorial Administrators')
+        notify.send(sender=sender, recipient=instance.to.user, actor=administration,
+                    verb=' invited you to become Editor-in-charge.', target=instance)
+
+
+def notify_new_referee_invitation(sender, instance, created, **kwargs):
+    """
+    Notify a Referee about a new refereeing invitation.
+    """
+    if created:
+        notify.send(sender=sender, recipient=instance.referee.user,
+                    actor=instance.submission.editor_in_charge,
+                    verb=' would like to invite you to referee a Submission.', target=instance)
diff --git a/submissions/templates/submissions/pool/assignment_request.html b/submissions/templates/submissions/pool/assignment_request.html
index a01954b9f..2f0b46cde 100644
--- a/submissions/templates/submissions/pool/assignment_request.html
+++ b/submissions/templates/submissions/pool/assignment_request.html
@@ -13,7 +13,7 @@
 {% block pagetitle %}: Assignment Request{% endblock pagetitle %}
 
 {% block content %}
-    <h1>Assignment request</h1>
+    <h1 class="highlight">Assignment request</h1>
     <h3>Can you act as Editor-in-charge? (see below to accept/decline)</h3>
 
     {% include 'submissions/_submission_assignment_request.html' with assignment=assignment consider_assignment_form=form %}
-- 
GitLab