From be54c5a96f7e546d8c707b94d1f4ca0eb0504732 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Tue, 18 Dec 2018 21:49:03 +0100
Subject: [PATCH] Update Search query

---
 SciPost_v1/settings/base.py                        |  2 ++
 scipost/forms.py                                   | 11 ++++++++++-
 .../static/scipost/assets/config/preconfig.scss    |  2 +-
 submissions/forms.py                               |  4 ++++
 submissions/views.py                               | 14 ++++++--------
 5 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/SciPost_v1/settings/base.py b/SciPost_v1/settings/base.py
index 717768e78..6ab1e2093 100644
--- a/SciPost_v1/settings/base.py
+++ b/SciPost_v1/settings/base.py
@@ -83,6 +83,7 @@ INSTALLED_APPS = (
     'django_extensions',
     'affiliations',
     'ajax_select',
+    'haystack',
     'captcha',
     'colleges',
     'commentaries',
@@ -137,6 +138,7 @@ HAYSTACK_CONNECTIONS = {
         'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
         'PATH': 'local_files/haystack/',
         'EXCLUDED_INDEXES': ['sphinxdoc.search_indexes.DocumentIndex'],
+        'INCLUDE_SPELLING': True,
     },
 }
 
diff --git a/scipost/forms.py b/scipost/forms.py
index f2c7ed99a..ae8cd11d2 100644
--- a/scipost/forms.py
+++ b/scipost/forms.py
@@ -652,7 +652,16 @@ class SearchForm(HayStackSearchForm):
     end = forms.DateField(widget=MonthYearWidget(end=True), required=False)  # Month
 
     def search(self):
-        sqs = super().search()
+        if not self.is_valid():
+            return self.no_query_found()
+
+        if not self.cleaned_data.get("q"):
+            return self.no_query_found()
+
+        sqs = self.searchqueryset.filter(content__contains=self.cleaned_data["q"])
+
+        if self.load_all:
+            sqs = sqs.load_all()
 
         if self.cleaned_data['start']:
             sqs = sqs.filter(date__gte=self.cleaned_data['start'])
diff --git a/scipost/static/scipost/assets/config/preconfig.scss b/scipost/static/scipost/assets/config/preconfig.scss
index fe1ed4f29..411c2f2a1 100644
--- a/scipost/static/scipost/assets/config/preconfig.scss
+++ b/scipost/static/scipost/assets/config/preconfig.scss
@@ -5,7 +5,7 @@
 // General variable structure
 //
 //
-$border-radius-base: 2px;
+$border-radius-base: 1px;
 $border-radius-small: 1px;
 $border-radius-large: 2px;
 
diff --git a/submissions/forms.py b/submissions/forms.py
index 8bba2618c..f65ec848a 100644
--- a/submissions/forms.py
+++ b/submissions/forms.py
@@ -35,6 +35,7 @@ from mails.utils import DirectMailUtil
 from preprints.helpers import generate_new_scipost_identifier
 from preprints.models import Preprint
 from production.utils import get_or_create_production_stream
+from profiles.models import Profile
 from scipost.constants import SCIPOST_SUBJECT_AREAS
 from scipost.services import ArxivCaller
 from scipost.models import Contributor, Remark
@@ -911,6 +912,9 @@ class RefereeSearchForm(forms.Form):
     last_name = forms.CharField(widget=forms.TextInput({
         'placeholder': 'Search for a referee in the SciPost Profiles database'}))
 
+    def search(self):
+        return Profile.objects.filter(last_name__icontains=self.cleaned_data['last_name'])
+
 
 class ConsiderRefereeInvitationForm(forms.Form):
     accept = forms.ChoiceField(widget=forms.RadioSelect, choices=ASSIGNMENT_BOOL,
diff --git a/submissions/views.py b/submissions/views.py
index 8fe120f2a..bcba648b0 100644
--- a/submissions/views.py
+++ b/submissions/views.py
@@ -1,7 +1,7 @@
 __copyright__ = "Copyright 2016-2018, Stichting SciPost (SciPost Foundation)"
 __license__ = "AGPL v3"
 
-import time
+
 import datetime
 import feedparser
 import strings
@@ -925,11 +925,9 @@ def select_referee(request, identifier_w_vn_nr):
                                    preprint__identifier_w_vn_nr=identifier_w_vn_nr)
     context = {}
     queryresults = ''
-    referee_search_form = RefereeSearchForm(request.GET or None)
-    if referee_search_form.is_valid():
-        profiles_found = Profile.objects.filter(
-            last_name__icontains=referee_search_form.cleaned_data['last_name'])
-        context['profiles_found'] = profiles_found
+    form = RefereeSearchForm(request.GET or None)
+    if form.is_valid():
+        context['profiles_found'] = form.search()
         # Check for recent co-authorship (thus referee disqualification)
         try:
             sub_auth_boolean_str = '((' + (submission
@@ -938,7 +936,7 @@ def select_referee(request, identifier_w_vn_nr):
             for author in submission.metadata['entries'][0]['authors'][1:]:
                 sub_auth_boolean_str += '+OR+' + author['name'].split()[-1]
             sub_auth_boolean_str += ')+AND+'
-            search_str = sub_auth_boolean_str + referee_search_form.cleaned_data['last_name'] + ')'
+            search_str = sub_auth_boolean_str + form.cleaned_data['last_name'] + ')'
             queryurl = ('https://export.arxiv.org/api/query?search_query=au:%s'
                         % search_str + '&sortBy=submittedDate&sortOrder=descending'
                         '&max_results=5')
@@ -950,7 +948,7 @@ def select_referee(request, identifier_w_vn_nr):
     context.update({
         'submission': submission,
         'workdays_left_to_report': workdays_between(timezone.now(), submission.reporting_deadline),
-        'referee_search_form': referee_search_form,
+        'referee_search_form': form,
         'queryresults': queryresults,
         'profile_email_form': ProfileEmailForm(initial={'primary': True}),
     })
-- 
GitLab