From 06903282c5a11325f823d4cc3c573deb06f1a59e Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Thu, 27 Jul 2017 10:12:02 +0200
Subject: [PATCH] Activate Report object permission construction

---
 submissions/managers.py                       |  2 +-
 submissions/models.py                         |  7 +-
 ...single_public_report_without_comments.html |  8 +-
 .../templates/submissions/editorial_page.html | 17 +++-
 .../submissions/vet_submitted_report.html     | 67 +++++++++++++++
 .../vet_submitted_reports_list.html           | 82 ++++++-------------
 submissions/views.py                          | 55 +++++++------
 7 files changed, 147 insertions(+), 91 deletions(-)
 create mode 100644 submissions/templates/submissions/vet_submitted_report.html

diff --git a/submissions/managers.py b/submissions/managers.py
index 43c1902c7..232c7ed9d 100644
--- a/submissions/managers.py
+++ b/submissions/managers.py
@@ -151,7 +151,7 @@ class EICRecommendationManager(models.Manager):
             return self.none()
 
 
-class ReportManager(models.Manager):
+class ReportQuerySet(models.QuerySet):
     def accepted(self):
         return self.filter(status=STATUS_VETTED)
 
diff --git a/submissions/models.py b/submissions/models.py
index 158ae9b36..d189fa68f 100644
--- a/submissions/models.py
+++ b/submissions/models.py
@@ -13,7 +13,7 @@ from .constants import ASSIGNMENT_REFUSAL_REASONS, ASSIGNMENT_NULLBOOL,\
                        SUBMISSION_CYCLES, CYCLE_DEFAULT, CYCLE_SHORT, CYCLE_DIRECT_REC,\
                        EVENT_GENERAL, EVENT_TYPES, EVENT_FOR_AUTHOR, EVENT_FOR_EIC
 from .managers import SubmissionManager, EditorialAssignmentManager, EICRecommendationManager,\
-                      ReportManager, SubmissionEventQuerySet
+                      ReportQuerySet, SubmissionEventQuerySet
 from .utils import ShortSubmissionCycle, DirectRecommendationSubmissionCycle,\
                    GeneralSubmissionCycle
 
@@ -343,12 +343,15 @@ class Report(models.Model):
     anonymous = models.BooleanField(default=True, verbose_name='Publish anonymously')
     pdf_report = models.FileField(upload_to='UPLOADS/REPORTS/%Y/%m/', max_length=200, blank=True)
 
-    objects = ReportManager()
+    objects = ReportQuerySet.as_manager()
 
     class Meta:
         unique_together = ('submission', 'report_nr')
         default_related_name = 'reports'
         ordering = ['-date_submitted']
