From d70fa1a81e897a38ab7b2e6f7d61c0a8c58ea2a2 Mon Sep 17 00:00:00 2001
From: Erin Mullaney <erin.mullaney@torchbox.com>
Date: Tue, 12 Feb 2019 08:34:25 -0500
Subject: [PATCH] #957 don't save role reviewers who have already reviewed as
 additional records in assigned reviewers

---
 opentech/apply/funds/forms.py              | 3 +++
 opentech/apply/funds/models/submissions.py | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/opentech/apply/funds/forms.py b/opentech/apply/funds/forms.py
index 556c2e9a9..75ee3336a 100644
--- a/opentech/apply/funds/forms.py
+++ b/opentech/apply/funds/forms.py
@@ -68,7 +68,10 @@ class UpdateReviewersForm(forms.ModelForm):
         self.request = kwargs.pop('request', None)
         super().__init__(*args, **kwargs)
 
+        # All submitted reviews by non-role reviewers
         self.submitted_reviewers = User.objects.filter(id__in=self.instance.reviews.values('author'))
+        self.submitted_reviewers = self.submitted_reviewers.exclude(
+            id__in=AssignedReviewers.objects.role_reviewers_by_submission(self.instance).values('reviewer'))
 
         if self.can_alter_external_reviewers(self.instance, self.user):
             reviewers = self.instance.reviewers.all()
diff --git a/opentech/apply/funds/models/submissions.py b/opentech/apply/funds/models/submissions.py
index 96fcb6e25..55c7a0030 100644
--- a/opentech/apply/funds/models/submissions.py
+++ b/opentech/apply/funds/models/submissions.py
@@ -677,6 +677,11 @@ class ApplicationRevision(AccessFormData, models.Model):
         })
 
 
+class AssignedReviewersQuerySet(models.QuerySet):
+    def role_reviewers_by_submission(self, submission):
+        return self.filter(role__isnull=False, submission=submission)
+
+
 class AssignedReviewers(models.Model):
     reviewer = models.ForeignKey(
         settings.AUTH_USER_MODEL,
@@ -695,6 +700,8 @@ class AssignedReviewers(models.Model):
         null=True,
     )
 
+    objects = AssignedReviewersQuerySet.as_manager()
+
     class Meta:
         unique_together = ('submission', 'role')
 
-- 
GitLab