From 07f1b8c4a582968f5d428910c092d071dda599fe Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sat, 16 Apr 2016 09:13:03 +0200
Subject: [PATCH] Implement global search box in navbar using context processor

---
 SciPost_v1/settings.py                       |  1 +
 comments/models.py                           |  6 +-
 scipost/context_processors.py                |  5 ++
 scipost/forms.py                             |  8 +--
 scipost/static/scipost/SciPost.css           | 10 +--
 scipost/templates/scipost/index.html         |  1 +
 scipost/templates/scipost/navbar.html        | 14 +++--
 scipost/templates/scipost/personal_page.html |  1 +
 scipost/templates/scipost/search.html        | 61 +++++++++++++++++++
 scipost/urls.py                              |  4 ++
 scipost/views.py                             | 64 +++++++++++++++++++-
 11 files changed, 155 insertions(+), 20 deletions(-)
 create mode 100644 scipost/context_processors.py
 create mode 100644 scipost/templates/scipost/search.html

diff --git a/SciPost_v1/settings.py b/SciPost_v1/settings.py
index c1ed48f31..f6b56a028 100644
--- a/SciPost_v1/settings.py
+++ b/SciPost_v1/settings.py
@@ -105,6 +105,7 @@ TEMPLATES = [
                 'django.template.context_processors.request',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
+                'scipost.context_processors.searchform',
             ],
         },
     },
diff --git a/comments/models.py b/comments/models.py
index 2c984729b..08f03ec21 100644
--- a/comments/models.py
+++ b/comments/models.py
@@ -219,11 +219,11 @@ class Comment(models.Model):
             context['submission_title'] = self.submission.title
             context['submission_author_list'] = self.submission.author_list
         if self.commentary is not None:
