~netlandish/django-pagination

8f76903ab93a01d3b636d8977025c14cfa6f6131 — floguy 16 years ago ad03303
Allow for a variable as the paginate_by parameter.  Also added tests for this case.  Thanks, lfrodrigues.

git-svn-id: https://django-pagination.googlecode.com/svn/trunk@41 7f1efe38-554e-0410-b69d-834cb44da2d5
2 files changed, 17 insertions(+), 12 deletions(-)

M pagination/templatetags/pagination_tags.py
M pagination/tests.py
M pagination/templatetags/pagination_tags.py => pagination/templatetags/pagination_tags.py +11 -12
@@ 21,21 21,13 @@ def do_autopaginate(parser, token):
    if len(split) == 2:
        return AutoPaginateNode(split[1])
    elif len(split) == 3:
        try:
            paginate_by = int(split[2])
        except ValueError:
            raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % split[2])
        return AutoPaginateNode(split[1], paginate_by=paginate_by)
        return AutoPaginateNode(split[1], paginate_by=split[2])
    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)
        return AutoPaginateNode(split[1], paginate_by=split[2], orphans=orphans)
    else:
        raise template.TemplateSyntaxError('%r tag takes one required argument and one optional argument' % split[0])



@@ 58,13 50,20 @@ class AutoPaginateNode(template.Node):
    """
    def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION, orphans=DEFAULT_ORPHANS):
        self.queryset_var = template.Variable(queryset_var)
        self.paginate_by = paginate_by
        if isinstance(paginate_by, int):
            self.paginate_by = paginate_by
        else:
            self.paginate_by = template.Variable(paginate_by)
        self.orphans = orphans

    def render(self, context):
        key = self.queryset_var.var
        value = self.queryset_var.resolve(context)
        paginator = Paginator(value, self.paginate_by, self.orphans)
        if isinstance(self.paginate_by, int):
            paginate_by = self.paginate_by
        else:
            paginate_by = self.paginate_by.resolve(context)
        paginator = Paginator(value, paginate_by, self.orphans)
        try:
            page_obj = paginator.page(context['request'].page)
        except InvalidPage:

M pagination/tests.py => pagination/tests.py +6 -0
@@ 48,5 48,11 @@ u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var %}{% paginate %}")
>>> t.render(Context({'var': range(21), 'request': RequestProxy()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var 20 %}{% paginate %}")
>>> t.render(Context({'var': range(21), 'request': RequestProxy()}))
u'\\n\\n<div class="pagination">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var by %}{% paginate %}")
>>> t.render(Context({'var': range(21), 'by': 20, 'request': RequestProxy()}))
u'\\n\\n<div class="pagination">...
>>>
"""
\ No newline at end of file