~netlandish/django-pagination

a35ff9bd925bb5fd3ab2e43cae19a62afb2f5701 — floguy 16 years ago 4d9362d
Added test suite, and prevented pagination if the elusion would take up only one space.


git-svn-id: https://django-pagination.googlecode.com/svn/trunk@13 7f1efe38-554e-0410-b69d-834cb44da2d5
M pagination/templatetags/pagination_tags.py => pagination/templatetags/pagination_tags.py +13 -3
@@ 92,14 92,24 @@ def paginate(context, window=4):
            first_list = sorted(list(first))
            second_list = sorted(list(current))
            pages.extend(first_list)
            if first_list[-1] + 1 != second_list[0]:
            diff = second_list[0] - first_list[-1] 
            if diff == 2:
                pages.append(second_list[0] - 1)
            elif diff == 1:
                pass
            else:
                pages.append(None)
            pages.extend(second_list)
        else:
            pages.extend(sorted(list(first.union(current))))
        if len(current.intersection(last)) == 0:
            second_list = sorted(list(last))
            if pages[-1] + 1 != second_list[0]:
            diff = second_list[0] - pages[-1]
            if diff == 2:
                pages.append(second_list[0] - 1)
            elif diff == 1:
                pass
            else:
                pages.append(None)
            pages.extend(second_list)
        else:


@@ 113,4 123,4 @@ def paginate(context, window=4):
    except KeyError:
        return u''
register.inclusion_tag('pagination/pagination.html', takes_context=True)(paginate)
register.tag('autopaginate', do_autopaginate)
register.tag('autopaginate', do_autopaginate)
\ No newline at end of file

A pagination/tests.py => pagination/tests.py +20 -0
@@ 0,0 1,20 @@
"""
>>> from django.core.paginator import Paginator
>>> from pagination.templatetags.pagination_tags import paginate

>>> p = Paginator(range(15), 2)
>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
[1, 2, 3, 4, 5, 6, 7, 8]

>>> p = Paginator(range(17), 2)
>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> p = Paginator(range(19), 2)
>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
[1, 2, 3, 4, None, 7, 8, 9, 10]

>>> p = Paginator(range(21), 2)
>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages']
[1, 2, 3, 4, None, 8, 9, 10, 11]
"""
\ No newline at end of file

A tests/runtests.py => tests/runtests.py +16 -0
@@ 0,0 1,16 @@
import sys
sys.path.append('..')

import os
# Make a backup of DJANGO_SETTINGS_MODULE environment variable to restore later.
backup = os.environ.get('DJANGO_SETTINGS_MODULE', '')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from django.test.simple import run_tests

if __name__ == "__main__":
    failures = run_tests(['pagination',], verbosity=9)
    if failures:
        sys.exit(failures)
    # Reset the DJANGO_SETTINGS_MODULE to what it was before running tests.
    os.environ['DJANGO_SETTINGS_MODULE'] = backup

A tests/settings.py => tests/settings.py +6 -0
@@ 0,0 1,6 @@
DATABASE_ENGINE = 'sqlite3'
ROOT_URLCONF = ''
SITE_ID = 1
INSTALLED_APPS = (
    'pagination',
)