From 1fe7fbecccb8412a86a1376325a00553ce624603 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Mon, 16 Oct 2017 21:27:16 +0200
Subject: [PATCH] Part 5

---
 colleges/forms.py                             | 22 +++++++-
 colleges/managers.py                          | 14 +++++
 .../templates/colleges/submission_add.html    | 30 +++++++++++
 .../templates/colleges/submission_pool.html   | 48 +++++++++++++++++
 .../colleges/conflicts_of_interests.html      |  2 +
 colleges/urls.py                              |  6 +++
 colleges/views.py                             | 52 ++++++++++++++++++-
 submissions/forms.py                          |  2 +-
 .../_submission_card_fellow_content.html      |  7 +++
 9 files changed, 180 insertions(+), 3 deletions(-)
 create mode 100644 colleges/templates/colleges/submission_add.html
 create mode 100644 colleges/templates/colleges/submission_pool.html
 create mode 100644 colleges/templates/partials/colleges/conflicts_of_interests.html

diff --git a/colleges/forms.py b/colleges/forms.py
index d76f0c8c0..1b9ecb4fc 100644
--- a/colleges/forms.py
+++ b/colleges/forms.py
@@ -87,7 +87,7 @@ class FellowshipRemoveSubmissionForm(forms.ModelForm):
 
 class FellowshipAddSubmissionForm(forms.ModelForm):
     submission = forms.ModelChoiceField(queryset=None, to_field_name='arxiv_identifier_w_vn_nr',
-                                        empty_label="Please choice the Submission to add to the pool")
+                                        empty_label="Please choose the Submission to add to the pool")
 
     class Meta:
         model = Fellowship
@@ -103,3 +103,23 @@ class FellowshipAddSubmissionForm(forms.ModelForm):
         fellowship = self.instance
         fellowship.pool.add(submission)
         return fellowship
+
+
+class SubmissionAddFellowshipForm(forms.ModelForm):
+    fellowship = forms.ModelChoiceField(queryset=None, to_field_name='id',
+                                        empty_label="Please choose the Fellow to add to the Pool")
+
+    class Meta:
+        model = Submission
+        fields = []
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        pool = self.instance.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.fellows.add(fellowship)
+        return submission
diff --git a/colleges/managers.py b/colleges/managers.py
index 224ebde3b..55757bc12 100644
--- a/colleges/managers.py
+++ b/colleges/managers.py
@@ -19,3 +19,17 @@ class FellowQuerySet(models.QuerySet):
             Q(start_date__lte=today, until_date__gte=today) |
             Q(start_date__isnull=True, until_date__isnull=True)
             ).order_by('contributor__user__last_name')
