X-Git-Url: https://git.mdrn.pl/django-pagination.git/blobdiff_plain/301bedcc8073d960a6cb88515cabcc4e472be2a4..5432e8fe148352116ca3c7cbdc96d8cf7e677dc5:/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 815cb7f..f106942 100644 --- a/linaro_django_pagination/templatetags/pagination_tags.py +++ b/linaro_django_pagination/templatetags/pagination_tags.py @@ -37,11 +37,18 @@ 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 # TODO, import this normally later on from linaro_django_pagination.settings import * @@ -58,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 @@ -67,33 +74,33 @@ 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: raise TemplateSyntaxError( "Invalid syntax. Proper usage of this tag is: " - "{%% autopaginate QUERYSET [PAGINATE_BY] [ORPHANS]" - " [as CONTEXT_VAR_NAME] %%}") + "{% autopaginate QUERYSET [PAGINATE_BY] [ORPHANS]" + " [as CONTEXT_VAR_NAME] %}") return AutoPaginateNode(queryset_var, multiple_paginations, paginate_by, orphans, context_var) @@ -133,7 +140,11 @@ class AutoPaginateNode(Node): self.multiple_paginations = multiple_paginations def render(self, context): - if self.multiple_paginations or "paginator" in context: + # Save multiple_paginations state in context + if self.multiple_paginations and 'multiple_paginations' not in context: + context['multiple_paginations'] = True + + if context.get('multiple_paginations') or getattr(context, "paginator", None): page_suffix = '_%s' % self.queryset_var else: page_suffix = '' @@ -163,7 +174,7 @@ class AutoPaginateNode(Node): 'False, an HTTP 404 page would have been shown instead.') context[key] = [] context['invalid_page'] = True - return u'' + return '' if self.context_var is not None: context[self.context_var] = page_obj.object_list else: @@ -171,7 +182,7 @@ class AutoPaginateNode(Node): context['paginator'] = paginator context['page_obj'] = page_obj context['page_suffix'] = page_suffix - return u'' + return '' class PaginateNode(Node): @@ -181,32 +192,36 @@ 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) - t = select_template(template_list) - if not t: - return None - context = Context(to_return) - return t.render(context) + return loader.render_to_string(template_list, new_context, + context_instance = context) + def do_paginate(parser, token): """ - {% paginate [using] [template] %} + Emits the pagination control for the most recent autopaginate list - {% paginate %} - {% paginate using paginations/custom_pagination.html %} + Syntax is: + + paginate [using "TEMPLATE"] + + Where TEMPLATE is a quoted template name. If missing the default template + is used (paginate/pagination.html). """ - argv = token.contents.split() + argv = token.split_contents() argc = len(argv) - if argc > 3: - raise TemplateSyntaxError("Tag %s takes at most 2 argument." % argv[0]) if argc == 1: - return PaginateNode() - if argc == 3 and argv[1] == 'using': - return PaginateNode(template=argv[2]) - raise TemplateSyntaxError("Tag %s is invalid. Please check the syntax" % argv[0]) + template = None + elif argc == 3 and argv[1] == 'using': + template = unescape_string_literal(argv[2]) + else: + raise TemplateSyntaxError( + "Invalid syntax. Proper usage of this tag is: " + "{% paginate [using \"TEMPLATE\"] %}") + return PaginateNode(template) def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN): @@ -253,7 +268,7 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN): paginator = context['paginator'] page_obj = context['page_obj'] page_suffix = context.get('page_suffix', '') - 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 @@ -269,7 +284,7 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN): window_end = window_end - window_start window_start = 0 if window_end > paginator.num_pages: - window_start = window_start - (window_end - paginator.num_pages) + window_start = max(0, window_start - (window_end - paginator.num_pages)) window_end = paginator.num_pages pages = page_range[window_start:window_end] @@ -297,9 +312,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, @@ -317,10 +333,10 @@ 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): return {}