comparison gpp/custom_search.py @ 438:524fd1b3919a

Fixing #214; don't need a custom model search view. All the info is in the template. Wrote a template tag to extract the info from the request context and urlencode them.
author Brian Neal <bgneal@gmail.com>
date Wed, 18 May 2011 03:04:25 +0000
parents d4d167876c25
children b910cc1460c8
comparison
equal deleted inserted replaced
437:a8ac4dd3bf03 438:524fd1b3919a
4 4
5 """ 5 """
6 import urllib 6 import urllib
7 7
8 from django import forms 8 from django import forms
9 from haystack.views import SearchView
10 from haystack.forms import ModelSearchForm 9 from haystack.forms import ModelSearchForm
11 10
12 11
13 MODEL_CHOICES = ( 12 MODEL_CHOICES = (
14 ('forums.topic', 'Forum Topics'), 13 ('forums.topic', 'Forum Topics'),
33 32
34 def __init__(self, *args, **kwargs): 33 def __init__(self, *args, **kwargs):
35 super(CustomModelSearchForm, self).__init__(*args, **kwargs) 34 super(CustomModelSearchForm, self).__init__(*args, **kwargs)
36 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES, 35 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES,
37 label='', widget=forms.CheckboxSelectMultiple) 36 label='', widget=forms.CheckboxSelectMultiple)
38
39
40 class ModelSearchView(SearchView):
41 """
42 This custom search view puts an extra value in the template context named
43 search_parms. search_parms will contain the search term q plus any models
44 arguments all as a urlencoded string. This is useful for generating
45 pagination links in the template.
46
47 """
48 def search_params(self):
49 """
50 Return the q and models search parameters as a urlencoded string if the
51 form is valid. An empty string is returned if the form is not valid.
52
53 """
54 if self.form.is_valid():
55 q = self.form.cleaned_data['q']
56 models = self.form.cleaned_data['models']
57
58 params = [('q', q)]
59 params.extend([('models', model) for model in models])
60 return urllib.urlencode(params)
61
62 return ''
63
64 def extra_context(self):
65 return {'search_params': self.search_params() }