From 8dc677aefa2cfa8591106d8b9d75da6175e3b2f7 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sat, 14 Apr 2018 21:58:43 +0200
Subject: [PATCH] Prevent double mailing after comment mailing.

---
 comments/utils.py     | 14 +++++++++++++-
 comments/views.py     |  8 ++++----
 submissions/models.py |  2 ++
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/comments/utils.py b/comments/utils.py
index 8ffa2e4fc..3f04f17ed 100644
--- a/comments/utils.py
+++ b/comments/utils.py
@@ -25,8 +25,20 @@ class CommentUtils(BaseMailUtil):
         Requires loading:
         comment -- Comment
         """
+        from submissions.models import Submission, Report
+
+        comment = cls._context['comment']
+        send_mail = True
+        if isinstance(comment.content_object, Submission):
+            send_mail = comment.author not in comment.content_object.authors.all()
+        elif isinstance(comment.content_object, Report):
+            send_mail = comment.author not in comment.content_object.submission.authors.all()
+
+        if not send_mail:
+            return
+
         cls._send_mail(cls, 'comment_vet_accepted',
-                       [cls._context['comment'].author.user.email],
+                       [comment.author.user.email],
                        'SciPost Comment published')
 
     @classmethod
diff --git a/comments/views.py b/comments/views.py
index f916794d0..69f658645 100644
--- a/comments/views.py
+++ b/comments/views.py
@@ -76,10 +76,6 @@ def vet_submitted_comment(request, comment_id):
             comment.vetted_by = request.user.contributor
             comment.save()
 
-            # Send emails
-            CommentUtils.load({'comment': comment})
-            CommentUtils.email_comment_vet_accepted_to_author()
-
             # Update `latest_activity` fields
             content_object = comment.content_object
             content_object.latest_activity = timezone.now()
@@ -100,6 +96,10 @@ def vet_submitted_comment(request, comment_id):
                     SubmissionUtils.load({'submission': content_object.submission})
                     SubmissionUtils.send_author_comment_received_email()
 
+            # Send emails
+            CommentUtils.load({'comment': comment})
+            CommentUtils.email_comment_vet_accepted_to_author()
+
         elif form.cleaned_data['action_option'] == '2':
             # The comment request is simply rejected
             comment.status = int(form.cleaned_data['refusal_reason'])
diff --git a/submissions/models.py b/submissions/models.py
index 7152bbec3..7b9e7a348 100644
--- a/submissions/models.py
+++ b/submissions/models.py
@@ -70,6 +70,8 @@ class Submission(models.Model):
                                         default=CYCLE_DEFAULT)
     fellows = models.ManyToManyField('colleges.Fellowship', blank=True,
                                      related_name='pool')
+    # visible_pool = models.BooleanField(default=True)
+    # visible_public = models.BooleanField(default=False)
     subject_area = models.CharField(max_length=10, choices=SCIPOST_SUBJECT_AREAS,
                                     verbose_name='Primary subject area', default='Phys:QP')
     submission_type = models.CharField(max_length=10, choices=SUBMISSION_TYPE)
-- 
GitLab