From 515c7506f70ea5b98b99fc7cc11e0a39841bf020 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Tue, 13 Feb 2018 21:07:56 +0100
Subject: [PATCH] Include invitations in the Contributorsfilter

---
 scipost/forms.py                                   |  9 ++++++++-
 scipost/managers.py                                |  3 +++
 scipost/templates/scipost/contributors_filter.html | 10 +++++++---
 scipost/views.py                                   |  7 +++----
 4 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/scipost/forms.py b/scipost/forms.py
index e52294ab5..60c748382 100644
--- a/scipost/forms.py
+++ b/scipost/forms.py
@@ -186,20 +186,27 @@ class DraftInvitationForm(forms.ModelForm):
 
 class ContributorsFilterForm(forms.Form):
     names = forms.CharField(widget=forms.Textarea())
+    include_invitations = forms.BooleanField(required=False, initial=True,
+                                             label='Include invitations in the filter.')
 
     def filter(self):
         names_found = []
         names_not_found = []
+        invitations_found = []
         r = self.cleaned_data['names'].replace('\r', '\n').split('\n')
+        include_invitations = self.cleaned_data.get('include_invitations', False)
         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)
+            elif include_invitations and RegistrationInvitation.objects.pending_response().filter(
+              last_name__istartswith=last_name).exists():
+                invitations_found.append(name)
             else:
                 names_not_found.append(name)
-        return names_found, names_not_found
+        return names_found, names_not_found, invitations_found
 
 
 class RegistrationInvitationForm(forms.ModelForm):
diff --git a/scipost/managers.py b/scipost/managers.py
index 5ed6efd72..72557dcef 100644
--- a/scipost/managers.py
+++ b/scipost/managers.py
@@ -49,6 +49,9 @@ class RegistrationInvitationManager(models.Manager):
     def declined(self):
         return self.filter(responded=True, declined=True)
 
+    def pending_response(self):
+        return self.filter(responded=False)
+
     def declined_or_without_response(self):
         return self.filter(Q(responded=True, declined=True) | Q(responded=False))
 
diff --git a/scipost/templates/scipost/contributors_filter.html b/scipost/templates/scipost/contributors_filter.html
index eedc758ad..12418bd8a 100644
--- a/scipost/templates/scipost/contributors_filter.html
+++ b/scipost/templates/scipost/contributors_filter.html
@@ -33,13 +33,17 @@
     <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>
+        <pre class="mb-3"><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>
+        <pre class="mb-3"><code>{% for name in names_found %}{{ name }}{% if not forloop.last %}<br>{% endif %}{% endfor %}</code></pre>
+    {% endif %}
 
+    {% if invitations_found %}
+        <h3>Invitations (pending response) found in database</h3>
+        <pre class="mb-3"><code>{% for name in invitations_found %}{{ name }}{% if not forloop.last %}<br>{% endif %}{% endfor %}</code></pre>
     {% endif %}
 {% endif %}
 
diff --git a/scipost/views.py b/scipost/views.py
index a5623844c..b9d8656f9 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -386,17 +386,16 @@ def contributors_filter(request):
     view returns all entries of those lists with users that are certainly not registered
     or invitated.
     """
-    names_found = names_not_found = None
+    names_found = names_not_found = invitations_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'))
+        names_found, names_not_found, invitations_found = form.filter()
 
     context = {
         'form': form,
         'names_found': names_found,
         'names_not_found': names_not_found,
+        'invitations_found': invitations_found,
     }
     return render(request, 'scipost/contributors_filter.html', context)
 
-- 
GitLab