Skip to content
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 Django 3.2-compatible list pagination #1

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 15 additions & 46 deletions suit/templatetags/suit_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,31 @@
from django.contrib.admin.templatetags.admin_list import result_list
from django.contrib.admin.views.main import ALL_VAR, PAGE_VAR
from django.utils.html import escape
from django.utils.html import format_html

from suit.compat import tpl_context_class


register = template.Library()

DOT = '.'


@register.simple_tag
def paginator_number(cl, i):
"""
Generates an individual page index link in a paginated list.
"""
if i == DOT:
return mark_safe(
'<li class="disabled"><a href="#" onclick="return false;">..'
'.</a></li>')
if i == cl.paginator.ELLIPSIS:
return format_html(
'<li class="disabled"><a href="#" onclick="return false;">{}'
'</a></li>', cl.paginator.ELLIPSIS)
elif i == cl.page_num:
return mark_safe(
'<li class="active"><a href="">%d</a></li> ' % (i + 1))
return format_html(
'<li class="active"><a href="">{}</a></li> ', i)
else:
return mark_safe('<li><a href="%s"%s>%d</a></li> ' % (
return format_html('<li><a href="{}"{}>{}</a></li> ',
escape(cl.get_query_string({PAGE_VAR: i})),
(i == cl.paginator.num_pages - 1 and ' class="end"' or ''),
i + 1))
(' class="end"' if i == cl.paginator.num_pages else ''),
i)


@register.simple_tag
Expand All @@ -47,7 +46,8 @@ def paginator_info(cl):
entries_to = paginator.count
else:
entries_from = (
(paginator.per_page * cl.page_num) + 1) if paginator.count > 0 else 0
# (paginator.per_page * cl.page_num) + 1) if paginator.count > 0 else 0
(paginator.per_page * (cl.page_num - 1)) + 1) if paginator.count > 0 else 0
entries_to = entries_from - 1 + paginator.per_page
if paginator.count < entries_to:
entries_to = paginator.count
Expand All @@ -58,42 +58,11 @@ def paginator_info(cl):
@register.inclusion_tag('admin/pagination.html')
def pagination(cl):
"""
Generates the series of links to the pages in a paginated list.
Generate the series of links to the pages in a paginated list.
"""
paginator, page_num = cl.paginator, cl.page_num

pagination_required = (not cl.show_all or not cl.can_show_all) \
and cl.multi_page
if not pagination_required:
page_range = []
else:
ON_EACH_SIDE = 3
ON_ENDS = 2

# If there are 10 or fewer pages, display links to every page.
# Otherwise, do some fancy
if paginator.num_pages <= 8:
page_range = range(paginator.num_pages)
else:
# Insert "smart" pagination links, so that there are always ON_ENDS
# links at either end of the list of pages, and there are always
# ON_EACH_SIDE links at either end of the "current page" link.
page_range = []
if page_num > (ON_EACH_SIDE + ON_ENDS):
page_range.extend(range(0, ON_EACH_SIDE - 1))
page_range.append(DOT)
page_range.extend(range(page_num - ON_EACH_SIDE, page_num + 1))
else:
page_range.extend(range(0, page_num + 1))
if page_num < (paginator.num_pages - ON_EACH_SIDE - ON_ENDS - 1):
page_range.extend(
range(page_num + 1, page_num + ON_EACH_SIDE + 1))
page_range.append(DOT)
page_range.extend(
range(paginator.num_pages - ON_ENDS, paginator.num_pages))
else:
page_range.extend(range(page_num + 1, paginator.num_pages))

pagination_required = (not cl.show_all or not cl.can_show_all) and cl.multi_page # NEW
page_range = cl.paginator.get_elided_page_range(cl.page_num) if pagination_required else [] # NEW
need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page
return {
'cl': cl,
Expand Down