Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added PAGINATION_QUERY_VAR #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pagination/middleware.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from django.conf import settings


def get_page(self):
"""
A function which will be monkeypatched onto the request to get the current
integer representing the current page.
"""
try:
return int(self.REQUEST['page'])
return int(self.GET[getattr(settings, "PAGINATION_QUERY_VAR", 'page')])
except (KeyError, ValueError, TypeError):
return 1


class PaginationMiddleware(object):
"""
Inserts a variable representing the current page onto the request object if
it exists in either **GET** or **POST** portions of the request.
"""
def process_request(self, request):
request.__class__.page = property(get_page)
request.__class__.page = property(get_page)
65 changes: 33 additions & 32 deletions pagination/templatetags/pagination_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
DEFAULT_PAGINATION = getattr(settings, 'PAGINATION_DEFAULT_PAGINATION', 20)
DEFAULT_WINDOW = getattr(settings, 'PAGINATION_DEFAULT_WINDOW', 4)
DEFAULT_ORPHANS = getattr(settings, 'PAGINATION_DEFAULT_ORPHANS', 0)
INVALID_PAGE_RAISES_404 = getattr(settings,
'PAGINATION_INVALID_PAGE_RAISES_404', False)
INVALID_PAGE_RAISES_404 = getattr(settings, 'PAGINATION_INVALID_PAGE_RAISES_404', False)


def do_autopaginate(parser, token):
"""
Expand All @@ -31,46 +31,43 @@ def do_autopaginate(parser, token):
try:
context_var = split[as_index + 1]
except IndexError:
raise template.TemplateSyntaxError("Context variable assignment " +
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],
context_var=context_var)
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,
context_var=context_var)
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):
"""
Emits the required objects to allow for Digg-style pagination.

First, it looks in the current context for the variable specified, and using
that object, it emits a simple ``Paginator`` and the current page object
that object, it emits a simple ``Paginator`` and the current page object
into the context names ``paginator`` and ``page_obj``, respectively.

It will then replace the variable specified with only the objects for the
current page.

.. note::

It is recommended to use *{% paginate %}* after using the autopaginate
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, context_var=None):
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
Expand All @@ -91,8 +88,7 @@ def render(self, context):
page_obj = paginator.page(context['request'].page)
except InvalidPage:
if INVALID_PAGE_RAISES_404:
raise Http404('Invalid page requested. If DEBUG were set to ' +
'False, an HTTP 404 page would have been shown instead.')
raise Http404('Invalid page requested. If DEBUG were set to False, an HTTP 404 page would have been shown instead.')
context[key] = []
context['invalid_page'] = True
return u''
Expand All @@ -111,20 +107,20 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
Digg-like display of the available pages, given the current page. If there
are too many pages to be displayed before and after the current page, then
elipses will be used to indicate the undisplayed gap between page numbers.

Requires one argument, ``context``, which should be a dictionary-like data
structure and must contain the following keys:

``paginator``
A ``Paginator`` or ``QuerySetPaginator`` object.

``page_obj``
This should be the result of calling the page method on the
This should be the result of calling the page method on the
aforementioned ``Paginator`` or ``QuerySetPaginator`` object, given
the current page.

This same ``context`` dictionary-like data structure may also include:

``getvars``
A dictionary of all of the **GET** parameters in the current request.
This is useful to maintain certain types of state, even when requesting
Expand All @@ -133,7 +129,7 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
try:
paginator = context['paginator']
page_obj = context['page_obj']
page_range = paginator.page_range
page_range = list(paginator.page_range)
# Calculate the record range in the current page for display.
records = {'first': 1 + (page_obj.number - 1) * paginator.per_page}
records['last'] = records['first'] + paginator.per_page - 1
Expand All @@ -145,10 +141,10 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
last = set(page_range[-window:])
# Now we look around our current page, making sure that we don't wrap
# around.
current_start = page_obj.number-1-window
current_start = page_obj.number - 1 - window
if current_start < 0:
current_start = 0
current_end = page_obj.number-1+window
current_end = page_obj.number - 1 + window
if current_end < 0:
current_end = 0
current = set(page_range[current_start:current_end])
Expand Down Expand Up @@ -187,7 +183,7 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
second_list.sort()
diff = second_list[0] - pages[-1]
# If there is a gap of two, between the last page of the current
# set and the first page of the last set, then we're missing a
# set and the first page of the last set, then we're missing a
# page.
if diff == 2:
pages.append(second_list[0] - 1)
Expand All @@ -214,13 +210,18 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
'is_paginated': paginator.count > paginator.per_page,
}
if 'request' in context:
query_var = getattr(settings, "PAGINATION_QUERY_VAR", 'page')
getvars = context['request'].GET.copy()
if 'page' in getvars:
del getvars['page']
if "_" in getvars:
del getvars["_"]
if query_var in getvars:
del getvars[query_var]
if len(getvars.keys()) > 0:
to_return['getvars'] = "&%s" % getvars.urlencode()
else:
to_return['getvars'] = ''
if 'link_prefix' in context:
to_return['link_prefix'] = context['link_prefix']
return to_return
except KeyError, AttributeError:
return {}
Expand Down