From 9d11d4834fa73f76cab152c14a365aa4351dc6e6 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Mon, 26 Apr 2021 20:33:07 +0200
Subject: [PATCH] Add last name search to FellowshipListView

---
 .../templates/colleges/fellowship_list.html   | 24 ++++++++++++++++++-
 colleges/views.py                             |  9 +++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/colleges/templates/colleges/fellowship_list.html b/colleges/templates/colleges/fellowship_list.html
index 4fdf63090..1c0e007cd 100644
--- a/colleges/templates/colleges/fellowship_list.html
+++ b/colleges/templates/colleges/fellowship_list.html
@@ -65,8 +65,30 @@
     </tbody>
   </table>
 
+  <div class="mt-2">
+    <ul class="list-inline">
+      <li class="list-inline-item">Last name startswith:</li>
+      <li class="list-inline-item">
+	<form action="" method="get">{{ searchform }}
+	  {% if request.GET.field %}
+	    <input type="hidden" name="field" value="{{ request.GET.field }}">
+	    {% if request.GET.specialty %}
+	      <input type="hidden" name="specialty" value="{{ request.GET.specialty }}">
+	    {% endif %}
+	  {% endif %}
+	  <input class="btn btn-outline-secondary" type="submit" value="Search">
+	</form>
+      </li>
+    </ul>
+  </div>
+
   {% if view.kwargs.acad_field %}
-    <h3>Fellowships {% if view.kwargs.acad_field %}in {{ view.kwargs.acad_field }}{% if view.kwargs.specialty %} ({{ view.kwargs.specialty }}){% endif %}{% endif %}:</h3>
+    <h3>Fellowships
+      {% if view.kwargs.acad_field %}in {{ view.kwargs.acad_field }}
+	{% if view.kwargs.specialty %} ({{ view.kwargs.specialty }}){% endif %}
+      {% endif %}
+      {% if request.GET.text %} with last name starting with {{ request.GET.text }}{% endif %}
+      :</h3>
     <br/>
   {% endif %}
   <table class="table mt-3">
diff --git a/colleges/views.py b/colleges/views.py
index 45b600d43..751d35b09 100644
--- a/colleges/views.py
+++ b/colleges/views.py
@@ -144,8 +144,17 @@ class FellowshipListView(PermissionsMixin, PaginationMixin, ListView):
                 queryset = queryset.filter(guest=False)
             elif self.request.GET.get('type') == 'guest':
                 queryset = queryset.filter(guest=True)
+        if self.request.GET.get('text'):
+            query = Q_with_alternative_spellings(
+                contributor__profile__last_name__istartswith=self.request.GET['text'])
+            queryset = queryset.filter(query)
         return queryset
 
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data(**kwargs)
+        context['searchform'] = SearchTextForm(initial={'text': self.request.GET.get('text')})
+        return context
+
 
 class FellowshipStartEmailView(PermissionsMixin, MailView):
     """
-- 
GitLab