From 73cab094762315bc017fc29a6c4c1f674e0b682a Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Wed, 1 Jan 2014 22:38:34 -0500 Subject: [PATCH] Improvements to search page --- .../management/commands/populate_indexden.py | 4 +- karmaworld/apps/notes/search.py | 10 +++-- karmaworld/apps/notes/views.py | 11 ++++-- karmaworld/assets/css/search.css | 8 ++++ karmaworld/templates/header.html | 37 +++++++++++-------- .../templates/notes/note_list_entry.html | 6 ++- .../templates/notes/search_results.html | 25 ++++++++++--- 7 files changed, 72 insertions(+), 29 deletions(-) diff --git a/karmaworld/apps/notes/management/commands/populate_indexden.py b/karmaworld/apps/notes/management/commands/populate_indexden.py index 421c0cc..d80106c 100644 --- a/karmaworld/apps/notes/management/commands/populate_indexden.py +++ b/karmaworld/apps/notes/management/commands/populate_indexden.py @@ -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 diff --git a/karmaworld/apps/notes/search.py b/karmaworld/apps/notes/search.py index e08c3b9..afff889 100644 --- a/karmaworld/apps/notes/search.py +++ b/karmaworld/apps/notes/search.py @@ -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 diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index b8a3376..bc43cb2 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -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: diff --git a/karmaworld/assets/css/search.css b/karmaworld/assets/css/search.css index 4eb45e3..821f110 100644 --- a/karmaworld/assets/css/search.css +++ b/karmaworld/assets/css/search.css @@ -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 diff --git a/karmaworld/templates/header.html b/karmaworld/templates/header.html index e47245f..f151af5 100644 --- a/karmaworld/templates/header.html +++ b/karmaworld/templates/header.html @@ -2,22 +2,25 @@ diff --git a/karmaworld/templates/notes/note_list_entry.html b/karmaworld/templates/notes/note_list_entry.html index 1b9702e..627a7c8 100644 --- a/karmaworld/templates/notes/note_list_entry.html +++ b/karmaworld/templates/notes/note_list_entry.html @@ -21,7 +21,11 @@
- {{ note.text|slice:":500" }} … + {% if note_preview %} + {{ note_preview|safe }} … + {% else %} + {{ note.text|slice:":500" }} … + {% endif %}
diff --git a/karmaworld/templates/notes/search_results.html b/karmaworld/templates/notes/search_results.html index 7792f8e..d1ded58 100644 --- a/karmaworld/templates/notes/search_results.html +++ b/karmaworld/templates/notes/search_results.html @@ -13,6 +13,15 @@
+ + +
YOU SEARCHED FOR @@ -42,11 +51,17 @@
-
- {% for note in object_list %} - {% include 'notes/note_list_entry.html' with note=note %} - {% endfor %} -
+ {% if object_list %} +
+ {% for tuple in object_list %} + {% include 'notes/note_list_entry.html' with note=tuple.0 note_preview=tuple.1 %} + {% endfor %} +
+ {% else %} +
+

Sorry! No results were found.

+
+ {% endif %}
-- 2.25.1