From c94096616183dfc4f603802c9131c81f3bef9a8d Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sun, 28 Oct 2018 08:09:02 +0100
Subject: [PATCH] Add filter in Topic list

---
 ontology/templates/ontology/topic_list.html | 12 ++++++++++--
 ontology/views.py                           | 17 +++++++++++++++++
 profiles/forms.py                           |  4 ----
 profiles/views.py                           |  3 ++-
 scipost/forms.py                            |  7 +++++++
 5 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/ontology/templates/ontology/topic_list.html b/ontology/templates/ontology/topic_list.html
index b61a206ab..ccac22f92 100644
--- a/ontology/templates/ontology/topic_list.html
+++ b/ontology/templates/ontology/topic_list.html
@@ -12,11 +12,19 @@
 <div class="row">
   <div class="col-12">
     <h3 class="highlight">Topics</h3>
-    {% if perms.scipost.can_manage_ontology %}
     <ul>
+      {% if perms.scipost.can_manage_ontology %}
       <li><a href="{% url 'ontology:topic_create' %}">Add a Topic</a></li>
+      {% endif %}
+      <li>
+	<ul class="list-inline">
+	  <li class="list-inline-item">Filter to Topic name containing:</li>
+	  <li class="list-inline-item">
+	    <form action="" method="get">{{ searchform }}
+	  </li>
+	  <li class="list-inline-item"><input class="btn btn-outline-secondary" type="submit" value="Submit"></form></li>
+      </ul>
     </ul>
-    {% endif %}
   </div>
 </div>
 
diff --git a/ontology/views.py b/ontology/views.py
index 1dc5b8673..def72669f 100644
--- a/ontology/views.py
+++ b/ontology/views.py
@@ -12,6 +12,7 @@ from django.views.generic.list import ListView
 
 from .models import Topic, RelationAsym, RelationSym
 
+from scipost.forms import SearchTextForm
 from scipost.mixins import PaginationMixin, PermissionsMixin
 
 
@@ -45,6 +46,22 @@ class TopicListView(PaginationMixin, ListView):
     model = Topic
     paginate_by = 25
 
+    def get_queryset(self):
+        """
+        Return a queryset of Topics using optional GET data.
+        """
+        queryset = Topic.objects.all()
+        if self.request.GET.get('text'):
+            queryset = queryset.filter(name__icontains=self.request.GET['text'])
+        return queryset
+
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data(**kwargs)
+        context.update({
+            'searchform': SearchTextForm(initial={'text': self.request.GET.get('text')}),
+        })
+        return context
+
 
 class TopicDetailView(DetailView):
     model = Topic
diff --git a/profiles/forms.py b/profiles/forms.py
index e8dd84e0d..ea6a53815 100644
--- a/profiles/forms.py
+++ b/profiles/forms.py
@@ -57,7 +57,3 @@ class ProfileEmailForm(forms.ModelForm):
         """Save to a profile."""
         self.instance.profile = self.profile
         return super().save()
-
-
-class SearchTextForm(forms.Form):
-    text = forms.CharField(label='')
diff --git a/profiles/views.py b/profiles/views.py
index d447b1676..4355197be 100644
--- a/profiles/views.py
+++ b/profiles/views.py
@@ -15,12 +15,13 @@ from guardian.decorators import permission_required
 from scipost.constants import SCIPOST_SUBJECT_AREAS
 from scipost.mixins import PermissionsMixin, PaginationMixin
 from scipost.models import Contributor
+from scipost.forms import SearchTextForm
 
 from invitations.models import RegistrationInvitation
 from submissions.models import RefereeInvitation
 
 from .models import Profile, ProfileEmail
-from .forms import ProfileForm, ProfileEmailForm, SearchTextForm
+from .forms import ProfileForm, ProfileEmailForm
 
 
 
diff --git a/scipost/forms.py b/scipost/forms.py
index 8dfebf75a..3a42e63fe 100644
--- a/scipost/forms.py
+++ b/scipost/forms.py
@@ -431,6 +431,13 @@ def get_date_filter_choices():
     return months, years
 
 
+class SearchTextForm(forms.Form):
+    """
+    Simple text-based search form.
+    """
+    text = forms.CharField(label='')
+
+
 class SearchForm(HayStackSearchForm):
     # The date filters don't function well...
     start = forms.DateField(widget=MonthYearWidget(), required=False)  # Month
-- 
GitLab