changeset 219:26ee684c2033

Initial commit of Haystack search integration. See #51.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 Jun 2010 20:46:52 +0000
parents 6dbb8faef085
children 71fd8454688b
files gpp/news/search_indexes.py gpp/search_sites.py gpp/settings.py gpp/templates/base.html gpp/templates/search/indexes/news/story_summary.txt gpp/templates/search/indexes/news/story_text.txt gpp/templates/search/indexes/news/story_title.txt gpp/templates/search/search.html gpp/urls.py media/icons/magnifier.png
diffstat 10 files changed, 89 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/news/search_indexes.py	Sat Jun 05 20:46:52 2010 +0000
@@ -0,0 +1,16 @@
+"""Haystack search index for the news application."""
+from haystack.indexes import *
+from haystack import site
+from news.models import Story
+
+
+class StoryIndex(SearchIndex):
+    text = CharField(document=True, use_template=True)
+    author = CharField(model_attr='submitter')
+    pub_date = DateTimeField(model_attr='date_submitted')
+
+    title = CharField(use_template=True, indexed=False)
+    summary = CharField(use_template=True, indexed=False)
+
+
+site.register(Story, StoryIndex)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/search_sites.py	Sat Jun 05 20:46:52 2010 +0000
@@ -0,0 +1,2 @@
+import haystack
+haystack.autodiscover()
--- a/gpp/settings.py	Tue Jun 01 05:16:40 2010 +0000
+++ b/gpp/settings.py	Sat Jun 05 20:46:52 2010 +0000
@@ -128,6 +128,7 @@
     'django.contrib.sites',
     'elsewhere',
     'tagging',
+    'haystack',
     'accounts',
     'antispam',
     'bio',
@@ -201,6 +202,14 @@
 MAX_TAG_LENGTH = 50
 
 #######################################################################
+# Haystack Search Settings
+#######################################################################
+HAYSTACK_SITECONF = 'gpp.search_sites'
+HAYSTACK_SEARCH_ENGINE = 'xapian'
+HAYSTACK_XAPIAN_PATH = os.path.join(project_path, 'xapian_index')
+
+
+#######################################################################
 # GPP Specific Settings
 #######################################################################
 GPP_LOG_LEVEL = 0
--- a/gpp/templates/base.html	Tue Jun 01 05:16:40 2010 +0000
+++ b/gpp/templates/base.html	Sat Jun 05 20:46:52 2010 +0000
@@ -41,6 +41,7 @@
       <li>Welcome, <a href="{% url bio-me %}">{{ user.username }}</a></li>
       <li><a href="{% url forums-index %}">Forums</a></li>
       <li>{% unread_messages user %}</li>
+      <li><a href="{% url haystack_search %}">Search</a></li>
       <li><a href="{% url accounts-logout %}">Logout</a></li>
       {% else %}
       <li><a href="{% url accounts-login %}">Login</a></li>
@@ -66,6 +67,7 @@
       <li><a href="{% url potd-view %}">Photo of the Day</a></li>
       <li><a href="{% url weblinks-main %}">Links</a></li>
       <li><a href="{% url downloads-index %}">Downloads</a></li>
+      <li><a href="{% url haystack_search %}">Search</a></li>
    </ul>
    {% cache 300 potd_block %}
       {% photo_of_the_day %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/search/indexes/news/story_summary.txt	Sat Jun 05 20:46:52 2010 +0000
@@ -0,0 +1,2 @@
+{{ object.short_text|safe }}
+{{ object.long_text|safe }}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/search/indexes/news/story_text.txt	Sat Jun 05 20:46:52 2010 +0000
@@ -0,0 +1,6 @@
+{{ object.title }}
+{{ object.submitter.username }}
+{{ object.submitter.get_full_name }}
+{{ object.short_text }}
+{{ object.long_text }}
+{{ object.tags }}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/search/indexes/news/story_title.txt	Sat Jun 05 20:46:52 2010 +0000
@@ -0,0 +1,1 @@
+<a href="{{ object.get_absolute_url }}">{{ object.title }}</a>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/search/search.html	Sat Jun 05 20:46:52 2010 +0000
@@ -0,0 +1,42 @@
+{% extends 'base.html' %}
+{% load highlight %}
+{% block title %}Search{% endblock %}
+{% block content %}
+<h2>Search <img src="{{ MEDIA_URL }}icons/magnifier.png" alt="Search" /></h2>
+<form method="get" action=".">
+  <table>
+      {{ form.as_table }}
+      <tr>
+          <td>&nbsp;</td>
+          <td>
+              <input type="submit" value="Search">
+          </td>
+      </tr>
+  </table>
+  {% if query %}
+      <h3>Results</h3>
+      {% if page.object_list %}
+      <dl>
+      {% for result in page.object_list %}
+         <dt>
+            {{ result.verbose_name }}: {{ result.title|safe }} &bull; {{ result.score }}
+         </dt>
+         <dd>
+            {% highlight result.summary with query css_class "highlight" max_length 200 %}
+         </dd>
+      {% endfor %}
+      </dl>
+      {% endif %}
+
+      {% if page.has_previous or page.has_next %}
+          <div>
+              {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
+              |
+              {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
+          </div>
+      {% endif %}
+  {% else %}
+      {# Show some example queries to run, maybe query syntax, something else? #}
+  {% endif %}
+</form>
+{% endblock %}
--- a/gpp/urls.py	Tue Jun 01 05:16:40 2010 +0000
+++ b/gpp/urls.py	Sat Jun 05 20:46:52 2010 +0000
@@ -3,6 +3,8 @@
 from django.contrib import admin
 from django.views.decorators.cache import cache_page
 
+from haystack.views import SearchView, search_view_factory
+
 from news.feeds import LatestNewsFeed
 from forums.feeds import ForumsFeed
 
@@ -45,6 +47,13 @@
    (r'^smiley/', include('smiley.urls')),
 )
 
+# Haystack search views
+urlpatterns += patterns('haystack.views',
+    url(r'^search/$', search_view_factory(view_class=SearchView, load_all=False),
+        name='haystack_search'),
+)
+
+
 if settings.DEBUG:
    urlpatterns += patterns('',
       (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
Binary file media/icons/magnifier.png has changed