Improvements to search page
authorCharles Connell <charles@connells.org>
Thu, 2 Jan 2014 03:38:34 +0000 (22:38 -0500)
committerCharles Connell <charles@connells.org>
Thu, 2 Jan 2014 03:42:56 +0000 (22:42 -0500)
karmaworld/apps/notes/management/commands/populate_indexden.py
karmaworld/apps/notes/search.py
karmaworld/apps/notes/views.py
karmaworld/assets/css/search.css
karmaworld/templates/header.html
karmaworld/templates/notes/note_list_entry.html
karmaworld/templates/notes/search_results.html

index 421c0cc5cb68bddfe2a0de8a8dc03fa7c58df299..d80106ca56373a3ef4bf42737ce681c9df7624da 100644 (file)
@@ -2,6 +2,7 @@
 # -*- coding:utf8 -*-
 # Copyright (C) 2013  FinalsClub Foundation
 
+import traceback
 from django.core.management.base import BaseCommand
 from karmaworld.apps.notes.models import Note
 from karmaworld.apps.notes.search import *
@@ -19,6 +20,7 @@ class Command(BaseCommand):
             try:
                 print "Indexing {n}".format(n=note)
                 add_document(note)
-            except:
+            except Exception, e:
+                traceback.print_exc()
                 continue
 
index e08c3b9f79fc49f6628fd44da9f9d9f5787ab0a7..afff889c2483ccdd580293b22a61a95f6f861edc 100644 (file)
@@ -44,10 +44,12 @@ def search(query, course_id=None):
     """Returns note IDs matching the given query,
     filtered by course ID if given"""
     if course_id:
-        results = index.search('(text:"%s" OR name:"%s") AND course_id:%s' % (query, query, course_id))
+        real_query = '("%s" OR name:"%s") AND course_id:%s' % (query, query, course_id)
     else:
-        results = index.search('text:"%s" OR name:"%s"' % (query, query))
+        real_query = '"%s" OR name:"%s"' % (query, query)
 
-    matching_note_ids = [r['docid'] for r in results['results']]
+    raw_results = index.search(real_query, snippet_fields=['text'])
 
-    return matching_note_ids
\ No newline at end of file
+    results = {r['docid']: r['snippet_text'] for r in raw_results['results']}
+
+    return results
\ No newline at end of file
index b8a33763d1c6fb65c8df1df1aa588a30a1d532aa..bc43cb22bbdfb7643b9184d97a0dfe1fca7da6a1 100644 (file)
@@ -158,12 +158,17 @@ class NoteSearchView(ListView):
             return Note.objects.none()
 
         if 'course_id' in self.request.GET:
-            matching_note_ids = search.search(self.request.GET['query'],
+            matching_notes = search.search(self.request.GET['query'],
                                               self.request.GET['course_id'])
         else:
-            matching_note_ids = search.search(self.request.GET['query'])
+            matching_notes = search.search(self.request.GET['query'])
 
-        return Note.objects.filter(id__in=matching_note_ids)
+        instances = Note.objects.filter(id__in=matching_notes.viewkeys())
+        results = []
+        for i in instances:
+            results.append((i, matching_notes[str(i.id)]))
+
+        return results
 
     def get_context_data(self, **kwargs):
         if 'query' in self.request.GET:
index 4eb45e3d8bd856ff7542686b7162b5d84eb22a69..821f1103197443fd9c0ed6144a9cfcb6d26a86d3 100644 (file)
@@ -3,3 +3,11 @@
   padding-bottom: 20px;
   margin-bottom: 20px;
 }
+
+#no_results
+{
+  font-family: "MuseoSlab-900";
+  font-size: 10px;
+  color: #8e8e8e;
+  margin-bottom: 20px;
+}
\ No newline at end of file
index e47245fe487c74656f2bb5bd4c0a266f023956a3..f151af51ed66c35f6e023f4d1391472fda7a698c 100644 (file)
@@ -2,22 +2,25 @@
 
 <header id="global_header">
   <div class="row">
-    <div id="logo_container" class="small-6 large-3 columns">
-      <a href="/">
-        <img src="{{ STATIC_URL }}img/global_header_logo.png" alt="global_header_logo" width="144" height="17" />
-      </a>
-    </div><!--/logo container-->
-
-    <div class="small-4 columns show-for-medium-up">
-      {% if course.id %}
-        <div id="search_container">
-          <form action="{% url 'note_search' %}" method="GET">
-            <input type="text" id="search_txt" name="query" placeholder="Search in this course">
-            <input type="hidden" name="course_id" value="{{ course.id }}">
-          </form>
+    <div id="logo_container" class="small-5 columns">
+      <div class="row">
+        <div class="small-5 columns">
+          <a href="/">
+            <img src="{{ STATIC_URL }}img/global_header_logo.png" alt="global_header_logo" width="144" height="17" />
+          </a>
         </div>
-      {% endif %}
-    </div>
+        <div class="small-7 columns">
+          {% if course.id %}
+            <div id="search_container">
+              <form action="{% url 'note_search' %}" method="GET">
+                <input type="text" id="search_txt" name="query" placeholder="Search in this course">
+                <input type="hidden" name="course_id" value="{{ course.id }}">
+              </form>
+            </div>
+          {% endif %}
+        </div>
+      </div>
+    </div><!--/logo container-->
 
     {% if display_add_course %}
       <div class="small-2 columns small-offset-3 show-for-medium-up">
@@ -32,5 +35,9 @@
       </div>
     {% endif %}
 
+    <div id="login_container" class="small-1 columns">
+      <a class=white href="{% url 'about' %}">About</a>
+    </div>
+
   </div>
 </header><!--/global header-->
index 1b9702e63b62f31a62c261f53bf6442e3917b20a..627a7c85a120f217054f35b92bfc5d853bd93251 100644 (file)
 
 
     <div class="note_preview">
-      {{ note.text|slice:":500" }} &hellip;
+      {% if note_preview %}
+        {{ note_preview|safe }} &hellip;
+      {% else %}
+        {{ note.text|slice:":500" }} &hellip;
+      {% endif %}
     </div>
 
 
index 7792f8e2353ba42a1ca29e624e88b83843c387c7..d1ded580ab8d3139b84394f152f31200576e72e0 100644 (file)
 <section id="results_content">
 
   <div id="results_header" class="hero_gradient_bar">
+
+    <div class="row">
+      <div class="twelve columns header_subhead">
+        <a href="{{ course.get_absolute_url}}">
+          <i class="fa fa-arrow-left"></i>&nbsp;back to {{ course.name }}
+        </a>
+      </div>
+    </div>
+
     <div class="row">
       <div class="small-12 columns header_subhead">
         YOU SEARCHED FOR
 
   <div id="results_container">
     <div class="row">
-      <div class="small-12 columns large-10 large-offset-1">
-        {% for note in object_list %}
-          {% include 'notes/note_list_entry.html' with note=note %}
-        {% endfor %}
-      </div>
+      {% if object_list %}
+        <div class="small-12 columns large-10 large-offset-1">
+          {% for tuple in object_list %}
+            {% include 'notes/note_list_entry.html' with note=tuple.0 note_preview=tuple.1 %}
+          {% endfor %}
+        </div>
+        {% else %}
+          <div id="no_results" class="small-12 columns center column">
+            <h4>Sorry! No results were found.</h4>
+          </div>
+        {% endif %}
     </div>
   </div><!-- /course_container -->