From 7406deae097f5b93576b6497e7ba82a53599ae34 Mon Sep 17 00:00:00 2001
From: Geert Kapteijns <ghkapteijns@gmail.com>
Date: Wed, 15 Feb 2017 18:09:34 +0100
Subject: [PATCH] Change theses search form method to GET. Extract query logic
 to custom manager.

---
 theses/managers.py                  | 17 +++++++++++++++++
 theses/models.py                    |  4 ++++
 theses/templates/theses/theses.html | 13 ++++++-------
 theses/views.py                     | 28 ++++++++--------------------
 4 files changed, 35 insertions(+), 27 deletions(-)
 create mode 100644 theses/managers.py

diff --git a/theses/managers.py b/theses/managers.py
new file mode 100644
index 000000000..68dc44574
--- /dev/null
+++ b/theses/managers.py
@@ -0,0 +1,17 @@
+from django.db import models
+
+
+class ThesisLinkManager(models.Manager):
+    def search_results(self, form):
+        return self.vetted().filter(
+            title__icontains=form.cleaned_data['title_keyword'],
+            author__icontains=form.cleaned_data['author'],
+            abstract__icontains=form.cleaned_data['abstract_keyword'],
+            supervisor__icontains=form.cleaned_data['supervisor'],
+        ).order_by('-defense_date')
+
+    def latest(self, n):
+        return self.vetted().order_by('latest_activity')[:n]
+
+    def vetted(self):
+        return self.filter(vetted=True)
diff --git a/theses/models.py b/theses/models.py
index 69fb37790..0b5d33375 100644
--- a/theses/models.py
+++ b/theses/models.py
@@ -7,6 +7,8 @@ from scipost.constants import SCIPOST_DISCIPLINES, SCIPOST_SUBJECT_AREAS,\
                               subject_areas_dict, disciplines_dict
 from scipost.models import Contributor
 
+from .managers import ThesisLinkManager
+
 
 class ThesisLink(models.Model):
     MASTER_THESIS = 'MA'
@@ -64,6 +66,8 @@ class ThesisLink(models.Model):
     abstract = models.TextField(verbose_name='abstract, outline or summary')
     latest_activity = models.DateTimeField(default=timezone.now)
 
+    objects = ThesisLinkManager()
+
     def __str__(self):
         return self.title
 
diff --git a/theses/templates/theses/theses.html b/theses/templates/theses/theses.html
index e50560f5f..6abaedb7e 100644
--- a/theses/templates/theses/theses.html
+++ b/theses/templates/theses/theses.html
@@ -20,8 +20,7 @@
     <div class="col-md-4">
         <div class="panel page-header-panel">
           <h2>Search SciPost Theses:</h2>
-          <form class="small" action="{% url 'theses:theses' %}" method="post">
-            {% csrf_token %}
+          <form class="small" action="{% url 'theses:theses' %}" method="get">
             <table>
               {{ form|bootstrap:'4,8,sm' }}
             </table>
@@ -39,14 +38,14 @@
     </div>
   </div>
 
-  {% if thesislink_search_list or form.has_changed %}
+  {% if search_results or form.has_changed %}
   <div class="row">
       <div class="col-12">
         <hr class="hr12">
         <h3>Search results:</h3>
-        {% if thesislink_search_list %}
+        {% if search_results %}
           <ul>
-            {% for thesislink in thesislink_search_list %}
+            {% for thesislink in search_results %}
                 {% include 'theses/_thesislink_header_as_li.html' with thesislink=thesislink %}
             {% endfor %}
           </ul>
@@ -57,13 +56,13 @@
 </div>
 {% endif %}
 
-{% if thesislink_recent_list %}
+{% if recent_theses %}
 <div class="row">
     <div class="col-12">
       <hr class="hr12">
       <h2>Recently active Thesis Links:</h2>
       <ul>
-        {% for thesislink in thesislink_recent_list %}
+        {% for thesislink in recent_theses %}
             {% include 'theses/_thesislink_header_as_li.html' with thesislink=thesislink %}
         {% endfor %}
       </ul>
diff --git a/theses/views.py b/theses/views.py
index a398d8622..b000a4679 100644
--- a/theses/views.py
+++ b/theses/views.py
@@ -87,29 +87,17 @@ class VetThesisLink(UpdateView):
 
 
 def theses(request):
-    if request.method == 'POST':
-        form = ThesisLinkSearchForm(request.POST)
-        if form.is_valid() and form.has_changed():
-            thesislink_search_list = ThesisLink.objects.filter(
-                title__icontains=form.cleaned_data['title_keyword'],
-                author__icontains=form.cleaned_data['author'],
-                abstract__icontains=form.cleaned_data['abstract_keyword'],
-                supervisor__icontains=form.cleaned_data['supervisor'],
-                vetted=True,
-            )
-            thesislink_search_list.order_by('-pub_date')
-        else:
-            thesislink_search_list = []
-
+    form = ThesisLinkSearchForm(request.GET)
+    if form.is_valid() and form.has_changed():
+        search_results = ThesisLink.objects.search_results(form)
+        recent_theses = []
     else:
-        form = ThesisLinkSearchForm()
-        thesislink_search_list = []
+        recent_theses = ThesisLink.objects.latest(5)
+        search_results = []
 
-    thesislink_recent_list = (
-        ThesisLink.objects.filter(vetted=True, latest_activity__gte=timezone.now() + datetime.timedelta(days=-7)))
     context = {
-        'form': form, 'thesislink_search_list': thesislink_search_list,
-        'thesislink_recent_list': thesislink_recent_list
+        'form': form, 'search_results': search_results,
+        'recent_theses': recent_theses
     }
     return render(request, 'theses/theses.html', context)
 
-- 
GitLab