From 66283dc30e15c033f0f0ebdde61c1ca3fa079ee6 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Wed, 21 Mar 2018 10:58:38 +0000
Subject: [PATCH] Revise permissions to encapsulate users ability to review a
 submission

---
 .../templates/dashboard/applicant_dashboard.html     |  2 +-
 opentech/apply/funds/models.py                       | 12 ++++++++++++
 .../funds/applicationsubmission_admin_detail.html    |  2 +-
 .../funds/applicationsubmission_detail.html          |  2 +-
 opentech/apply/funds/templatetags/workflow_tags.py   |  4 ++--
 opentech/apply/review/templatetags/review_tags.py    |  4 ++--
 6 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/opentech/apply/dashboard/templates/dashboard/applicant_dashboard.html b/opentech/apply/dashboard/templates/dashboard/applicant_dashboard.html
index 68b7dffa4..3330c4ebc 100644
--- a/opentech/apply/dashboard/templates/dashboard/applicant_dashboard.html
+++ b/opentech/apply/dashboard/templates/dashboard/applicant_dashboard.html
@@ -27,7 +27,7 @@
                 <h6 class="heading heading--no-margin heading--submission-meta"><span>Submitted:</span> {{ submission.submit_time.date }} by {{ submission.user.get_full_name }}</h6>
             </div>
             {% include "funds/includes/status_bar.html" with phases=submission.workflow status=submission.phase class="status-bar--small" %}
-            {% if request.user|can_edit:submission %}
+            {% if request.user|has_edit_perm:submission %}
                 <a href="{% url 'funds:edit_submission' submission.id %}">Edit your submission</a>
             {% endif %}
         </div>
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index 41ee9b7bf..23a431b7a 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -668,6 +668,18 @@ class ApplicationSubmission(WorkflowHelpers, BaseStreamForm, AbstractFormSubmiss
     def reviewed_by(self, user):
         return self.reviews.filter(author=user).exists()
 
+    def can_review(self, user):
+        if self.reviewed_by(user):
+            return False
+
+        if user.is_apply_staff:
+            return True
+
+        if user in self.missing_reviewer_reviews:
+            return True
+
+        return False
+
     def data_and_fields(self):
         for stream_value in self.form_fields:
             try:
diff --git a/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html b/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html
index c93706166..61d32e9f7 100644
--- a/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html
+++ b/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html
@@ -14,7 +14,7 @@
 
         {% include 'funds/includes/review_table.html' %}
 
-        {% if request.user|can_review:object and not request.user|has_reviewed:object%}
+        {% if request.user|has_review_perm:object and request.user|can_review:object%}
             <a href="{% url 'apply:reviews:create' submission_pk=object.id %}" class="button button--primary">Create review</a>
         {% endif %}
 
diff --git a/opentech/apply/funds/templates/funds/applicationsubmission_detail.html b/opentech/apply/funds/templates/funds/applicationsubmission_detail.html
index 8c4e67783..4b02f221b 100644
--- a/opentech/apply/funds/templates/funds/applicationsubmission_detail.html
+++ b/opentech/apply/funds/templates/funds/applicationsubmission_detail.html
@@ -40,7 +40,7 @@
             <div>
                 <h6 class="heading heading--submission-meta">
                     <span>Submitted: </span>{{ object.submit_time.date }} by {{ object.user.get_full_name }}
-                    {% if request.user|can_edit:submission %}
+                    {% if request.user|has_edit_perm:submission %}
                         <a href="{% url 'funds:edit_submission' object.id %}">Edit</a>
                     {% endif %}
                 </h6>
diff --git a/opentech/apply/funds/templatetags/workflow_tags.py b/opentech/apply/funds/templatetags/workflow_tags.py
index 727101c2b..87adb9ad8 100644
--- a/opentech/apply/funds/templatetags/workflow_tags.py
+++ b/opentech/apply/funds/templatetags/workflow_tags.py
@@ -8,10 +8,10 @@ def check_permission(user, perm, submission):
 
 
 @register.filter
-def can_edit(user, submission):
+def has_edit_perm(user, submission):
     return check_permission(user, 'edit', submission)
 
 
 @register.filter
-def can_review(user, submission):
+def has_review_perm(user, submission):
     return check_permission(user, 'review', submission)
diff --git a/opentech/apply/review/templatetags/review_tags.py b/opentech/apply/review/templatetags/review_tags.py
index 631b00b70..5b6194c6a 100644
--- a/opentech/apply/review/templatetags/review_tags.py
+++ b/opentech/apply/review/templatetags/review_tags.py
@@ -33,5 +33,5 @@ def traffic_light(value):
 
 
 @register.filter
-def has_reviewed(user, submission):
-    return submission.reviewed_by(user)
+def can_review(user, submission):
+    return submission.can_review(user)
-- 
GitLab