# HG changeset patch # User Brian Neal # Date 1302395534 0 # Node ID d4d167876c25278b300342110277b6daf1325c80 # Parent b1f939b1fb01b21b5233fc0634c3d05794717a6f Fixing #205; search result pagination wasn't including models. Created a custom model search form so that the query term and model selections would persist across pages. diff -r b1f939b1fb01 -r d4d167876c25 gpp/custom_search.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/custom_search.py Sun Apr 10 00:32:14 2011 +0000 @@ -0,0 +1,65 @@ +""" +This module contains custom code to tailor the Haystack search application to +our needs. + +""" +import urllib + +from django import forms +from haystack.views import SearchView +from haystack.forms import ModelSearchForm + + +MODEL_CHOICES = ( + ('forums.topic', 'Forum Topics'), + ('forums.post', 'Forum Posts'), + ('news.story', 'News Stories'), + ('bio.userprofile', 'User Profiles'), + ('weblinks.link', 'Links'), + ('downloads.download', 'Downloads'), + ('podcast.item', 'Podcasts'), + ('ygroup.post', 'Yahoo Group Archives'), +) + + +class CustomModelSearchForm(ModelSearchForm): + """ + This customized ModelSearchForm allows us to explictly label and order + the model choices. + + """ + q = forms.CharField(required=False, label='', + widget=forms.TextInput(attrs={'class': 'text', 'size': 48})) + + def __init__(self, *args, **kwargs): + super(CustomModelSearchForm, self).__init__(*args, **kwargs) + self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES, + label='', widget=forms.CheckboxSelectMultiple) + + +class ModelSearchView(SearchView): + """ + This custom search view puts an extra value in the template context named + search_parms. search_parms will contain the search term q plus any models + arguments all as a urlencoded string. This is useful for generating + pagination links in the template. + + """ + def search_params(self): + """ + Return the q and models search parameters as a urlencoded string if the + form is valid. An empty string is returned if the form is not valid. + + """ + if self.form.is_valid(): + q = self.form.cleaned_data['q'] + models = self.form.cleaned_data['models'] + + params = [('q', q)] + params.extend([('models', model) for model in models]) + return urllib.urlencode(params) + + return '' + + def extra_context(self): + return {'search_params': self.search_params() } diff -r b1f939b1fb01 -r d4d167876c25 gpp/templates/search/search.html --- a/gpp/templates/search/search.html Sat Apr 09 19:20:12 2011 +0000 +++ b/gpp/templates/search/search.html Sun Apr 10 00:32:14 2011 +0000 @@ -5,7 +5,7 @@ @@ -25,39 +26,10 @@ {% block content %}

Search Search

- - - - - -
+{{ form.q }}
Search in: - - - - - - - - - - - - - - - - -
- - -
- - -
- -
+{{ form.models }}

Check all | Check none

@@ -88,9 +60,9 @@ {% if page.has_previous or page.has_next %}
- {% if page.has_previous %}{% endif %}« Previous{% if page.has_previous %}{% endif %} + {% if page.has_previous %}{% endif %}« Previous{% if page.has_previous %}{% endif %} | - {% if page.has_next %}{% endif %}Next »{% if page.has_next %}{% endif %} + {% if page.has_next %}{% endif %}Next »{% if page.has_next %}{% endif %}
{% endif %} {% else %} diff -r b1f939b1fb01 -r d4d167876c25 gpp/urls.py --- a/gpp/urls.py Sat Apr 09 19:20:12 2011 +0000 +++ b/gpp/urls.py Sun Apr 10 00:32:14 2011 +0000 @@ -3,10 +3,11 @@ from django.contrib import admin from django.views.decorators.cache import cache_page -from haystack.views import SearchView, search_view_factory +from haystack.views import search_view_factory from news.feeds import LatestNewsFeed from forums.feeds import ForumsFeed +from custom_search import ModelSearchView, CustomModelSearchForm admin.autodiscover() @@ -51,7 +52,9 @@ # Haystack search views urlpatterns += patterns('haystack.views', - url(r'^search/$', search_view_factory(view_class=SearchView, load_all=True), + url(r'^search/$', + search_view_factory(view_class=ModelSearchView, + form_class=CustomModelSearchForm, load_all=True), name='haystack_search'), ) diff -r b1f939b1fb01 -r d4d167876c25 static/css/base.css --- a/static/css/base.css Sat Apr 09 19:20:12 2011 +0000 +++ b/static/css/base.css Sun Apr 10 00:32:14 2011 +0000 @@ -433,3 +433,10 @@ div.news-story-container { padding: 0.5em; } + +ul.no-bullet-inline-block { + list-style: none; +} +ul.no-bullet-inline-block li { + display: inline-block; +}