Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #646 from floemker/paginator
Use an own paginator
- Loading branch information
Showing
7 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.core.paginator import Paginator | ||
|
||
class WikiPaginator(Paginator): | ||
|
||
def __init__(self, *args, **kwargs): | ||
""" | ||
:param side_pages: How many pages should be shown before and after the current page | ||
""" | ||
self.side_pages = kwargs.pop('side_pages', 4) | ||
super(WikiPaginator, self).__init__(*args, **kwargs) | ||
|
||
def page(self, number): | ||
# Save last accessed page number for context-based lookup in page_range | ||
self.last_accessed_page_number = number | ||
return super(WikiPaginator, self).page(number) | ||
|
||
@property | ||
def page_range(self): | ||
left = max(self.last_accessed_page_number - self.side_pages, 2) | ||
right = min(self.last_accessed_page_number + self.side_pages+1, self.num_pages) | ||
pages = [] | ||
if self.num_pages > 0: | ||
pages = [1] | ||
if left > 2: | ||
pages += [0] | ||
pages += range(left, right) | ||
if right < self.num_pages: | ||
pages += [0] | ||
if self.num_pages > 1: | ||
pages += [self.num_pages] | ||
return pages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
{% load i18n %} | ||
{% if is_paginated %} | ||
<ul class="pagination"> | ||
{% if page_obj.has_previous %} | ||
<li><a class="prev btn btn-info" href="?{% if search_query %}q={{ search_query }}&{% endif %}page={{ page_obj.previous_page_number }}{% if appended_key %}&{{ appended_key }}={{ appended_value }}{% endif %}">«</a></li> | ||
{% else %} | ||
<li class="disabled"><span>«</span></li> | ||
{% endif %} | ||
|
||
{% for pc in paginator.page_range %} | ||
<li class="{% if pc == page_obj.number %} active{% endif %}"><a href="?{% if search_query %}q={{ search_query }}&{% endif %}page={{ pc }}{% if appended_key %}&{{ appended_key }}={{ appended_value }}{% endif %}">{{ pc }}</a></li> | ||
{% if pc == 0 %} | ||
<li class="disabled"><span>...</span></li> | ||
{% else %} | ||
<li class="{% if pc == page_obj.number %} active{% endif %}"><a href="?{% if search_query %}q={{ search_query }}&{% endif %}page={{ pc }}{% if appended_key %}&{{ appended_key }}={{ appended_value }}{% endif %}">{{ pc }}</a></li> | ||
{% endif %} | ||
{% endfor %} | ||
|
||
{% if page_obj.has_next %} | ||
<li><a class="next btn btn-info" href="?{% if search_query %}q={{ search_query }}&{% endif %}page={{ page_obj.next_page_number }}{% if appended_key %}&{{ appended_key }}={{ appended_value }}{% endif %}">»</a></li> | ||
{% else %} | ||
<li class="disabled"><span>»</span></li> | ||
{% endif %} | ||
</ul> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.test import TestCase | ||
from wiki.core.paginator import WikiPaginator | ||
|
||
|
||
class PaginatorTest(TestCase): | ||
""" | ||
Test the WikiPaginator and it's page_range() function | ||
""" | ||
|
||
def test_paginator(self): | ||
objects = [1] | ||
p = WikiPaginator(objects, 2, side_pages=2) | ||
self.assertEqual(p.num_pages, 1) | ||
|
||
p.page(1) | ||
self.assertEqual(p.page_range, [1]) | ||
|
||
objects = [1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
p = WikiPaginator(objects, 2, side_pages=2) | ||
self.assertEqual(p.num_pages, 5) | ||
|
||
p.page(1) | ||
self.assertEqual(p.page_range, [1, 2, 3, 0, 5]) | ||
|
||
p.page(3) | ||
self.assertEqual(p.page_range, [1, 2, 3, 4, 5]) | ||
|
||
objects = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] | ||
p = WikiPaginator(objects, 2, side_pages=2) | ||
self.assertEqual(p.num_pages, 9) | ||
|
||
p.page(1) | ||
self.assertEqual(p.page_range, [1, 2, 3, 0, 9]) | ||
|
||
p.page(5) | ||
self.assertEqual(p.page_range, [1, 0, 3, 4, 5, 6, 7, 0, 9]) | ||
|
||
p.page(8) | ||
self.assertEqual(p.page_range, [1, 0, 6, 7, 8, 9]) |