From bad1d3d94971e05e5879b0f588b4b1a84d6ddaaf Mon Sep 17 00:00:00 2001 From: floguy Date: Thu, 29 May 2008 05:35:43 +0000 Subject: [PATCH] Version 0.1 alive and kicking! git-svn-id: https://django-pagination.googlecode.com/svn/trunk@6 7f1efe38-554e-0410-b69d-834cb44da2d5 --- .../templates/pagination/pagination.html | 4 +- pagination/templatetags/pagination_tags.py | 87 ++++++++++++++++--- 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/pagination/templates/pagination/pagination.html b/pagination/templates/pagination/pagination.html index ac7ad68..fbb678d 100644 --- a/pagination/templates/pagination/pagination.html +++ b/pagination/templates/pagination/pagination.html @@ -1,3 +1,4 @@ +{% if is_paginated %} \ No newline at end of file + +{% endif %} \ No newline at end of file diff --git a/pagination/templatetags/pagination_tags.py b/pagination/templatetags/pagination_tags.py index 0bd9805..7c6c94d 100644 --- a/pagination/templatetags/pagination_tags.py +++ b/pagination/templatetags/pagination_tags.py @@ -2,34 +2,99 @@ try: set except NameError: from sets import Set as set +from django import template +from pagination.registration import get_registry, default_pagination +registry = get_registry() +from django.db.models.query import QuerySet +from django.core.paginator import QuerySetPaginator, InvalidPage +#from django.template.loader import render_to_string + +register = template.Library() + +def do_autopaginate(parser, token): + split = token.split_contents() + if len(split) == 1: + return AutoPaginateNode() + elif len(split) == 2: + return AutoPaginateNode(queryset_var=split[1]) + else: + raise template.TemplateSyntaxError('%r tag takes only one optional argument.' % split[0]) + +class AutoPaginateNode(template.Node): + def __init__(self, queryset_var=None): + if queryset_var: + self.queryset_var = template.Variable(queryset_var) + else: + self.queryset_var = None + + def render(self, context): + if self.queryset_var is not None: + try: + key = self.queryset_var.var + value = self.queryset_var.resolve(context) + model = value.model + pagination = registry.get_for_model(model) + if pagination is None: + pagination = default_pagination + paginator = QuerySetPaginator(value, pagination) + try: + page_obj = paginator.page(context['request'].page) + except: + return u'' + context[key] = page_obj.object_list + context['paginator'] = paginator + context['page_obj'] = page_obj + return u'' + except template.VariableDoesNotExist: + pass + for d in context: + for key, value in d.iteritems(): + if issubclass(value.__class__, QuerySet): + model = value.model + pagination = registry.get_for_model(model) + if pagination is not None: + paginator = QuerySetPaginator(value, pagination) + try: + page_obj = paginator.page(context['request'].page) + except: + return u'' + context[key] = page_obj.object_list + context['paginator'] = paginator + context['page_obj'] = page_obj + return u'' + return u'' def paginate(context, window=4): paginator = context['paginator'] page_obj = context['page_obj'] page_range = paginator.page_range first = set(page_range[:window]) - last_start = len(page_range)-window - if last_start < 0: - last_start = 0 - last = set(page_range[last_start:]) + last = set(page_range[-window:]) current_start = page_obj.number-1-window if current_start < 0: current_start = 0 - current = set(page_range[current_start: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]) pages = [] if len(first.intersection(current)) == 0: - pages.extend(list(first)) + pages.extend(sorted(list(first))) pages.append(None) - pages.extend(list(current)) + pages.extend(sorted(list(current))) else: - pages.extend(first.union(current)) + pages.extend(sorted(list(first.union(current)))) if len(current.intersection(last)) == 0: pages.append(None) - pages.extend(list(last)) + pages.extend(sorted(list(last))) else: - pages.extend(list(last.difference(current))) + pages.extend(sorted(list(last.difference(current)))) return { 'pages': pages, 'page_obj': page_obj, 'paginator': paginator, - } \ No newline at end of file + 'is_paginated': paginator.count > paginator.per_page, + } + +register.inclusion_tag('pagination/pagination.html', takes_context=True)(paginate) +register.tag('autopaginate', do_autopaginate) -- 2.20.1