Implements pagination when searching in the map list. The search method is now a GET (was POST), it's now used by the pagination links. An argument is passed so the pagination is done on the searched list.
Signed-off-by: Maxime Hadjinlian <[email protected]> --- www/maposmatic/views.py | 27 +++++++++++---------------- www/templates/maposmatic/all_maps.html | 10 +++++----- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/www/maposmatic/views.py b/www/maposmatic/views.py index c3ae2d3..3dc8d81 100644 --- a/www/maposmatic/views.py +++ b/www/maposmatic/views.py @@ -141,21 +141,16 @@ def all_maps(request): terms, when provided.""" map_list = None - - if request.method == 'POST': - form = forms.MapSearchForm(request.POST) - if form.is_valid(): - map_list = (models.MapRenderingJob.objects - .order_by('maptitle') - .filter(status=2) - .filter(maptitle__icontains=form.cleaned_data['query'])) - if len(map_list) == 1: - return HttpResponseRedirect(reverse('job-by-id', - args=[map_list[0].id])) - - # TODO: find a way to have a working paginator. For now, limit to - # ITEMS_PER_PAGE results. - map_list = map_list[:www.settings.ITEMS_PER_PAGE] + form = forms.MapSearchForm(request.GET) + + if form.is_valid(): + map_list = (models.MapRenderingJob.objects + .order_by('maptitle') + .filter(status=2) + .filter(maptitle__icontains=form.cleaned_data['query'])) + if len(map_list) == 1: + return HttpResponseRedirect(reverse('job-by-id', + args=[map_list[0].id])) else: form = forms.MapSearchForm() @@ -177,7 +172,7 @@ def all_maps(request): return render_to_response('maposmatic/all_maps.html', { 'maps': maps, 'letters': helpers.get_letters(), - 'form': form }, + 'form': form, 'is_search': form.is_valid() }, context_instance=MapOSMaticRequestContext(request)) def all_maps_by_letter(request, letter): diff --git a/www/templates/maposmatic/all_maps.html b/www/templates/maposmatic/all_maps.html index e940ee5..e64a5c6 100644 --- a/www/templates/maposmatic/all_maps.html +++ b/www/templates/maposmatic/all_maps.html @@ -31,7 +31,7 @@ {% block page %} <div class="mapsearch"> - <form action="{% url maps %}" method="post">{{ form.query }}<input type="submit" value="{% trans "Search" %}" /></form> + <form action="{% url maps %}" method="get">{{ form.query }}<input type="submit" value="{% trans "Search" %}" /></form> </div> <h1><a href="{% url maps %}">{% trans "Maps" %}</a> <a href="/feeds/maps/"><img src="/smedia/feed.png" class="feedicon" title="{% trans "MapOSMatic maps feed" %}" /></h1> @@ -42,9 +42,9 @@ {% ifnotequal maps.paginator.num_pages 1 %} <div class="pagination"> - {% if maps.has_previous %}<a href="?page={{ maps.previous_page_number }}">« {% trans "Previous" %}</a>{% endif %} + {% if maps.has_previous %}<a href="?{% if is_search %}query={{ form.cleaned_data.query|urlencode }}&{% endif %}page={{ maps.previous_page_number }}">« {% trans "Previous" %}</a>{% endif %} <span class="current">{% trans "Page" %} {{ maps.number }} {% trans "of" %} {{ maps.paginator.num_pages }}</span> - {% if maps.has_next %}<a href="?page={{ maps.next_page_number }}">{% trans "Next" %} »</a>{% endif %} + {% if maps.has_next %}<a href="?{% if is_search %}query={{ form.cleaned_data.query|urlencode }}&{% endif %}page={{ maps.next_page_number }}">{% trans "Next" %} »</a>{% endif %} </div> {% endifnotequal %} @@ -67,9 +67,9 @@ {% ifnotequal maps.paginator.num_pages 1 %} <div class="pagination"> - {% if maps.has_previous %}<a href="?page={{ maps.previous_page_number }}">« {% trans "Previous" %}</a>{% endif %} + {% if maps.has_previous %}<a href="?{% if is_search %}query={{ form.cleaned_data.query|urlencode }}&{% endif %}page={{ maps.previous_page_number }}">« {% trans "Previous" %}</a>{% endif %} <span class="current">{% trans "Page" %} {{ maps.number }} {% trans "of" %} {{ maps.paginator.num_pages }}</span> - {% if maps.has_next %}<a href="?page={{ maps.next_page_number }}">{% trans "Next" %} »</a>{% endif %} + {% if maps.has_next %}<a href="?{% if is_search %}query={{ form.cleaned_data.query|urlencode }}&{% endif %}page={{ maps.next_page_number }}">{% trans "Next" %} »</a>{% endif %} </div> {% endifnotequal %} -- 1.6.6.1
