New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use an own paginator #646
Merged
Merged
Use an own paginator #646
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4978999
Use an own paginator
floemker 7432303
Added missing column wiki_urlpath.moved_to_id
floemker 67f499e
Updated slug help text on the create form.
floemker c278ab7
Reworded the slug help text to emphasize the problem of not updated l…
floemker 076e89c
Rename wiki.core.wiki_paginate to wiki.core.paginator
benjaoming b065409
Merge branch 'benjaoming-floemker-paginator' into paginator
floemker 03593d9
Fixed python 3 only syntax.
floemker 7689524
Rename wiki.core.wiki_paginate to wiki.core.paginator, refac page num…
benjaoming d05282a
Rename so it doesn't sound like it's the last page number
benjaoming cb6b519
Merge pull request #4 from benjaoming/floemker-paginator
floemker File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,30 @@ | ||
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 _get_page(self, *args, **kwargs): | ||
self.curPage = super(WikiPaginator, self)._get_page(*args, **kwargs) | ||
return self.curPage | ||
|
||
@property | ||
def page_range(self): | ||
left = max(self.curPage.number - self.side_pages, 2) | ||
right = min(self.curPage.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.wiki_paginate 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]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment contract is too implicit. It means that
page_range
is broken if_get_page
hasn't been called. Isn't there a way to just call this method directly frompage_range
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjaoming Calling _get_page() from page_range() unfortunately does not work.
Source from the django paginator (django/core/paginator.py):
I.e. the page number is required. And that is exactly what is needed here in the
first place. Based on that I would say that it is guaranteed that _get_page()
is called before page_range().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried reordering this a bit
curPage
to something with underscores, camelCase isn't used in Python normally :)It's in floemker#4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More efficient is always good (int instead of a Page object). Committed.