-            header += ('<a href="/commentary/{{ commentary_id }}#comment_id{{ id }}"> \"{{ text_cut }}\"</a>' +
+            header += ('<a href="/commentary/{{ commentary_url }}#comment_id{{ id }}"> \"{{ text_cut }}\"</a>' +
                        '<p>submitted on {{ date_submitted }}')
-            header += (' in commentary on <a href="/commentary/{{ commentary_id }}" class="pubtitleli">' + 
+            header += (' in commentary on <a href="/commentary/{{ commentary_url }}" class="pubtitleli">' + 
                        '{{ commentary_pub_title }}</a> by {{ commentary_author_list }}</p>')
-            context['commentary_id'] = self.commentary.id
+            context['commentary_url'] = self.commentary.arxiv_or_DOI_string
             context['commentary_pub_title'] = self.commentary.pub_title
             context['commentary_author_list'] = self.commentary.author_list
         if self.thesislink is not None:
diff --git a/scipost/context_processors.py b/scipost/context_processors.py
new file mode 100644
index 000000000..19c0fbf68
--- /dev/null
+++ b/scipost/context_processors.py
@@ -0,0 +1,5 @@
+from .forms import SearchForm
+
+
+def searchform(request):
+    return {'search_form': SearchForm()}
diff --git a/scipost/forms.py b/scipost/forms.py
index 8d716a68c..ebc730a16 100644
--- a/scipost/forms.py
+++ b/scipost/forms.py
@@ -106,12 +106,8 @@ class AuthorshipClaimForm(forms.Form):
 #        fields = ['relevance', 'importance', 'clarity', 'validity', 'rigour', 'originality', 'significance']
 
 
-
-class DocumentsSearchForm(forms.Form):
-    author = forms.CharField(max_length=100, required=False, label="Author(s)")
-    title_keyword = forms.CharField(max_length=100, label="Title", required=False)
-    abstract_keyword = forms.CharField(max_length=1000, required=False, label="Abstract")
-
+class SearchForm(forms.Form):
+    query = forms.CharField(max_length=100, label='')
 
 
 class CreateListForm(forms.ModelForm):
diff --git a/scipost/static/scipost/SciPost.css b/scipost/static/scipost/SciPost.css
index 766629572..d1ea8d694 100644
--- a/scipost/static/scipost/SciPost.css
+++ b/scipost/static/scipost/SciPost.css
@@ -52,21 +52,21 @@ hr.hr12 {
 ul.personalTabMenu {
   background-color: #dddddd;
   display: inline-block;
-  font-size: 12px;
+  font-size: 11px;
   padding: 0px;
 }
 ul.personalTabMenu li {
   display: inline-block;
   margin: 0px;
-  padding: 10px;
+  padding: 4px;
 }
 ul.personalTabMenu li a {
-  padding: 5px;
+  padding: 4px;
 }
 ul.personalTabMenu li a.active {
   background-color: #eeeeee;
   border: 1px solid black;
-  padding: 5px;
+  padding: 4px;
 }
 ul.personalTabMenu li a.inactive {
   background-color: #dddddd;
@@ -282,7 +282,7 @@ nav {
   padding:0px;
   float:center;
   clear:both;
-  font-size: 14px;
+  font-size: 12px;
   font-weight: 500;
 }
 nav #loggedinas {
diff --git a/scipost/templates/scipost/index.html b/scipost/templates/scipost/index.html
index 67385cfdd..cce12c3bd 100644
--- a/scipost/templates/scipost/index.html
+++ b/scipost/templates/scipost/index.html
@@ -17,6 +17,7 @@
       <h3><a href="{% url 'scipost:FAQ' %}">Frequently asked questions</a></h3>
       <h3><a href="{% url 'scipost:about' %}">Read more</a></h3>
     </div>
+
     <div class="flex-greybox320">
       <h1><a href="{% url 'journals:journals' %}">Journals</a></h1>
       <p id="journalsannouncesmall">OPEN FOR SUBMISSION FROM JUNE 2016</p>
diff --git a/scipost/templates/scipost/navbar.html b/scipost/templates/scipost/navbar.html
index b32713467..f38ed3745 100644
--- a/scipost/templates/scipost/navbar.html
+++ b/scipost/templates/scipost/navbar.html
@@ -1,6 +1,6 @@
 <nav>
   <div class="row">
-    <div class="col-6">
+    <div class="col-5">
       <a href="{% url 'scipost:index' %}" {% if request.path == '/' %}class="highlighted"{% endif %}>Home</a>
       <!-- Temporary strip for online version -->
       {% if user.is_authenticated %}
@@ -10,15 +10,21 @@
       <a href="{% url 'theses:theses' %}" {% if "/theses/" in request.path %}class="highlighted"{% endif %}>Theses</a>
       {% endif %}
     </div>
-    <div class="col-6">
+    <div class="col-4">
       {% if user.is_authenticated %}
       <p id="loggedinas"><em>Logged in as {{ user.username }}</em></p>
       <a href="{% url 'scipost:logout' %}">Logout</a>
       <a href="{% url 'scipost:personal_page' %}" {% if "/personal_page" in request.path %}class="highlighted"{% endif %}>Personal Page</a>
       {% else %}
       <a href="{% url 'scipost:login' %}">Login</a>
-      {% endif %}
-      
+      {% endif %}      
+    </div>
+    <div class="col-3">
+      <form action="{% url 'scipost:search' %}" method="post">
+	{% csrf_token %}
+	{{ search_form }}
+	<input type="submit" value="Search"/>
+      </form>
     </div>
   </div>
 </nav>
diff --git a/scipost/templates/scipost/personal_page.html b/scipost/templates/scipost/personal_page.html
index 50e98fda0..ebad13dc3 100644
--- a/scipost/templates/scipost/personal_page.html
+++ b/scipost/templates/scipost/personal_page.html
@@ -100,6 +100,7 @@
   </div>
 
   <ul class="personalTabMenu">
+
     <li><a class="TabItem" id="AccountTab">Account</a></li>
     {% if request.user|is_in_group:'SciPost Administrators' or request.user|is_in_group:'Editorial College' or request.user|is_in_group:'Vetting Editors' %}
     <li><a class="TabItem" id="EdActionTab">Editorial Actions</a></li>
diff --git a/scipost/templates/scipost/search.html b/scipost/templates/scipost/search.html
new file mode 100644
index 000000000..979100b79
--- /dev/null
+++ b/scipost/templates/scipost/search.html
@@ -0,0 +1,61 @@
+{% extends 'scipost/base.html' %}
+
+{% block pagetitle %}: list{% endblock pagetitle %}
+
+{% block headsup %}
+
+{% endblock headsup %}
+
+{% block bodysup %}
+
+
+<section>
+  <h1>Search results</h1>
+
+  {% if commentary_search_list %}
+  <br />
+  <hr class="hr12">
+  <h3>Commentaries:</h3>
+  <ul>
+    {% for commentary in commentary_search_list %}
+    {{ commentary.header_as_li }}
+    {% endfor %}
+  </ul>
+  {% endif %}
+
+  {% if submission_search_list %}
+  <br />
+  <hr class="hr12">
+  <h3>Submissions:</h3>
+  <ul>
+    {% for submission in submission_search_list %}
+    {{ submission.header_as_li }}
+    {% endfor %}
+  </ul>
+  {% endif %}
+
+  {% if thesislink_search_list %}
+  <br />
+  <hr class="hr12">
+  <h3>Theses:</h3>
+  <ul>
+    {% for thesislink in thesislink_search_list %}
+    {{ thesislink.header_as_li }}
+    {% endfor %}
+  </ul>
+  {% endif %}
+
+  {% if comment_search_list %}
+  <br />
+  <hr class="hr12">
+  <h3>Comments:</h3>
+  <ul>
+    {% for comment in comment_search_list %}
+    {{ comment.header_as_li }}
+    {% endfor %}
+  </ul>
+  {% endif %}
+
+</section>
+
+{% endblock bodysup %}
diff --git a/scipost/urls.py b/scipost/urls.py
index a2ea4128b..5cc396ffc 100644
--- a/scipost/urls.py
+++ b/scipost/urls.py
@@ -17,6 +17,10 @@ urlpatterns = [
     #url(r'^description$', views.description, name='description'),
     url(r'^peer_witnessed_refereeing$', TemplateView.as_view(template_name='scipost/peer_witnessed_refereeing.html'), name='peer_witnessed_refereeing'),
 
+    # Search 
+    url(r'^search$', views.search, name='search'),
+
+
     ################
     # Contributors:
     ################
diff --git a/scipost/views.py b/scipost/views.py
index 6d8375a89..70cb014e3 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -51,6 +51,64 @@ def is_VE(user):
     return user.groups.filter(name='Vetting Editors').exists()
 
 
+# Global search
+
+#def documentsSearchResults(title_keyword, author, abstract_keyword):
+def documentsSearchResults(query):
+    """
+    Searches through commentaries, submissions and thesislinks.
+    Returns a Context object which can be further used in templates.
+    """
+    keyword = query
+    title_keyword = query
+    author = query
+    abstract_keyword = query
+    commentary_search_list = Commentary.objects.filter(
+        pub_title__icontains=title_keyword,
+        author_list__icontains=author,
+        pub_abstract__icontains=abstract_keyword,
+        vetted=True,
+        )
+    commentary_search_list.order_by('-pub_date')
+    submission_search_list = Submission.objects.filter(
+        title__icontains=title_keyword,
+        author_list__icontains=author,
+        abstract__icontains=abstract_keyword,
+        status__gte=1,
+        )
+    submission_search_list.order_by('-pub_date')
+    thesislink_search_list = ThesisLink.objects.filter(
+        title__icontains=title_keyword,
+        author__icontains=author,
+        abstract__icontains=abstract_keyword,
+#        supervisor__icontains=supervisor,
+        vetted=True,
+        )
+    thesislink_search_list.order_by('-pub_date')
+    comment_search_list = Comment.objects.filter(
+        comment_text__icontains=keyword,
+        status__gte='1',
+        )
+    comment_search_list.order_by('-pub_date')
+    context = {'commentary_search_list': commentary_search_list,
+               'submission_search_list': submission_search_list,
+               'thesislink_search_list': thesislink_search_list,
+               'comment_search_list': comment_search_list}
+    return context
+
+
+def search(request):
+    if request.method == 'POST':
+        form = SearchForm(request.POST)
+        if form.is_valid():
+            context = documentsSearchResults(form.cleaned_data['query'])
+        else:
+            context = {}
+    else:
+        context = {}
+    return render(request, 'scipost/search.html', context)
+
+
 #############
 # Main view
 #############
@@ -61,7 +119,8 @@ def index(request):
     thesislink_search_form = ThesisLinkSearchForm(request.POST)
     context = {'submission_search_form': submission_search_form, 
                'commentary_search_form': commentary_search_form, 
-               'thesislink_search_form': thesislink_search_form}
+               'thesislink_search_form': thesislink_search_form,
+               }
     return render(request, 'scipost/index.html', context)
 
 ###############
@@ -366,7 +425,8 @@ def personal_page(request):
         teams = Team.objects.filter(members__in=[contributor])
         graphs_owned = Graph.objects.filter(owner=contributor)
         graphs_private = Graph.objects.filter(Q(teams_with_access__leader=contributor) | Q(teams_with_access__members__in=[contributor]))
-        context = {'contributor': contributor, 'nr_reg_to_vet': nr_reg_to_vet, 
+        context = {'contributor': contributor, 
+                   'nr_reg_to_vet': nr_reg_to_vet, 
                    'nr_reg_awaiting_validation': nr_reg_awaiting_validation, 
                    'nr_commentary_page_requests_to_vet': nr_commentary_page_requests_to_vet, 
                    'nr_comments_to_vet': nr_comments_to_vet, 
-- 
GitLab