Allow ignored get vars.
[django-pagination.git] / fnp_django_pagination / templatetags / pagination_tags.py
index 015e19c..b47bd12 100644 (file)
@@ -142,7 +142,9 @@ class AutoPaginateNode(Node):
         if self.multiple_paginations and 'multiple_paginations' not in context:
             context['multiple_paginations'] = True
 
-        if context.get('multiple_paginations') or getattr(context, "paginator", None):
+        if context.get('page_suffix'):
+            page_suffix = context['page_suffix']
+        elif context.get('multiple_paginations') or getattr(context, "paginator", None):
             page_suffix = '_%s' % self.queryset_var
         else:
             page_suffix = ''
@@ -185,12 +187,14 @@ class AutoPaginateNode(Node):
 
 class PaginateNode(Node):
 
-    def __init__(self, template=None):
+    def __init__(self, template=None, window=None, margin=None):
         self.template = template
+        self.window = window
+        self.margin = margin
 
     def render(self, context):
         template_list = ['pagination/pagination.html']
-        new_context = paginate(context)
+        new_context = paginate(context, window=self.window, margin=self.margin)
         if self.template:
             template_list.insert(0, self.template)
         return loader.render_to_string(template_list, new_context)
@@ -202,25 +206,39 @@ def do_paginate(parser, token):
 
     Syntax is:
 
-        paginate [using "TEMPLATE"]
+        paginate [using "TEMPLATE"] [window N] [margin N]
 
     Where TEMPLATE is a quoted template name. If missing the default template
     is used (paginate/pagination.html).
     """
     argv = token.split_contents()
     argc = len(argv)
-    if argc == 1:
-        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)
+    template = None
+    window = DEFAULT_WINDOW
+    margin = DEFAULT_MARGIN
+    ignored_vars = []
+    i = 1
+    while i < argc:
+        if argv[i] == 'using':
+            template = unescape_string_literal(argv[i + 1])
+            i += 2
+        elif argv[i] == 'window':
+            window = argv[i + 1]
+            i += 2
+        elif argv[i] == 'margin':
+            margin = argv[i + 1]
+            i += 2
+        elif argv[i] == 'ignore':
+            ignored_vars.append(argv[i + 1])
+            i += 2
+        else:
+            raise TemplateSyntaxError(
+                "Invalid syntax. Proper usage of this tag is: "
+                "{% paginate [using \"TEMPLATE\"] %}")
+    return PaginateNode(template, window, margin, ignored_vars)
 
 
-def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN):
+def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN, ignored_vars=None):
     """
     Renders the ``pagination/pagination.html`` template, resulting in a
     Digg-like display of the available pages, given the current page.  If there
@@ -256,6 +274,15 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN):
         window=2, margin=0, current=11     ... 7 8 9 10 [11]
         """
 
+    try:
+        window = int(window)
+    except ValueError:
+        window = Variable(window).resolve(context)
+    try:
+        margin = int(margin)
+    except ValueError:
+        margin = Variable(margin).resolve(context)
+
     if window < 0:
         raise ValueError('Parameter "window" cannot be less than zero')
     if margin < 0:
@@ -326,6 +353,10 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN):
         }
         if 'request' in context:
             getvars = context['request'].GET.copy()
+            if ignored_vars:
+                for v in ignored_vars:
+                    if v in getvars:
+                        del getvars[v]
             if 'page%s' % page_suffix in getvars:
                 del getvars['page%s' % page_suffix]
             if len(getvars.keys()) > 0: