X-Git-Url: https://git.mdrn.pl/django-pagination.git/blobdiff_plain/b5f7065624d3f212ef9e22911478011789cc32b0..862f40cfd7a0b8df475b5542c518d7916ce6391e:/linaro_django_pagination/templatetags/pagination_tags.py diff --git a/linaro_django_pagination/templatetags/pagination_tags.py b/linaro_django_pagination/templatetags/pagination_tags.py index 15363d8..da22e95 100644 --- a/linaro_django_pagination/templatetags/pagination_tags.py +++ b/linaro_django_pagination/templatetags/pagination_tags.py @@ -37,11 +37,16 @@ from django.template import ( Context, Library, Node, - TOKEN_BLOCK, TemplateSyntaxError, Variable, loader, ) + +try: + from django.template.base import TOKEN_BLOCK +except ImportError: # Django < 1.8 + from django.template import TOKEN_BLOCK + from django.template.loader import select_template from django.utils.text import unescape_string_literal @@ -60,7 +65,7 @@ def do_autopaginate(parser, token): # 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 = len([tok for tok in parser.tokens if expr(tok)]) > 0 i = iter(token.split_contents()) paginate_by = None @@ -69,26 +74,26 @@ def do_autopaginate(parser, token): orphans = None word = None try: - word = i.next() + word = next(i) assert word == "autopaginate" - queryset_var = i.next() - word = i.next() + queryset_var = next(i) + word = next(i) if word != "as": paginate_by = word try: paginate_by = int(paginate_by) except ValueError: pass - word = i.next() + word = next(i) if word != "as": orphans = word try: orphans = int(orphans) except ValueError: pass - word = i.next() + word = next(i) assert word == "as" - context_var = i.next() + context_var = next(i) except StopIteration: pass if queryset_var is None: @@ -183,10 +188,10 @@ class PaginateNode(Node): def render(self, context): template_list = ['pagination/pagination.html'] - to_return = paginate(context) + new_context = paginate(context) if self.template: template_list.insert(0, self.template) - return loader.render_to_string(template_list, to_return, + return loader.render_to_string(template_list, new_context, context_instance = context) @@ -303,9 +308,10 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN): if pages[-1] != paginator.num_pages: pages.append(None) - to_return = { + new_context = { 'MEDIA_URL': settings.MEDIA_URL, 'STATIC_URL': getattr(settings, "STATIC_URL", None), + 'disable_link_for_first_page': DISABLE_LINK_FOR_FIRST_PAGE, 'display_disabled_next_link': DISPLAY_DISABLED_NEXT_LINK, 'display_disabled_previous_link': DISPLAY_DISABLED_PREVIOUS_LINK, 'display_page_links': DISPLAY_PAGE_LINKS, @@ -323,16 +329,12 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN): if 'page%s' % page_suffix in getvars: del getvars['page%s' % page_suffix] if len(getvars.keys()) > 0: - to_return['getvars'] = "&%s" % getvars.urlencode() + new_context['getvars'] = "&%s" % getvars.urlencode() else: - to_return['getvars'] = '' - return to_return + new_context['getvars'] = '' + return new_context except (KeyError, AttributeError): - to_return = {} - - context.update(to_return) - - return context + return {} register = Library()