misc: adjust pagination links for corner cases (#14677)

This commit is contained in:
Frédéric Péters 2017-01-31 11:15:28 +01:00
parent 4f31842a06
commit c428b89bcd
2 changed files with 39 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import json
import pytest
import os
import pickle
import re
import time
import datetime
@ -17,12 +18,17 @@ from wcs.qommon.misc import (simplify, json_loads, parse_isotime, format_time,
from wcs.admin.settings import FileTypesDirectory
from wcs.scripts import Script
from wcs.qommon import evalutils
from wcs.qommon.http_request import HTTPRequest
from wcs.qommon.backoffice.listing import pagination_links
from utilities import get_app, create_temporary_pub
from utilities import get_app, create_temporary_pub, clean_temporary_pub
def setup_module(module):
cleanup()
def teardown_module(module):
clean_temporary_pub()
def test_parse_file_size():
assert FileSizeWidget.parse_file_size('17') == 17
assert FileSizeWidget.parse_file_size('17o') == 17
@ -236,3 +242,30 @@ def test_etld():
pub.load_effective_tld_names()
assert pub.etld.parse('www.example.net') == ('www.example', 'net')
assert pub.etld.parse('www.example.rhcloud.com') == ('www.example', 'rhcloud.com')
def test_pagination():
pub = create_temporary_pub()
req = HTTPRequest(None, {'SERVER_NAME': 'example.net', 'SCRIPT_NAME': ''})
req.response.filter = {}
pub.form = {'ajax': 'true'}
pub._set_request(req)
def get_texts(s):
return [x for x in re.findall(r'>(.*?)<', str(s)) if x.strip()]
assert get_texts(pagination_links(0, 10, 0)) == [
'1', '(0-0/0)', 'Per page: ', '10']
assert get_texts(pagination_links(0, 10, 10)) == [
'1', '(1-10/10)', 'Per page: ', '10']
assert get_texts(pagination_links(0, 10, 20)) == [
'1', '2', '(1-10/20)', 'Per page: ', '10', '20']
assert get_texts(pagination_links(10, 10, 20)) == [
'1', '2', '(11-20/20)', 'Per page: ', '10', '20']
assert get_texts(pagination_links(10, 10, 50)) == [
'1', '2', '3', '4', '5', '(11-20/50)', 'Per page: ', '10', '20', '50']
assert get_texts(pagination_links(10, 10, 500)) == [
'1', '2', '3', '4', '5', '6', '7', '&#8230;', '50', '(11-20/500)', 'Per page: ', '10', '20', '50', '100']
assert get_texts(pagination_links(100, 10, 500)) == [
'1', '&#8230;', '8', '9', '10', '11', '12', '13', '14', '&#8230;', '50', '(101-110/500)', 'Per page: ', '10', '20', '50', '100']
assert get_texts(pagination_links(100, 20, 500)) == [
'1', '&#8230;', '3', '4', '5', '6', '7', '8', '9', '&#8230;', '25', '(101-120/500)', 'Per page: ', '10', '20', '50', '100']

View File

@ -31,8 +31,6 @@ def pagination_links(offset, limit, total_count):
# link to previous page
query['offset'] = max(offset-limit, 0)
query['limit'] = limit
if 'ajax' in query:
del query['ajax']
r += htmltext('<a class="previous-page" data-limit="%s" data-offset="%s" href="?%s"><!--%s--></a>') % (
limit, query['offset'], urllib.urlencode(query, doseq=1), _('Previous Page'))
else:
@ -41,10 +39,12 @@ def pagination_links(offset, limit, total_count):
# display links to individual pages
page_range = 7
current_page = offset / limit + 1
last_page = (total_count-1) / limit + 1
last_page = max((total_count-1) / limit + 1, 1)
start = max(current_page - (page_range / 2), 1)
end = min(start + page_range - 1, last_page)
page_numbers = list(range(start, end + 1))
if not page_numbers:
page_numbers = [1]
if 1 not in page_numbers:
page_numbers.insert(0, 1)
if 2 not in page_numbers:
@ -84,7 +84,7 @@ def pagination_links(offset, limit, total_count):
r += htmltext('<span class="next-page"><!--%s--></span>') % _('Next Page')
r += htmltext(' <span class="displayed-range">(%s-%s/%s)</span> ') % (
offset + 1,
min(offset + 1, total_count),
min((offset + limit, total_count)),
total_count)
@ -97,7 +97,7 @@ def pagination_links(offset, limit, total_count):
else:
r += htmltext('<a data-limit="%s" data-offset="0"" href="?%s">%s</a>') % (
page_size, urllib.urlencode(query, doseq=1), page_size)
if page_size > total_count:
if page_size >= total_count:
break
r += htmltext('</div>')