Skip to content

Commit

Permalink
Merge pull request #857 from akvo/feature/843_v3_pagination
Browse files Browse the repository at this point in the history
[#843] Added pagination for projects, organisations and updates list
  • Loading branch information
kardan committed Oct 27, 2014
2 parents 5d7ef30 + ab14077 commit a683448
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 59 deletions.
14 changes: 13 additions & 1 deletion akvo/rsr/views/organisation.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 Organisation
from akvo.utils import pagination

from django.shortcuts import get_object_or_404, render


def directory(request):
context = {'organisations': Organisation.objects.all()}
organisations_list = Organisation.objects.all()
page = request.GET.get('page')

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

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


Expand Down
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
14 changes: 13 additions & 1 deletion akvo/rsr/views/project_update.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 ProjectUpdate, Project
from akvo.utils import pagination

from django.shortcuts import get_object_or_404, render


def directory(request):
context = {'updates': ProjectUpdate.objects.all()}
updates_list = ProjectUpdate.objects.all()
page = request.GET.get('page')

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

context = {
'page': page,
'paginator': paginator,
'page_range': page_range,
}
return render(request, 'update_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>
17 changes: 4 additions & 13 deletions akvo/templates/organisation_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 organisation list below by searcing by name, country or type" %}</p>
<p>{% trans "Refine the organisation list below by searching by name, country or type" %}</p>
<div class="input-group"\>
<input type="text" class="form-control" placeholder="{% trans "Search" %}">
<span class="input-group-btn">
Expand Down Expand Up @@ -50,22 +50,13 @@

<div class="container">
<div class="row center-text">
<p>{% blocktrans %}Viewing n of n organisations{% 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 }} organisations</p>
{% include 'navigation/pagination.html' %}
</div>

<section class="main-list">
<div class="row">
{% for o in organisations %}
{% for o in page %}
<article class="col-md-3 col-sm-4 col-xs-6">
<a href="{% url 'organisation-main' o.id %}">
<img src="http://placehold.it/400x300">
Expand Down
25 changes: 4 additions & 21 deletions akvo/templates/project_directory.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,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 @@ -261,15 +252,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 %}
27 changes: 5 additions & 22 deletions akvo/templates/update_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 update list below by searcing by project, name or organisation" %}</p>
<p>{% trans "Refine the update list below by searching by project, name or organisation" %}</p>
<div class="input-group"\>
<input type="text" class="form-control" placeholder="{% trans "Search" %}">
<span class="input-group-btn">
Expand Down Expand Up @@ -51,22 +51,13 @@
<div class="container">

<div class="row center-text">
<p>{% blocktrans %}Viewing n of n updates{% 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 }} updates</p>
{% include 'navigation/pagination.html' %}
</div>

<section class="main-list">
<div class="row">
{% for u in updates %}
{% for u in page %}
<article class="col-md-3 col-sm-4 col-xs-6 col-ty-12">
<a href="{% url 'update-main' u.project.id u.id %}">
<img src="http://placehold.it/400x300">
Expand All @@ -87,15 +78,7 @@ <h1><a href="{% url 'update-main' u.project.id u.id %}">{{u.title}}</a></h1>
</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>
Expand Down
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 a683448

Please sign in to comment.