From c6bcc80615146e571bd8c29e9b07e44d9d6147d8 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Mon, 29 Jan 2018 20:43:42 +0100
Subject: [PATCH] Add Contributors filter

---
 scipost/forms.py                              | 18 ++++++++
 .../personal_page/editorial_actions.html      |  1 +
 .../scipost/contributors_filter.html          | 42 +++++++++++++++++++
 .../draft_registration_invitation.html        |  3 +-
 scipost/urls.py                               |  1 +
 scipost/views.py                              | 28 ++++++++++++-
 6 files changed, 91 insertions(+), 2 deletions(-)
 create mode 100644 scipost/templates/scipost/contributors_filter.html

diff --git a/scipost/forms.py b/scipost/forms.py
index 854726e93..a1a08bda2 100644
--- a/scipost/forms.py
+++ b/scipost/forms.py
@@ -188,6 +188,24 @@ class DraftInvitationForm(forms.ModelForm):
         return invitation_type
 
 
+class ContributorsFilterForm(forms.Form):
+    names = forms.CharField(widget=forms.Textarea())
+
+    def filter(self):
+        names_found = []
+        names_not_found = []
+        r = self.cleaned_data['names'].replace('\r', '\n').split('\n')
+        for name in r:
+            last_name = name.split(',')[0]
+            if not last_name:
+                continue
+            if Contributor.objects.filter(user__last_name__istartswith=last_name).exists():
+                names_found.append(name)
+            else:
+                names_not_found.append(name)
+        return names_found, names_not_found
+
+
 class RegistrationInvitationForm(forms.ModelForm):
     cited_in_submission = AutoCompleteSelectField('submissions_lookup', required=False)
     cited_in_publication = AutoCompleteSelectField('publication_lookup', required=False)
diff --git a/scipost/templates/partials/scipost/personal_page/editorial_actions.html b/scipost/templates/partials/scipost/personal_page/editorial_actions.html
index ffd9844f5..c614b2e48 100644
--- a/scipost/templates/partials/scipost/personal_page/editorial_actions.html
+++ b/scipost/templates/partials/scipost/personal_page/editorial_actions.html
@@ -21,6 +21,7 @@
                 <li><a href="{% url 'scipost:registration_requests' %}">Awaiting validation</a> ({{ nr_reg_awaiting_validation }})</li>
             {% endif %}
             {% if perms.scipost.can_draft_registration_invitations %}
+                <li><a href="{% url 'scipost:contributors_filter' %}">Contributors filter</a></li>
                 <li><a href="{% url 'scipost:draft_registration_invitation' %}">Draft a Registration Invitation</a></li>
             {% endif %}
             {% if perms.scipost.can_manage_registration_invitations %}
diff --git a/scipost/templates/scipost/contributors_filter.html b/scipost/templates/scipost/contributors_filter.html
new file mode 100644
index 000000000..2c32cc28c
--- /dev/null
+++ b/scipost/templates/scipost/contributors_filter.html
@@ -0,0 +1,42 @@
+{% extends 'scipost/_personal_page_base.html' %}
+
+{% load bootstrap %}
+
+{% block pagetitle %}: contributors filter{% endblock pagetitle %}
+
+{% block breadcrumb_items %}
+    {{block.super}}
+    <a href="{% url 'scipost:draft_registration_invitation' %}" class="breadcrumb-item">Draft registration invitation</a>
+    <span class="breadcrumb-item">Contributors filter</span>
+{% endblock %}
+
+{% block content %}
+
+<div class="row">
+    <div class="col-12">
+        <h1 class="highlight">Contributors filter</h1>
+        <p>This form can be used to split your list of names into a list of names with registered or already invited Contributors and a list of unknown names according to the current database.</p>
+        <p>Please, for every name use the format <code>{last name}</code> or  <code>{last name}, {first name}</code> and use one name per line.</p>
+        <form method="post">
+              {% csrf_token %}
+              {{ form|bootstrap }}
+              <input type="submit" class="btn btn-primary" value="Filter"/>
+        </form>
+    </div>
+</div>
+
+{% if form.is_bound %}
+    <hr class="divider">
+    <h2>Filter result</h2>
+    {% if names_not_found %}
+        <h3>New names</h3>
+        <pre><code>{% for name in names_not_found %}{{ name }}{% if not forloop.last %}<br>{% endif %}{% endfor %}</code></pre>
+    {% endif %}
+    <br>
+    {% if names_found %}
+        <h3>Names found in the system</h3>
+        <pre><code>{% for name in names_found %}{{ name }}{% if not forloop.last %}<br>{% endif %}{% endfor %}</code></pre>
+    {% endif %}
+{% endif %}
+
+{% endblock %}
diff --git a/scipost/templates/scipost/draft_registration_invitation.html b/scipost/templates/scipost/draft_registration_invitation.html
index f92638fd4..4f8900f25 100644
--- a/scipost/templates/scipost/draft_registration_invitation.html
+++ b/scipost/templates/scipost/draft_registration_invitation.html
@@ -6,7 +6,7 @@
 
 {% block breadcrumb_items %}
     {{block.super}}
-    <span class="breadcrumb-item">Pool</span>
+    <span class="breadcrumb-item">Draft registration invitation</span>
 {% endblock %}
 
 {% block content %}
@@ -35,6 +35,7 @@ $(document).ready(function(){
 <div class="row">
     <div class="col-12">
         <h1 class="highlight">Draft a registration invitation</h1>
+        <p>If you have a list of names you want to check with the current database of users, <a href="{% url 'scipost:contributors_filter' %}">please click here</a>.</p>
     </div>
 </div>
 
diff --git a/scipost/urls.py b/scipost/urls.py
index 3c8fb0216..9ab0cf02b 100644
--- a/scipost/urls.py
+++ b/scipost/urls.py
@@ -89,6 +89,7 @@ urlpatterns = [
         views.registration_invitations, name="registration_invitations"),
     url(r'^draft_registration_invitation$',
         views.draft_registration_invitation, name="draft_registration_invitation"),
+    url(r'^contributors_filter$', views.contributors_filter, name="contributors_filter"),
     url(r'^edit_draft_reg_inv/(?P<draft_id>[0-9]+)$',
         views.edit_draft_reg_inv, name="edit_draft_reg_inv"),
     url(r'^map_draft_reg_inv_to_contributor/(?P<draft_id>[0-9]+)/(?P<contributor_id>[0-9]+)$',
diff --git a/scipost/views.py b/scipost/views.py
index f56568717..64fd94a57 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -36,7 +36,8 @@ from .forms import AuthenticationForm, DraftInvitationForm, UnavailabilityPeriod
                    RegistrationForm, RegistrationInvitationForm, AuthorshipClaimForm,\
                    ModifyPersonalMessageForm, SearchForm, VetRegistrationForm, reg_ref_dict,\
                    UpdatePersonalDataForm, UpdateUserDataForm, PasswordChangeForm,\
-                   EmailGroupMembersForm, EmailParticularForm, SendPrecookedEmailForm
+                   EmailGroupMembersForm, EmailParticularForm, SendPrecookedEmailForm,\
+                   ContributorsFilterForm
 from .utils import Utils, EMAIL_FOOTER, SCIPOST_SUMMARY_FOOTER, SCIPOST_SUMMARY_FOOTER_HTML
 
 from affiliations.forms import AffiliationsFormset
@@ -378,8 +379,33 @@ def draft_registration_invitation(request):
     return render(request, 'scipost/draft_registration_invitation.html', context)
 
 
+@permission_required('scipost.can_draft_registration_invitations', return_403=True)
+def contributors_filter(request):
+    """
+    For Invitation Officers that use lists of scientists as a to-do. This
+    view returns all entries of those lists with users that are certainly not registered
+    or invitated.
+    """
+    names_found = names_not_found = None
+    form = ContributorsFilterForm(request.POST or None)
+    if form.is_valid():
+        names_found, names_not_found = form.filter()
+        # messages.success(request, 'Draft invitation saved.')
+        # return redirect(reverse('scipost:draft_registration_invitation'))
+
+    context = {
+        'form': form,
+        'names_found': names_found,
+        'names_not_found': names_not_found,
+    }
+    return render(request, 'scipost/contributors_filter.html', context)
+
+
 @login_required
 def edit_draft_reg_inv(request, draft_id):
+    """
+    Edit DraftInvitation instance. It's only possible to edit istances created by the User itself.
+    """
     draft = get_object_or_404((get_objects_for_user(request.user, 'scipost.change_draftinvitation')
                                .filter(processed=False)),
                               id=draft_id)
-- 
GitLab