# HG changeset patch # User Brian Neal # Date 1275854775 0 # Node ID 71fd8454688b74b8226579c84cd82dc2704bfba7 # Parent 26ee684c20338bdd4706a88e2805fad4d18fb47f 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. diff -r 26ee684c2033 -r 71fd8454688b gpp/news/models.py --- 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)) diff -r 26ee684c2033 -r 71fd8454688b gpp/news/search_indexes.py --- 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) diff -r 26ee684c2033 -r 71fd8454688b gpp/templates/search/indexes/news/story_summary.txt --- 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 }} diff -r 26ee684c2033 -r 71fd8454688b gpp/templates/search/indexes/news/story_title.txt --- 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 @@ -{{ object.title }} diff -r 26ee684c2033 -r 71fd8454688b gpp/templates/search/indexes/weblinks/link_text.txt --- /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 }} diff -r 26ee684c2033 -r 71fd8454688b gpp/templates/search/search.html --- 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 @@
{% for result in page.object_list %}
- {{ result.verbose_name }}: {{ result.title|safe }} • {{ result.score }} + {{ result.verbose_name }}: {{ result.object.search_title }} • ({{ result.score }})
- {% highlight result.summary with query css_class "highlight" max_length 200 %} + {% highlight result.object.search_summary with query css_class "highlight" max_length 200 %}
{% endfor %}
+ {% else %} +

No results found for {{ query }}.

{% endif %} {% if page.has_previous or page.has_next %} diff -r 26ee684c2033 -r 71fd8454688b gpp/urls.py --- 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'), ) diff -r 26ee684c2033 -r 71fd8454688b gpp/weblinks/models.py --- 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 diff -r 26ee684c2033 -r 71fd8454688b gpp/weblinks/search_indexes.py --- /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)