Version 0.1 alive and kicking!
authorfloguy <floguy@7f1efe38-554e-0410-b69d-834cb44da2d5>
Thu, 29 May 2008 05:35:43 +0000 (05:35 +0000)
committerfloguy <floguy@7f1efe38-554e-0410-b69d-834cb44da2d5>
Thu, 29 May 2008 05:35:43 +0000 (05:35 +0000)
git-svn-id: https://django-pagination.googlecode.com/svn/trunk@6 7f1efe38-554e-0410-b69d-834cb44da2d5

pagination/templates/pagination/pagination.html
pagination/templatetags/pagination_tags.py

index ac7ad68..fbb678d 100644 (file)
@@ -1,3 +1,4 @@
+{% if is_paginated %}
 <div class="pagination">
     {% if page_obj.has_previous %}
         <a href="?page={{ page_obj.previous_page_number }}">&lsaquo;&lsaquo; previous</a>
@@ -20,4 +21,5 @@
     {% else %}
         <span class="disabled">next &rsaquo;&rsaquo;</span>
     {% endif %}
-</div>
\ No newline at end of file
+</div>
+{% endif %}
\ No newline at end of file
index 0bd9805..7c6c94d 100644 (file)
@@ -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)