Skip to content

Commit

Permalink
[#843] Added pagination for projects list
Browse files Browse the repository at this point in the history
Pagination has been added for the projects list, including a general script in utils.py that takes care of the format of the pagination so that the number of items is never bigger than 9.
  • Loading branch information
KasperBrandt committed Oct 27, 2014
1 parent a19f78c commit 338b6b0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
14 changes: 13 additions & 1 deletion akvo/rsr/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
Akvo RSR module. For additional details on the GNU license please
see < http://www.gnu.org/licenses/agpl.html >.
"""

from akvo.rsr.models import Project
from akvo.utils import pagination

from django.shortcuts import get_object_or_404, render


def directory(request):
context = {'projects': Project.objects.published()}
projects_list = Project.objects.published()
page = request.GET.get('page')

page, paginator, page_range = pagination(page, projects_list, 10)

context = {
'page': page,
'paginator': paginator,
'page_range': page_range,
}
return render(request, 'project_directory.html', context)


Expand Down
19 changes: 19 additions & 0 deletions akvo/templates/navigation/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<ul class="pagination">
{% if page.has_previous %}
<li><a href="?page={{ page.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for page_number in page_range %}
{% if not page_number == '...' %}
<li {% if page_number == page.number %}class="active"{% endif %}><a href="?page={{ page_number }}">{{ page_number }}</a></li>
{% else %}
<li class="disabled"><span>{{ page_number }}</span></li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li><a href="?page={{ page.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
27 changes: 5 additions & 22 deletions akvo/templates/project_directory.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<section id="search-filter">
<div id="search">
<form class="form-inline" role="form">
<p>{% trans "Refine the project list below by searcing by name, organisation or sector" %}</p>
<p>{% trans "Refine the project list below by searching by name, organisation or sector" %}</p>
<div class="input-group"\>
<input type="text" class="form-control" placeholder="{% trans "Search" %}">
<span class="input-group-btn">
Expand Down Expand Up @@ -51,21 +51,12 @@
<div class="container">

<div class="row center-text">
<p>{% blocktrans %}Viewing n of n projects{% endblocktrans %}</p>

<ul class="pagination">
<li class="disabled"><a href="#">&laquo;</a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">&raquo;</a></li>
</ul>
<p>Viewing {{ page.start_index }} - {{ page.end_index }} of {{ paginator.count }} projects</p>
{% include 'navigation/pagination.html' %}
</div>

<section class="main-list">
{% for p in projects %}
{% for p in page %}
<article class="row">
<div class="col-sm-2 col-xs-3">
<a href="{% url 'project-main' p.id %}">
Expand Down Expand Up @@ -105,15 +96,7 @@ <h4 class="section">Finance</h4>
</section>

<div class="row center-text">
<ul class="pagination">
<li class="disabled"><a href="#">&laquo;</a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">&raquo;</a></li>
</ul>
{% include 'navigation/pagination.html' %}
</div>
</div>
{% endblock %}
28 changes: 28 additions & 0 deletions akvo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from django.conf import settings
from django.core.mail import send_mail, EmailMessage
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.db.models import get_model
from django.http import HttpResponse
Expand Down Expand Up @@ -470,3 +471,30 @@ def rsr_show_keywords(instance):
return keyword_str
else:
return 'None'


def pagination(page, object_list, objects_per_page):
paginator = Paginator(object_list, objects_per_page)

try:
page = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
page = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
page = paginator.page(paginator.num_pages)

page_range = paginator.page_range
active = page.number

if not len(page_range) < 10:
if active > 4:
page_range[1] = '...'
del page_range[2:active-2]
if (page_range[-1] - active) > 3:
page_range[-2] = '...'
active_index = page_range.index(active)
del page_range[active_index+2:-2]

return page, paginator, page_range

0 comments on commit 338b6b0

Please sign in to comment.