From: floguy Date: Fri, 24 Oct 2008 08:29:57 +0000 (+0000) Subject: Allow for a variable as the paginate_by parameter. Also added tests for this case... X-Git-Tag: 1.0.6~16 X-Git-Url: https://git.mdrn.pl/django-pagination.git/commitdiff_plain/8f76903ab93a01d3b636d8977025c14cfa6f6131?hp=--cc 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 --- 8f76903ab93a01d3b636d8977025c14cfa6f6131 diff --git a/pagination/templatetags/pagination_tags.py b/pagination/templatetags/pagination_tags.py index 32b3b64..01a956e 100644 --- a/pagination/templatetags/pagination_tags.py +++ b/pagination/templatetags/pagination_tags.py @@ -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: diff --git a/pagination/tests.py b/pagination/tests.py index 21ec9d2..1579166 100644 --- a/pagination/tests.py +++ b/pagination/tests.py @@ -48,5 +48,11 @@ u'\\n\\n