From fc01656b1d14a83bca0667df4e42ea586dd96424 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Fri, 17 Nov 2017 21:38:42 +0100
Subject: [PATCH] Set up manage for voting fellows

---
 colleges/forms.py                             | 45 ++++++++++++
 .../fellowship_submission_remove_voting.html  | 28 ++++++++
 .../colleges/submission_add_for_voting.html   | 30 ++++++++
 .../colleges/submission_voting_fellows.html   | 48 +++++++++++++
 colleges/urls.py                              | 10 +++
 colleges/views.py                             | 70 ++++++++++++++++++-
 submissions/admin.py                          |  3 +-
 .../submissions/pool/submission_details.html  |  4 ++
 .../pool/submission_info_table.html           |  7 +-
 9 files changed, 241 insertions(+), 4 deletions(-)
 create mode 100644 colleges/templates/colleges/fellowship_submission_remove_voting.html
 create mode 100644 colleges/templates/colleges/submission_add_for_voting.html
 create mode 100644 colleges/templates/colleges/submission_voting_fellows.html

diff --git a/colleges/forms.py b/colleges/forms.py
index 3c1c8f311..7a4653094 100644
--- a/colleges/forms.py
+++ b/colleges/forms.py
@@ -86,6 +86,30 @@ class FellowshipRemoveSubmissionForm(forms.ModelForm):
         return fellowship
 
 
+class FellowVotingRemoveSubmissionForm(forms.ModelForm):
+    """
+    Use this form in admin-accessible views only! It could possibly reveal the
+    identity of the Editor-in-charge!
+    """
+    class Meta:
+        model = Fellowship
+        fields = []
+
+    def __init__(self, *args, **kwargs):
+        self.submission = kwargs.pop('submission')
+        super().__init__(*args, **kwargs)
+
+    def clean(self):
+        if self.submission.editor_in_charge == self.instance.contributor:
+            self.add_error(None, ('Submission cannot be removed as the Fellow is'
+                                  ' Editor-in-charge of this Submission.'))
+
+    def save(self):
+        fellowship = self.instance
+        fellowship.voting_pool.remove(self.submission)
+        return fellowship
+
+
 class FellowshipAddSubmissionForm(forms.ModelForm):
     submission = forms.ModelChoiceField(queryset=None, to_field_name='arxiv_identifier_w_vn_nr',
                                         empty_label="Please choose the Submission to add to the pool")
@@ -126,6 +150,27 @@ class SubmissionAddFellowshipForm(forms.ModelForm):
         return submission
 
 