+
+    def filter_for_submission_author(self, submission):
+        try:
+            submissions_exclude = Submission.objects.filter()
+            Contributor.objects.filter(user__last_name)
+
+            # return (self.exclude(authors=user.contributor)
+            #         .exclude(Q(author_list__icontains=user.last_name),
+            #                  ~Q(authors_false_claims=user.contributor)))
+            return (self.exclude(contributor__in=submission.authors)
+                    .exclude(Q(contributor__user__last_name=submission.author_list),  # U/S, use: https://docs.djangoproject.com/en/1.11/ref/models/querysets/#iregex
+                             ~Q(contributor__in=submission.authors_false_claims.all())))
+        except:
+            return self.none()
diff --git a/colleges/templates/colleges/submission_add.html b/colleges/templates/colleges/submission_add.html
new file mode 100644
index 000000000..e071c00b4
--- /dev/null
+++ b/colleges/templates/colleges/submission_add.html
@@ -0,0 +1,30 @@
+{% extends 'submissions/admin/base.html' %}
+
+{% load bootstrap %}
+
+{% block breadcrumb_items %}
+    {{ block.super }}
+    <a href="{% url 'colleges:submission' submission.arxiv_identifier_w_vn_nr %}" class="breadcrumb-item">Submission Pool Composition</a>
+    <span class="breadcrumb-item">Add Fellowship</span>
+{% endblock %}
+
+{% block pagetitle %}: Add Fellowship{% endblock pagetitle %}
+
+{% block content %}
+    <h1>Add Fellowship to Submission's Pool</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 Pool:</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_pool.html b/colleges/templates/colleges/submission_pool.html
new file mode 100644
index 000000000..1b487dc58
--- /dev/null
+++ b/colleges/templates/colleges/submission_pool.html
@@ -0,0 +1,48 @@
+{% extends 'submissions/admin/base.html' %}
+
+{% load bootstrap %}
+
+{% block breadcrumb_items %}
+    {{ block.super }}
+    <span class="breadcrumb-item">Submission Pool Composition</span>
+{% endblock %}
+
+{% block pagetitle %}: Submission Pool Composition{% endblock pagetitle %}
+
+{% block content %}
+    <h1>Submission Pool Composition</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>Pool Composition</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.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' fellowship.id submission.arxiv_identifier_w_vn_nr %}">Remove this Fellowship from Submission's pool</a>
+                    </td>
+                </tr>
+            {% endfor %}
+            <tr>
+                <td colspan="4" class="py-3 text-center"><a href="{% url 'colleges:submission_add_fellowship' submission.arxiv_identifier_w_vn_nr %}">Add Fellowship to this Submission's pool</a></td>
+            </tr>
+        </tbody>
+    </table>
+
+
+{% endblock %}
diff --git a/colleges/templates/partials/colleges/conflicts_of_interests.html b/colleges/templates/partials/colleges/conflicts_of_interests.html
new file mode 100644
index 000000000..902b2f025
--- /dev/null
+++ b/colleges/templates/partials/colleges/conflicts_of_interests.html
@@ -0,0 +1,2 @@
+<h3>Possible Conflicts of Interests found:</h3>
+{{ submission.id }}
diff --git a/colleges/urls.py b/colleges/urls.py
index 6ffb8a52c..a363e26c0 100644
--- a/colleges/urls.py
+++ b/colleges/urls.py
@@ -12,6 +12,12 @@ urlpatterns = [
     url(r'^fellowships/(?P<id>[0-9]+)/edit$', views.fellowship_edit, name='fellowship_edit'),
     url(r'^fellowships/(?P<id>[0-9]+)/terminate$', views.fellowship_terminate,
         name='fellowship_terminate'),
+    url(r'^fellowships/submissions/{regex}/$'.format(
+        regex=SUBMISSIONS_COMPLETE_REGEX), views.submission_pool,
+        name='submission'),
+    url(r'^fellowships/submissions/{regex}/add$'.format(
+        regex=SUBMISSIONS_COMPLETE_REGEX), views.submission_add_fellowship,
+        name='submission_add_fellowship'),
     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 04e0467b3..6ad30cc99 100644
--- a/colleges/views.py
+++ b/colleges/views.py
@@ -1,9 +1,12 @@
 from django.contrib import messages
 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 submissions.models import Submission
 
 from .forms import FellowshipForm, FellowshipTerminateForm, FellowshipRemoveSubmissionForm,\
-    FellowshipAddSubmissionForm, AddFellowshipForm
+    FellowshipAddSubmissionForm, AddFellowshipForm, SubmissionAddFellowshipForm
 from .models import Fellowship
 
 
@@ -92,6 +95,53 @@ def fellowship_terminate(request, id):
     return redirect(fellowship.get_absolute_url())
 
 
+@login_required
+@permission_required('scipost.can_manage_college_composition', raise_exception=True)
+def submission_pool(request, arxiv_identifier_w_vn_nr):
+    """
+    List all Fellowships related to Submission.
+    """
+    submission = get_object_or_404(Submission, arxiv_identifier_w_vn_nr=arxiv_identifier_w_vn_nr)
+    # form = FellowshipRemoveSubmissionForm(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(fellowship.get_absolute_url())
+
+    context = {
+        # 'form': form,
+        'submission': submission
+    }
+    return render(request, 'colleges/submission_pool.html', context)
+
+
+@login_required
+@permission_required('scipost.can_manage_college_composition', raise_exception=True)
+def submission_add_fellowship(request, arxiv_identifier_w_vn_nr):
+    """
+    Add Fellowship to the pool of a Submission.
+    """
+    submission = get_object_or_404(Submission, arxiv_identifier_w_vn_nr=arxiv_identifier_w_vn_nr)
+    form = SubmissionAddFellowshipForm(request.POST or None, instance=submission)
+
+    if form.is_valid():
+        form.save()
+        messages.success(request, 'Fellowship {fellowship} ({id}) added to Submission.'.format(
+            fellowship=form.cleaned_data['fellowship'].contributor,
+            id=form.cleaned_data['fellowship'].id))
+        return redirect(reverse('colleges:submission',
+                                args=(submission.arxiv_identifier_w_vn_nr,)))
+
+    context = {
+        'submission': submission,
+        'form': form,
+    }
+    return render(request, 'colleges/submission_add.html', context)
+
+
 @login_required
 @permission_required('scipost.can_manage_college_composition', raise_exception=True)
 def fellowship_remove_submission(request, id, arxiv_identifier_w_vn_nr):
diff --git a/submissions/forms.py b/submissions/forms.py
index c976c3470..7dfb26dae 100644
--- a/submissions/forms.py
+++ b/submissions/forms.py
@@ -263,7 +263,7 @@ class RequestSubmissionForm(SubmissionChecks, forms.ModelForm):
         qs = self.fields['proceeding'].queryset.open_for_submission()
         self.fields['proceeding'].queryset = qs
         self.fields['proceeding'].empty_label = None
-        if qs.count() > 0:
+        if qs.exists():
             # Open the proceedings Journal for submission
             self.fields['submitted_to_journal'].choices += (
                 (SCIPOST_JOURNAL_PHYSICS_PROC, 'SciPost Proceedings'),)
diff --git a/submissions/templates/submissions/_submission_card_fellow_content.html b/submissions/templates/submissions/_submission_card_fellow_content.html
index 49c7e881e..ffd22007f 100644
--- a/submissions/templates/submissions/_submission_card_fellow_content.html
+++ b/submissions/templates/submissions/_submission_card_fellow_content.html
@@ -78,5 +78,12 @@
             </tr>
         {% endif %}
 
+        {% if perms.scipost.can_manage_college_composition %}
+            <tr>
+                <td>Pool size</td>
+                <td><a href="{% url 'colleges:submission' submission.arxiv_identifier_w_vn_nr %}">{{ submission.fellows.count }} Fellowships</a></td>
+            </tr>
+        {% endif %}
+
     </table>
 {% endblock %}
-- 
GitLab