Mercurial > public > sg101
changeset 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 | a8ac4dd3bf03 |
children | 1f139de929c4 |
files | gpp/core/templatetags/core_tags.py gpp/custom_search.py gpp/templates/search/search.html gpp/urls.py |
diffstat | 4 files changed, 44 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- 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)
--- 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() }
--- 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 %} <script type="text/javascript"> @@ -61,9 +62,9 @@ {% if page.has_previous or page.has_next %} <div> - {% if page.has_previous %}<a href="?{{ search_params }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %} + {% if page.has_previous %}<a href="?{% encode_params request.GET 'q' 'models' %}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %} | - {% if page.has_next %}<a href="?{{ search_params }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %} + {% if page.has_next %}<a href="?{% encode_params request.GET 'q' 'models' %}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %} </div> {% endif %} {% else %}
--- a/gpp/urls.py Sat May 14 19:02:07 2011 +0000 +++ b/gpp/urls.py Wed May 18 03:04:25 2011 +0000 @@ -7,7 +7,7 @@ from news.feeds import LatestNewsFeed from forums.feeds import ForumsFeed -from custom_search import ModelSearchView, CustomModelSearchForm +from custom_search import CustomModelSearchForm admin.autodiscover() @@ -53,8 +53,7 @@ # Haystack search views urlpatterns += patterns('haystack.views', url(r'^search/$', - search_view_factory(view_class=ModelSearchView, - form_class=CustomModelSearchForm, load_all=True), + search_view_factory(form_class=CustomModelSearchForm, load_all=True), name='haystack_search'), )