+        permissions = (
+            ('can_vet_submitted_reports', 'Can vet submitted Reports'),
+        )
 
     def __str__(self):
         return (self.author.user.first_name + ' ' + self.author.user.last_name + ' on ' +
diff --git a/submissions/templates/submissions/_single_public_report_without_comments.html b/submissions/templates/submissions/_single_public_report_without_comments.html
index 2c5c69adc..289f2169d 100644
--- a/submissions/templates/submissions/_single_public_report_without_comments.html
+++ b/submissions/templates/submissions/_single_public_report_without_comments.html
@@ -7,14 +7,14 @@
             {% if user.contributor == submission.editor_in_charge or user|is_in_group:'Editorial Administrators' and user|is_not_author_of_submission:submission.arxiv_identifier_w_vn_nr %}
 
                 <div class="reportid">
-                    <h3>{% if report.anonymous %}(chose public anonymity) {% endif %}<a href="{% url 'scipost:contributor_info' report.author.id %}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>
+                    <h3>{% if report.anonymous %}(chose public anonymity) {% endif %}<a href="{{report.author.get_absolute_url}}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>
                         on {{ report.date_submitted|date:'Y-n-j' }}</h3>
                     </h3>
                     {% if report.pdf_report %}
                         <a href="{% url 'submissions:report_detail_pdf' report.submission.arxiv_identifier_w_vn_nr report.report_nr %}" target="_blank">Download as PDF</a>
                     {% endif %}
                     {% if perms.scipost.can_manage_reports %}
-                        {% if report.pdf_report %}&middot; {% endif %}<a href="{% url 'submissions:report_pdf_compile' report.id %}"{% if not report.pdf_report %}class="btn btn-warning btn-sm"{% endif %}>Update/Compile the Report pdf</a>
+                        {% if report.pdf_report %}&middot; {% endif %}<a href="{% url 'submissions:report_pdf_compile' report.id %}">Update/Compile the Report pdf</a>
                     {% endif %}
                 </div>
 
@@ -46,14 +46,14 @@
                 </div>
             {% else %}
                 <div class="reportid">
-                    <h3>{% if report.anonymous %}Anonymous Report {{report.report_nr}}{% else %}<a href="{% url 'scipost:contributor_info' report.author.id %}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>{% endif %}
+                    <h3>{% if report.anonymous %}Anonymous Report {{report.report_nr}}{% else %}<a href="{{report.author.get_absolute_url}}">{{ report.author.user.first_name }} {{ report.author.user.last_name }}</a>{% endif %}
                         on {{ report.date_submitted|date:'Y-n-j' }}</h3>
                     </h3>
                     {% if report.pdf_report %}
                         <a href="{% url 'submissions:report_detail_pdf' report.submission.arxiv_identifier_w_vn_nr report.report_nr %}" target="_blank">Download as PDF</a>
                     {% endif %}
                     {% if perms.scipost.can_manage_reports %}
-                        {% if report.pdf_report %}&middot; {% endif %}<a href="{% url 'submissions:report_pdf_compile' report.id %}"{% if not report.pdf_report %}class="btn btn-warning btn-sm"{% endif %}>Update/Compile the Report pdf</a>
+                        {% if report.pdf_report %}&middot; {% endif %}<a href="{% url 'submissions:report_pdf_compile' report.id %}">Update/Compile the Report pdf</a>
                     {% endif %}
                 </div>
 
diff --git a/submissions/templates/submissions/editorial_page.html b/submissions/templates/submissions/editorial_page.html
index 5b562c5b4..3491f385a 100644
--- a/submissions/templates/submissions/editorial_page.html
+++ b/submissions/templates/submissions/editorial_page.html
@@ -170,11 +170,24 @@
                                 </div>
                             </form>
                         </li>
-                        <li><a href="{% url 'submissions:vet_submitted_reports_list' %}">Vet submitted Reports</a> ({{ submission.reports.awaiting_vetting.count }})</li>
+                        {% with submission.reports.awaiting_vetting as reports %}
+                            {% if reports %}
+                                <li>
+                                    Vet submitted Report{{reports|pluralize}}:
+                                    <ul class="mb-1">
+                                        {% for report in reports %}
+                                            <li><a href="{% url 'submissions:vet_submitted_report' report.id %}">{{report}}</a></li>
+                                        {% endfor %}
+                                    </ul>
+                                </li>
+                            {% else %}
+                                <li>All Reports have been vetted.</li>
+                            {% endif %}
+                        {% endwith %}
                         {% with submission.comments.awaiting_vetting as comments %}
                             {% if comments %}
                                 <li>
-                                    Vet submitted Comments:
+                                    Vet submitted Comment{{comments|pluralize}}:
                                     <ul class="mb-1">
                                         {% for comment in comments %}
                                             <li><a href="{% url 'comments:vet_submitted_comment' comment.id %}">{{comment}}</a></li>
diff --git a/submissions/templates/submissions/vet_submitted_report.html b/submissions/templates/submissions/vet_submitted_report.html
new file mode 100644
index 000000000..f52b135a8
--- /dev/null
+++ b/submissions/templates/submissions/vet_submitted_report.html
@@ -0,0 +1,67 @@
+{% extends 'submissions/_pool_base.html' %}
+
+{% block pagetitle %}: vet reports{% endblock pagetitle %}
+
+{% load bootstrap %}
+
+{% block headsup %}
+
+<script>
+$(document).ready(function(){
+  $('[name="action_option"]').on('change', function() {
+      if ($('[name="action_option"][value="refuse"]').is(':checked')) {
+          $('#refusal').show();
+      }
+      else {
+          $('#refusal').hide();
+      }
+  }).trigger('change');
+});
+</script>
+
+{% endblock headsup %}
+
+{% block breadcrumb_items %}
+    {{block.super}}
+    <span class="breadcrumb-item">Vet Report {{report.report_nr}}</span>
+{% endblock %}
+
+{% block content %}
+
+<div class="row">
+    <div class="col-12">
+        {% if not report_to_vet %}
+            <h1>There are no Reports for you to vet.</h1>
+            <p>Go back to my <a href="{% url 'scipost:personal_page' %}">personal page</a>.</p>
+        {% else %}
+            <h1 class="highlight">SciPost Report to vet:</h1>
+
+            <h2 class="mb-2">Submission associated to Report:</h2>
+            <div class="row">
+                <div class="col-12">
+                    {% include 'submissions/_submission_summary_short.html' with submission=report_to_vet.submission %}
+                </div>
+            </div>
+
+            <h2 class="mb-2">Report to vet:</h2>
+
+            {% include 'submissions/_single_public_report_without_comments.html' with submission=report_to_vet.submission report=report_to_vet %}
+
+            <hr class="small">
+            <h2>Please vet this Report:</h2>
+            <form action="{% url 'submissions:vet_submitted_reports_list' %}" method="post">
+                {% csrf_token %}
+                {{ form.report }}
+                {{ form.action_option|bootstrap }}
+                <div class="col-md-6" id="refusal">
+                    {{ form.refusal_reason|bootstrap }}
+                    {{ form.email_response_field|bootstrap }}
+                </div>
+                <input class="btn btn-secondary" type="submit" value="Submit" />
+            </form>
+
+        {% endif %}
+    </div>
+</div>
+
+{% endblock content %}
diff --git a/submissions/templates/submissions/vet_submitted_reports_list.html b/submissions/templates/submissions/vet_submitted_reports_list.html
index b3b521a87..44697fdc2 100644
--- a/submissions/templates/submissions/vet_submitted_reports_list.html
+++ b/submissions/templates/submissions/vet_submitted_reports_list.html
@@ -2,67 +2,39 @@
 
 {% block pagetitle %}: vet reports{% endblock pagetitle %}
 
-{% load bootstrap %}
-
-{% block headsup %}
-
-<script>
-$(document).ready(function(){
-  $('[name="action_option"]').on('change', function() {
-      if ($('[name="action_option"][value="refuse"]').is(':checked')) {
-          $('#refusal').show();
-      }
-      else {
-          $('#refusal').hide();
-      }
-  }).trigger('change');
-});
-</script>
-
-{% endblock headsup %}
-
 {% block breadcrumb_items %}
     {{block.super}}
-    <a href="{% url 'submissions:pool' %}" class="breadcrumb-item">Pool</a>
     <span class="breadcrumb-item">Vet Reports</span>
 {% endblock %}
 
 {% block content %}
 
-<div class="row">
-    <div class="col-12">
-        {% if not report_to_vet %}
-            <h1>There are no Reports for you to vet.</h1>
-            <p>Go back to my <a href="{% url 'scipost:personal_page' %}">personal page</a>.</p>
-        {% else %}
-            <h1 class="highlight">SciPost Report to vet:</h1>
-
-            <h2 class="mb-2">Submission associated to Report:</h2>
-            <div class="row">
-                <div class="col-12">
-                    {% include 'submissions/_submission_summary_short.html' with submission=report_to_vet.submission %}
-                </div>
-            </div>
-
-            <h2 class="mb-2">Report to vet:</h2>
-
-            {% include 'submissions/_single_public_report_without_comments.html' with submission=report_to_vet.submission report=report_to_vet %}
-
-            <hr class="small">
-            <h2>Please vet this Report:</h2>
-            <form action="{% url 'submissions:vet_submitted_reports_list' %}" method="post">
-                {% csrf_token %}
-                {{ form.report }}
-                {{ form.action_option|bootstrap }}
-                <div class="col-md-6" id="refusal">
-                    {{ form.refusal_reason|bootstrap }}
-                    {{ form.email_response_field|bootstrap }}
-                </div>
-                <input class="btn btn-secondary" type="submit" value="Submit" />
-            </form>
-
-        {% endif %}
-    </div>
-</div>
+<h1>SciPost Reports to vet</h1>
+
+{% if reports_to_vet %}
+    <table class="table mt-4">
+        <thead>
+            <th>Report #</th>
+            <th>For submission</th>
+            <th>Submitted</th>
+            <th>Actions</th>
+        </thead>
+        <tbody>
+            {% for report in reports_to_vet %}
+                <tr>
+                    <td>{{report.report_nr}}</td>
+                    <td>{{report.submission.title}} ({{report.submission.arxiv_identifier_w_vn_nr}})</td>
+                    <td>{{report.date_submitted|timesince}} ago</td>
+                    <td><a href="{% url 'submissions:vet_submitted_report' report.id  %}">Go to vetting page</a></td>
+                </tr>
+            {% endfor %}
+        </tbody>
+    </table>
+{% else %}
+    <h2 class="mt-3">There are no Reports for you to vet.</h2>
+    <p>Go back to my <a href="{% url 'scipost:personal_page' %}">personal page</a>.</p>
+{% endif %}
+
+<div class="mb-5"></div>
 
 {% endblock content %}
diff --git a/submissions/views.py b/submissions/views.py
index b6c11219e..e9c6acd28 100644
--- a/submissions/views.py
+++ b/submissions/views.py
@@ -1183,6 +1183,10 @@ def submit_report(request, arxiv_identifier_w_vn_nr):
         SubmissionUtils.email_EIC_report_delivered()
         SubmissionUtils.email_referee_report_delivered()
 
+        # Assign explicit permission to EIC to vet this report
+        assign_perm('submissions.can_vet_submitted_reports', submission.editor_in_charge.user,
+                    newreport)
+
         # Add SubmissionEvents for the EIC only, as it can also be rejected still
         submission.add_event_for_eic('%s has submitted a new Report.'
                                      % request.user.last_name)
@@ -1198,19 +1202,30 @@ def submit_report(request, arxiv_identifier_w_vn_nr):
 @permission_required('submissions.can_vet_submitted_reports', raise_exception=True)
 def vet_submitted_reports_list(request):
     """
-    Reports with status `unvetted` will be shown one-by-one (oldest first). A user may only
-    vet reports of submissions he/she is EIC of.
+    Reports with status `unvetted` will be shown (oldest first).
+    """
+    reports_to_vet = Report.objects.awaiting_vetting().order_by('date_submitted')
+    context = {'reports_to_vet': reports_to_vet}
+    return render(request, 'submissions/vet_submitted_reports_list.html', context)
+
+
+@login_required
+@transaction.atomic
+def vet_submitted_report(request, report_id):
+    """
+    Report with status `unvetted` will be shown. A user may only vet reports of submissions
+    he/she is EIC of or if he/she is SciPost Admin or Vetting Editor.
 
     After vetting an email is sent to the report author, bcc EIC. If report
     has not been refused, the submission author is also mailed.
     """
-    contributor = request.user.contributor
-    report_to_vet = (Report.objects.awaiting_vetting()
-                     .select_related('submission')
-                     .filter(submission__editor_in_charge=contributor)
-                     .order_by('date_submitted').first())
+    # Method `get_objects_for_user` gets all Reports that are assigned to the user
+    # or *all* Reports if user is SciPost Admin or Vetting Editor.
+    report = get_object_or_404((get_objects_for_user(request.user,
+                                                     'submissions.can_vet_submitted_reports')
+                                .awaiting_vetting()), id=report_id)
 
-    form = VetReportForm(request.POST or None, initial={'report': report_to_vet})
+    form = VetReportForm(request.POST or None, initial={'report': report})
     if form.is_valid():
         report = form.process_vetting(request.user.contributor)
 
@@ -1230,27 +1245,13 @@ def vet_submitted_reports_list(request):
             report.submission.add_event_for_author('A new Report has been submitted.')
 
         message = 'Submitted Report vetted for <a href="%s">%s</a>.' % (
-            reverse('submissions:editorial_page',
-                    args=(report.submission.arxiv_identifier_w_vn_nr,)),
-            report.submission.arxiv_identifier_w_vn_nr
-        )
+                    report.submission.get_absolute_url(),
+                    report.submission.arxiv_identifier_w_vn_nr)
         messages.success(request, message)
-        # Redirect instead of render to loose the POST call and make it a GET
+        # ??????????????????
         return redirect(reverse('submissions:vet_submitted_reports_list'))
-    context = {'contributor': contributor, 'report_to_vet': report_to_vet, 'form': form}
-    return render(request, 'submissions/vet_submitted_reports_list.html', context)
-
-
-@login_required
-@transaction.atomic
-def vet_submitted_report(request, report_id):
-    # Method `get_objects_for_user` gets all Reports that are assigned to the user
-    # or *all* Reports if user is SciPost Admin or Vetting Editor.
-    report = get_object_or_404((get_objects_for_user(request.user,
-                                                     'submissions.can_vet_submitted_reports')
-                                .awaiting_vetting()), id=report_id)
-
-    raise NotImplemented
+    context = {'report_to_vet': report, 'form': form}
+    return render(request, 'submissions/vet_submitted_report.html', context)
 
 
 @permission_required('scipost.can_prepare_recommendations_for_voting', raise_exception=True)
-- 
GitLab