+class SubmissionAddVotingFellowForm(forms.ModelForm):
+    fellowship = forms.ModelChoiceField(
+        queryset=None, to_field_name='id',
+        empty_label="Please choose the Fellow to add to the Submission's Voting Fellows")
+
+    class Meta:
+        model = Submission
+        fields = []
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        pool = self.instance.voting_fellows.values_list('id', flat=True)
+        self.fields['fellowship'].queryset = Fellowship.objects.active().exclude(id__in=pool)
+
+    def save(self):
+        fellowship = self.cleaned_data['fellowship']
+        submission = self.instance
+        submission.voting_fellows.add(fellowship)
+        return submission
+
+
 class FellowshipRemoveProceedingsForm(forms.ModelForm):
     """
     Use this form in admin-accessible views only! It could possibly reveal the
diff --git a/colleges/templates/colleges/fellowship_submission_remove_voting.html b/colleges/templates/colleges/fellowship_submission_remove_voting.html
new file mode 100644
index 000000000..837f1955a
--- /dev/null
+++ b/colleges/templates/colleges/fellowship_submission_remove_voting.html
@@ -0,0 +1,28 @@
+{% extends 'submissions/admin/base.html' %}
+
+{% load bootstrap %}
+
+{% block breadcrumb_items %}
+    {{ block.super }}
+    <a href="{% url 'colleges:submission_voting_fellows' submission.arxiv_identifier_w_vn_nr %}" class="breadcrumb-item">Submission's Voting Fellows</a>
+    <span class="breadcrumb-item">Remove Fellow</span>
+{% endblock %}
+
+{% block pagetitle %}: Remove Fellow from Submission's Voting Fellows{% endblock pagetitle %}
+
+{% block content %}
+    <h1>Remove Fellow from Submission's Voting Fellows</h1>
+    <h2 class="text-primary">Fellowship {{ fellowship }}</h2>
+
+    <h3>Submission details</h3>
+    {% include 'submissions/_submission_summary_short.html' with submission=submission %}
+    <br>
+
+    <form method="post">
+        {% csrf_token %}
+        {{ form|bootstrap }}
+        <input class="btn btn-danger px-3" type="submit" value="Remove from Submission's Voting Fellows">
+    </form>
+
+
+{% endblock %}
diff --git a/colleges/templates/colleges/submission_add_for_voting.html b/colleges/templates/colleges/submission_add_for_voting.html
new file mode 100644
index 000000000..35338669f
--- /dev/null
+++ b/colleges/templates/colleges/submission_add_for_voting.html
@@ -0,0 +1,30 @@
+{% extends 'submissions/admin/base.html' %}
+
+{% load bootstrap %}
+
+{% block breadcrumb_items %}
+    {{ block.super }}
+    <a href="{% url 'colleges:submission_voting_fellows' submission.arxiv_identifier_w_vn_nr %}" class="breadcrumb-item">Submission's Voting Fellows</a>
+    <span class="breadcrumb-item">Add Fellowship for voting</span>
+{% endblock %}
+
+{% block pagetitle %}: Add Fellowship for voting{% endblock pagetitle %}
+
+{% block content %}
+    <h1>Add Fellowship to Submission's Voting Fellows</h1>
+    <h2 class="text-primary">{{submission.title}}</h2>
+    <h3 class="mb-3">by {{submission.author_list}}</h3>
+    {% include 'submissions/_submission_summary.html' with submission=submission hide_title=1 %}
+    <br>
+
+    <h3>Choose one of the following (active) Fellowships to add to the Submission's Voting Fellows:</h3>
+    {% include 'partials/colleges/conflicts_of_interests.html' with submission=submission %}
+    <br>
+    <form method="post">
+        {% csrf_token %}
+        {{ form|bootstrap }}
+        <input class="btn btn-primary" type="submit" value="Add Fellowship">
+    </form>
+
+
+{% endblock %}
diff --git a/colleges/templates/colleges/submission_voting_fellows.html b/colleges/templates/colleges/submission_voting_fellows.html
new file mode 100644
index 000000000..bfd44e870
--- /dev/null
+++ b/colleges/templates/colleges/submission_voting_fellows.html
@@ -0,0 +1,48 @@
+{% extends 'submissions/admin/base.html' %}
+
+{% load bootstrap %}
+
+{% block breadcrumb_items %}
+    {{ block.super }}
+    <span class="breadcrumb-item">Submission's Voting Fellows</span>
+{% endblock %}
+
+{% block pagetitle %}: Submission's Voting Fellows{% endblock pagetitle %}
+
+{% block content %}
+    <h1>Submission's Voting Fellows</h1>
+    <h2 class="text-primary">{{submission.title}}</h2>
+    <h3 class="mb-3">by {{submission.author_list}}</h3>
+    {% include 'submissions/_submission_summary.html' with submission=submission hide_title=1 %}
+    <br>
+
+    <h3>Voting Fellows</h3>
+    {% include 'partials/colleges/conflicts_of_interests.html' with submission=submission %}
+    <table class="table table-hover">
+        <thead>
+            <tr>
+                <th>Fellowship ID</th>
+                <th>Fellow</th>
+                <th>Type</th>
+                <th colspan="2">Date range</th>
+            </tr>
+        </thead>
+        <tbody>
+            {% for fellowship in submission.voting_fellows.all %}
+                <tr>
+                    <td>{{ fellowship.id }}</td>
+                    <td>{{ fellowship.contributor }}</td>
+                    <td>{{ fellowship.guest|yesno:"Guest fellowship,Regular fellowship"|safe }}</td>
+                    <td>
+                        <a class="text-danger" href="{% url 'colleges:fellowship_remove_submission_voting' fellowship.id submission.arxiv_identifier_w_vn_nr %}">Remove this Fellowship from Submission's Voting Fellows</a>
+                    </td>
+                </tr>
+            {% endfor %}
+            <tr>
+                <td colspan="4" class="py-3 text-center"><a href="{% url 'colleges:submission_add_fellowship_voting' submission.arxiv_identifier_w_vn_nr %}">Add Fellowship to this Submission's Voting Fellows</a></td>
+            </tr>
+        </tbody>
+    </table>
+
+
+{% endblock %}
diff --git a/colleges/urls.py b/colleges/urls.py
index e5ddb9459..5b9f0faaa 100644
--- a/colleges/urls.py
+++ b/colleges/urls.py
@@ -15,9 +15,19 @@ urlpatterns = [
     url(r'^fellowships/submissions/{regex}/$'.format(
         regex=SUBMISSIONS_COMPLETE_REGEX), views.submission_pool,
         name='submission'),
+    url(r'^fellowships/submissions/{regex}/voting$'.format(
+        regex=SUBMISSIONS_COMPLETE_REGEX), views.submission_voting_fellows,
+        name='submission_voting_fellows'),
     url(r'^fellowships/submissions/{regex}/add$'.format(
         regex=SUBMISSIONS_COMPLETE_REGEX), views.submission_add_fellowship,
         name='submission_add_fellowship'),
+    url(r'^fellowships/submissions/{regex}/voting/add$'.format(
+        regex=SUBMISSIONS_COMPLETE_REGEX), views.submission_add_fellowship_voting,
+        name='submission_add_fellowship_voting'),
+    url(r'^fellowships/(?P<id>[0-9]+)/submissions/{regex}/remove$'.format(
+        regex=SUBMISSIONS_COMPLETE_REGEX), views.fellowship_remove_submission_voting,
+        name='fellowship_remove_submission_voting'),
+
     url(r'^fellowships/(?P<id>[0-9]+)/submissions/{regex}/remove$'.format(
         regex=SUBMISSIONS_COMPLETE_REGEX), views.fellowship_remove_submission,
         name='fellowship_remove_submission'),
diff --git a/colleges/views.py b/colleges/views.py
index 81bc5f917..56920ea4d 100644
--- a/colleges/views.py
+++ b/colleges/views.py
@@ -3,12 +3,13 @@ from django.contrib.auth.decorators import login_required, permission_required
 from django.shortcuts import get_object_or_404, render, redirect
 from django.core.urlresolvers import reverse
 
-from proceedings.models import Proceedings
+# from proceedings.models import Proceedings
 from submissions.models import Submission
 
 from .forms import FellowshipForm, FellowshipTerminateForm, FellowshipRemoveSubmissionForm,\
     FellowshipAddSubmissionForm, AddFellowshipForm, SubmissionAddFellowshipForm,\
-    FellowshipRemoveProceedingsForm, FellowshipAddProceedingsForm
+    FellowshipRemoveProceedingsForm, FellowshipAddProceedingsForm, SubmissionAddVotingFellowForm,\
+    FellowVotingRemoveSubmissionForm
 from .models import Fellowship
 
 
@@ -111,6 +112,71 @@ def submission_pool(request, arxiv_identifier_w_vn_nr):
     return render(request, 'colleges/submission_pool.html', context)
 
 
+@login_required
+@permission_required('scipost.can_manage_college_composition', raise_exception=True)
+def submission_voting_fellows(request, arxiv_identifier_w_vn_nr):
+    """
+    List all Fellowships selected for voting on the EIC related to Submission.
+    """
+    submission = get_object_or_404(Submission, arxiv_identifier_w_vn_nr=arxiv_identifier_w_vn_nr)
+
+    context = {
+        'submission': submission
+    }
+    return render(request, 'colleges/submission_voting_fellows.html', context)
+
+
+@login_required
+@permission_required('scipost.can_manage_college_composition', raise_exception=True)
+def submission_add_fellowship_voting(request, arxiv_identifier_w_vn_nr):
+    """
+    Add Fellowship to the Fellows voting on the EICRecommendation of a Submission.
+    """
+    submission = get_object_or_404(Submission, arxiv_identifier_w_vn_nr=arxiv_identifier_w_vn_nr)
+    form = SubmissionAddVotingFellowForm(request.POST or None, instance=submission)
+
+    if form.is_valid():
+        form.save()
+        messages.success(request, 'Fellowship {fellowship} ({id}) added to voting Fellows.'.format(
+            fellowship=form.cleaned_data['fellowship'].contributor,
+            id=form.cleaned_data['fellowship'].id))
+        return redirect(reverse('colleges:submission_voting_fellows',
+                                args=(submission.arxiv_identifier_w_vn_nr,)))
+
+    context = {
+        'submission': submission,
+        'form': form,
+    }
+    return render(request, 'colleges/submission_add_for_voting.html', context)
+
+
+@login_required
+@permission_required('scipost.can_manage_college_composition', raise_exception=True)
+def fellowship_remove_submission_voting(request, id, arxiv_identifier_w_vn_nr):
+    """
+    Remove Fellow from the EICRecommendation voting group for the Submission.
+    """
+    fellowship = get_object_or_404(Fellowship, id=id)
+    submission = get_object_or_404(fellowship.voting_pool.all(),
+                                   arxiv_identifier_w_vn_nr=arxiv_identifier_w_vn_nr)
+    form = FellowVotingRemoveSubmissionForm(request.POST or None,
+                                            submission=submission, instance=fellowship)
+
+    if form.is_valid() and request.POST:
+        form.save()
+        messages.success(request, 'Submission {sub} removed from Fellowship.'.format(
+            sub=arxiv_identifier_w_vn_nr))
+        return redirect(reverse('colleges:submission_voting_fellows',
+                                args=(submission.arxiv_identifier_w_vn_nr,)))
+
+    context = {
+        'fellowship': fellowship,
+        'form': form,
+        'submission': submission
+    }
+    return render(request, 'colleges/fellowship_submission_remove_voting.html', context)
+
+
 @login_required
 @permission_required('scipost.can_manage_college_composition', raise_exception=True)
 def submission_add_fellowship(request, arxiv_identifier_w_vn_nr):
diff --git a/submissions/admin.py b/submissions/admin.py
index 8a2f0f0e8..0f52fe45e 100644
--- a/submissions/admin.py
+++ b/submissions/admin.py
@@ -114,7 +114,8 @@ class SubmissionAdmin(GuardedModelAdmin):
                 'proceedings',
                 'pdf_refereeing_pack',
                 'plagiarism_report',
-                'fellows'),
+                'fellows',
+                'voting_fellows'),
         }),
         ('Meta', {
             'classes': ('collapse',),
diff --git a/submissions/templates/partials/submissions/pool/submission_details.html b/submissions/templates/partials/submissions/pool/submission_details.html
index 770936a3d..36614f27f 100644
--- a/submissions/templates/partials/submissions/pool/submission_details.html
+++ b/submissions/templates/partials/submissions/pool/submission_details.html
@@ -41,6 +41,9 @@
         {% if is_editorial_admin %}
             <h3>Editorial Administration</h3>
             <ul class="pl-4 mb-3">
+                <li><a href="{% url 'colleges:submission' submission.arxiv_identifier_w_vn_nr %}">Manage Pool composition</a></li>
+                <li><a href="{% url 'colleges:submission_voting_fellows' submission.arxiv_identifier_w_vn_nr %}">Manage Voting Fellows</a></li>
+
                 {# EIC Assignments #}
                 {% if perms.scipost.can_assign_submissions %}
                     <li>EIC Assignment requests:</li>
@@ -86,6 +89,7 @@
                     <li><a href="{% url 'submissions:treated_submission_pdf_compile' submission.arxiv_identifier_w_vn_nr %}">Update the Refereeing Package pdf</a></li>
                     <li>After proofs have been accepted, you can <a href="{% url 'journals:initiate_publication' %}">initiate the publication process</a> (leads to the validation page)</li>
                 {% endif %}
+
             </ul>
 
             <h3>Events</h3>
diff --git a/submissions/templates/partials/submissions/pool/submission_info_table.html b/submissions/templates/partials/submissions/pool/submission_info_table.html
index 62ae68a23..49ec884f6 100644
--- a/submissions/templates/partials/submissions/pool/submission_info_table.html
+++ b/submissions/templates/partials/submissions/pool/submission_info_table.html
@@ -77,8 +77,13 @@
     {% if perms.scipost.can_manage_college_composition %}
         <tr>
             <td>Pool composition</td>
-            <td><a href="{% url 'colleges:submission' submission.arxiv_identifier_w_vn_nr %}">{{ submission.fellows.count }} Fellowships</a></td>
+            <td><a href="{% url 'colleges:submission' submission.arxiv_identifier_w_vn_nr %}">{{ submission.fellows.count }} Fellowship{{ submission.fellows.count|pluralize }}</a></td>
         </tr>
+        <tr>
+            <td>Fellows voting</td>
+            <td><a href="{% url 'colleges:submission_voting_fellows' submission.arxiv_identifier_w_vn_nr %}">{{ submission.voting_fellows.count }} Fellowship{{ submission.voting_fellows.count|pluralize }}</a></td>
+        </tr>
+
     {% endif %}
 
 </table>
-- 
GitLab