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

Django 1.7, 1.8 and Python 3.x compatibility #45

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
dist
*.egg-info
build
20 changes: 20 additions & 0 deletions pagination/paginator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
from django.core.paginator import Paginator, Page, PageNotAnInteger, EmptyPage


class ConcretePaginator(Paginator):
"""
Fix on the Django paginator that now uses an xrange, which isn't
compatible with the slicing that goes on in the template tags here.
"""

def _get_page_range(self):
"""
Returns a 1-based range of pages for iterating through within
a template for loop.
"""
r = super(ConcretePaginator, self)._get_page_range()
if not isinstance(r, list):
r = list(r)
return r
page_range = property(_get_page_range)



class InfinitePaginator(Paginator):
"""
Paginator designed for cases when it's not important to know how many total
Expand Down
19 changes: 11 additions & 8 deletions pagination/templatetags/pagination_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from sets import Set as set

from django import template
from django.template import TOKEN_BLOCK
from django.template.base import TOKEN_BLOCK
from django.http import Http404
from django.core.paginator import Paginator, InvalidPage
from django.core.paginator import InvalidPage
from django.conf import settings

from pagination.paginator import ConcretePaginator


register = template.Library()

DEFAULT_PAGINATION = getattr(settings, 'PAGINATION_DEFAULT_PAGINATION', 20)
Expand All @@ -21,12 +24,12 @@ def do_autopaginate(parser, token):
"""
Splits the arguments to the autopaginate tag and formats them correctly.
"""

# Check whether there are any other autopaginations are later in this template
expr = lambda obj: (obj.token_type == TOKEN_BLOCK and \
len(obj.split_contents()) > 0 and obj.split_contents()[0] == "autopaginate")
multiple_paginations = len(filter(expr, parser.tokens)) > 0


multiple_paginations = any((expr(tok) for tok in parser.tokens))

split = token.split_contents()
as_index = None
context_var = None
Expand Down Expand Up @@ -99,7 +102,7 @@ def render(self, context):
paginate_by = self.paginate_by
else:
paginate_by = self.paginate_by.resolve(context)
paginator = Paginator(value, paginate_by, self.orphans)
paginator = ConcretePaginator(value, paginate_by, self.orphans)
try:
page_obj = paginator.page(context['request'].page(page_suffix))
except InvalidPage:
Expand Down Expand Up @@ -221,7 +224,6 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
pages.extend(differenced)
to_return = {
'MEDIA_URL': settings.MEDIA_URL,
'request': context['request'],
'pages': pages,
'records': records,
'page_obj': page_obj,
Expand All @@ -231,6 +233,7 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
'page_suffix': page_suffix,
}
if 'request' in context:
to_return['request'] = context['request']
getvars = context['request'].GET.copy()
if 'page%s' % page_suffix in getvars:
del getvars['page%s' % page_suffix]
Expand All @@ -239,7 +242,7 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
else:
to_return['getvars'] = ''
return to_return
except KeyError, AttributeError:
except (KeyError, AttributeError):
return {}

register.inclusion_tag('pagination/pagination.html', takes_context=True)(
Expand Down
Loading