# HG changeset patch # User Brian Neal # Date 1305687865 0 # Node ID 524fd1b3919af94a75b50513fdb22c1b1856ef3b # Parent a8ac4dd3bf031fbea27706a78bf8ae8d57c7bbca 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. diff -r a8ac4dd3bf03 -r 524fd1b3919a gpp/core/templatetags/core_tags.py --- a/gpp/core/templatetags/core_tags.py Sat May 14 19:02:07 2011 +0000 +++ b/gpp/core/templatetags/core_tags.py Wed May 18 03:04:25 2011 +0000 @@ -2,6 +2,7 @@ Miscellaneous/utility template tags. """ import datetime +import urllib from django import template from django.conf import settings @@ -85,3 +86,41 @@ 'profiles': profiles, 'today': today, } + + +class EncodeParamsNode(template.Node): + """ + This is the Node class for the encode_params template tag. + This template tag retrieves the named parameters from the supplied + querydict and returns them as a urlencoded string. + + """ + def __init__(self, querydict, args): + self.querydict = template.Variable(querydict) + self.args = args + + def render(self, context): + querydict = self.querydict.resolve(context) + params = [] + for arg in self.args: + params.extend([(arg, value) for value in querydict.getlist(arg)]) + + return urllib.urlencode(params) + + +@register.tag +def encode_params(parser, token): + """ + This is the compilation function for the encore_params template tag. + This template tag retrieves the named parameters from the supplied + querydict and returns them as a urlencoded string. + + """ + bits = token.split_contents() + if len(bits) < 3: + raise template.TemplateSyntaxError("%s takes at least 2 arguments: " + "querydict arg1 [arg2 arg3 ... argN]" % bits[0]) + + querydict = bits[1] + args = [arg[1:-1] for arg in bits[2:]] + return EncodeParamsNode(querydict, args) diff -r a8ac4dd3bf03 -r 524fd1b3919a gpp/custom_search.py --- a/gpp/custom_search.py Sat May 14 19:02:07 2011 +0000 +++ b/gpp/custom_search.py Wed May 18 03:04:25 2011 +0000 @@ -6,7 +6,6 @@ import urllib from django import forms -from haystack.views import SearchView from haystack.forms import ModelSearchForm @@ -35,31 +34,3 @@ 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 a8ac4dd3bf03 -r 524fd1b3919a gpp/templates/search/search.html --- a/gpp/templates/search/search.html Sat May 14 19:02:07 2011 +0000 +++ b/gpp/templates/search/search.html Wed May 18 03:04:25 2011 +0000 @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load highlight %} +{% load core_tags %} {% block title %}Search{% endblock %} {% block custom_js %}