Mercurial > public > sg101
changeset 220:71fd8454688b
For #51, added weblinks to search. Decided against using the search index to store prerendered results. My fear is this could get too unweildy once we add forums.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 06 Jun 2010 20:06:15 +0000 |
parents | 26ee684c2033 |
children | 8d13baeaa5c1 |
files | gpp/news/models.py gpp/news/search_indexes.py gpp/templates/search/indexes/news/story_summary.txt gpp/templates/search/indexes/news/story_title.txt gpp/templates/search/indexes/weblinks/link_text.txt gpp/templates/search/search.html gpp/urls.py gpp/weblinks/models.py gpp/weblinks/search_indexes.py |
diffstat | 9 files changed, 39 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/news/models.py Sat Jun 05 20:46:52 2010 +0000 +++ b/gpp/news/models.py Sun Jun 06 20:06:15 2010 +0000 @@ -75,3 +75,9 @@ now = datetime.datetime.now() delta = now - self.date_submitted return self.allow_comments and delta.days < 30 + + def search_title(self): + return self.title + + def search_summary(self): + return u"\n".join((self.short_text, self.long_text))
--- a/gpp/news/search_indexes.py Sat Jun 05 20:46:52 2010 +0000 +++ b/gpp/news/search_indexes.py Sun Jun 06 20:06:15 2010 +0000 @@ -9,8 +9,5 @@ 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)
--- a/gpp/templates/search/indexes/news/story_summary.txt Sat Jun 05 20:46:52 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -{{ object.short_text|safe }} -{{ object.long_text|safe }}
--- a/gpp/templates/search/indexes/news/story_title.txt Sat Jun 05 20:46:52 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -<a href="{{ object.get_absolute_url }}">{{ object.title }}</a>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/search/indexes/weblinks/link_text.txt Sun Jun 06 20:06:15 2010 +0000 @@ -0,0 +1,5 @@ +{{ object.title }} +{{ object.url }} +{{ object.user.username }} +{{ object.user.get_full_name }} +{{ object.description }}
--- a/gpp/templates/search/search.html Sat Jun 05 20:46:52 2010 +0000 +++ b/gpp/templates/search/search.html Sun Jun 06 20:06:15 2010 +0000 @@ -19,13 +19,15 @@ <dl> {% for result in page.object_list %} <dt> - {{ result.verbose_name }}: {{ result.title|safe }} • {{ result.score }} + {{ result.verbose_name }}: <a href="{{ result.object.get_absolute_url }}">{{ result.object.search_title }}</a> • ({{ result.score }}) </dt> <dd> - {% highlight result.summary with query css_class "highlight" max_length 200 %} + {% highlight result.object.search_summary with query css_class "highlight" max_length 200 %} </dd> {% endfor %} </dl> + {% else %} + <p>No results found for <em>{{ query }}</em>.</p> {% endif %} {% if page.has_previous or page.has_next %}
--- a/gpp/urls.py Sat Jun 05 20:46:52 2010 +0000 +++ b/gpp/urls.py Sun Jun 06 20:06:15 2010 +0000 @@ -49,7 +49,7 @@ # Haystack search views urlpatterns += patterns('haystack.views', - url(r'^search/$', search_view_factory(view_class=SearchView, load_all=False), + url(r'^search/$', search_view_factory(view_class=SearchView, load_all=True), name='haystack_search'), )
--- a/gpp/weblinks/models.py Sat Jun 05 20:46:52 2010 +0000 +++ b/gpp/weblinks/models.py Sun Jun 06 20:06:15 2010 +0000 @@ -60,6 +60,12 @@ def get_absolute_url(self): return ('weblinks-link_detail', [str(self.id)]) + def search_title(self): + return self.title + + def search_summary(self): + return self.description + class PendingLink(LinkBase): """This model represents links that users submit. They must be approved by
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/weblinks/search_indexes.py Sun Jun 06 20:06:15 2010 +0000 @@ -0,0 +1,17 @@ +"""Haystack search index for the weblinks application.""" +from haystack.indexes import * +from haystack import site + +from weblinks.models import Link + + +class LinkIndex(SearchIndex): + text = CharField(document=True, use_template=True) + author = CharField(model_attr='user') + pub_date = DateTimeField(model_attr='date_added') + + def get_queryset(self): + return Link.public_objects.all() + + +site.register(Link, LinkIndex)