Skip to content

Commit

Permalink
Added the ability to save the result list as a variable in the templa…
Browse files Browse the repository at this point in the history
…te's context. Thanks for the original inspiration for this patch, n.leush.

git-svn-id: https://django-pagination.googlecode.com/svn/trunk@42 7f1efe38-554e-0410-b69d-834cb44da2d5
  • Loading branch information
floguy committed Oct 24, 2008
1 parent 8f76903 commit 3ff8109
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
34 changes: 28 additions & 6 deletions pagination/templatetags/pagination_tags.py
Expand Up @@ -18,18 +18,35 @@ def do_autopaginate(parser, token):
Splits the arguments to the autopaginate tag and formats them correctly.
"""
split = token.split_contents()
as_index = None
context_var = None
for i, bit in enumerate(split):
if bit == 'as':
as_index = i
break
if as_index is not None:
try:
context_var = split[as_index + 1]
except IndexError:
raise template.TemplateSyntaxError("Context variable assignment " +\
"must take the form of {%% %r object.example_set.all ... as " +\
"context_var_name %%}" % split[0])
del split[as_index:as_index + 2]
if len(split) == 2:
return AutoPaginateNode(split[1])
elif len(split) == 3:
return AutoPaginateNode(split[1], paginate_by=split[2])
return AutoPaginateNode(split[1], paginate_by=split[2],
context_var=context_var)
elif len(split) == 4:
try:
orphans = int(split[3])
except ValueError:
raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % split[3])
return AutoPaginateNode(split[1], paginate_by=split[2], orphans=orphans)
raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % split[3])
return AutoPaginateNode(split[1], paginate_by=split[2], orphans=orphans,
context_var=context_var)
else:
raise template.TemplateSyntaxError('%r tag takes one required argument and one optional argument' % split[0])
raise template.TemplateSyntaxError('%r tag takes one required ' + \
'argument and one optional argument' % split[0])

class AutoPaginateNode(template.Node):
"""
Expand All @@ -48,13 +65,15 @@ class AutoPaginateNode(template.Node):
tag. If you choose not to use *{% paginate %}*, make sure to display the
list of available pages, or else the application may seem to be buggy.
"""
def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION, orphans=DEFAULT_ORPHANS):
def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION,
orphans=DEFAULT_ORPHANS, context_var=None):
self.queryset_var = template.Variable(queryset_var)
if isinstance(paginate_by, int):
self.paginate_by = paginate_by
else:
self.paginate_by = template.Variable(paginate_by)
self.orphans = orphans
self.context_var = context_var

def render(self, context):
key = self.queryset_var.var
Expand All @@ -70,7 +89,10 @@ def render(self, context):
context[key] = []
context['invalid_page'] = True
return u''
context[key] = page_obj.object_list
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context[key] = page_obj.object_list
context['paginator'] = paginator
context['page_obj'] = page_obj
return u''
Expand Down
3 changes: 3 additions & 0 deletions pagination/tests.py
Expand Up @@ -54,5 +54,8 @@
>>> 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">...
>>> t = Template("{% load pagination_tags %}{% autopaginate var by as foo %}{{ foo }}")
>>> t.render(Context({'var': range(21), 'by': 20, 'request': RequestProxy()}))
u'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]'
>>>
"""

0 comments on commit 3ff8109

Please sign in to comment.