From c565d4651fa795631819d92a125bb4a44572f368 Mon Sep 17 00:00:00 2001 From: floguy Date: Mon, 21 Jul 2008 09:03:14 +0000 Subject: [PATCH] Added support for orphans. Thanks, Martin Mahner. git-svn-id: https://django-pagination.googlecode.com/svn/trunk@23 7f1efe38-554e-0410-b69d-834cb44da2d5 --- pagination/templatetags/pagination_tags.py | 16 ++++++++++++++-- pagination/tests.py | 9 +++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pagination/templatetags/pagination_tags.py b/pagination/templatetags/pagination_tags.py index ab00df7..55e5392 100644 --- a/pagination/templatetags/pagination_tags.py +++ b/pagination/templatetags/pagination_tags.py @@ -10,6 +10,7 @@ register = template.Library() DEFAULT_PAGINATION = 20 DEFAULT_WINDOW = 4 +DEFAULT_ORPHANS = 0 def do_autopaginate(parser, token): """ @@ -24,6 +25,16 @@ def do_autopaginate(parser, token): except ValueError: raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % split[2]) return AutoPaginateNode(split[1], paginate_by=paginate_by) + elif len(split) == 4: + try: + paginate_by = int(split[2]) + except ValueError: + raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % split[2]) + try: + orphans = int(split[3]) + except ValueError: + raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % split[3]) + return AutoPaginateNode(split[1], paginate_by=paginate_by, orphans=orphans) else: raise template.TemplateSyntaxError('%r tag takes one required argument and one optional argument' % split[0]) @@ -51,9 +62,10 @@ class AutoPaginateNode(template.Node): tag. If you choose not to use *{% paginate %}*, make sure to display the list of availabale pages, or else the application may seem to be buggy. """ - def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION): + def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION, orphans=DEFAULT_ORPHANS): self.queryset_var = template.Variable(queryset_var) self.paginate_by = paginate_by + self.orphans = orphans def render(self, context): key = self.queryset_var.var @@ -68,7 +80,7 @@ class AutoPaginateNode(template.Node): except IndexError: return u'' paginator_class = Paginator - paginator = paginator_class(value, self.paginate_by) + paginator = paginator_class(value, self.paginate_by, self.orphans) try: page_obj = paginator.page(context['request'].page) except InvalidPage: diff --git a/pagination/tests.py b/pagination/tests.py index 1d00f90..837e55c 100644 --- a/pagination/tests.py +++ b/pagination/tests.py @@ -19,6 +19,15 @@ >>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages'] [1, 2, 3, 4, None, 8, 9, 10, 11] +# Testing orphans +>>> p = Paginator(range(5), 2, 1) +>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages'] +[1, 2] + +>>> p = Paginator(range(21), 2, 1) +>>> paginate({'paginator': p, 'page_obj': p.page(1)})['pages'] +[1, 2, 3, 4, None, 7, 8, 9, 10] + >>> t = Template("{% load pagination_tags %}{% autopaginate var 2 %}{% paginate %}") # WARNING: Please, please nobody read this portion of the code! -- 2.20.1