Commits

James Rivett-Carnac committed f4e7929

added diario_pager from prior revision

  • Participants
  • Parent commits f9cc0a8
  • Branches lexical_hilight

Comments (0)

Files changed (1)

File diario/templatetags/diario_pager.py

+# -*- coding: utf-8 -*-
+#
+#  Copyright (c) 2007, 2008 Guilherme Mesquita Gondim and contributors
+#  
+#  This file is part of Django Diário.
+#
+#  Django Diário is free software under terms of the GNU Lesser
+#  General Public License version 3 (LGPLv3) as published by the Free
+#  Software Foundation. See the file README for copying conditions.
+#
+
+"""
+The ``diario.templatetags.diario_pager`` module defines a number of
+template tags which are used to generate pagination controls
+
+To access Diário pager template tags in a template use the {% load %}
+tag::
+
+    {% load diario_pager %}
+"""
+
+from django import template
+
+register = template.Library()
+
+class PaginationControlNode(template.Node):
+    pager_template = "diario/tags/pager_template.html"
+
+    def __init__(self, pager_variable_name, page):
+        self.pager_variable_name = template.Variable(pager_variable_name)
+        self.page_variable = template.Variable(page)
+
+    def render(self, context):
+        """
+        convert the variable name to an actual paginator, and render the
+        template
+        """
+        try:
+            self.pager_inst = self.pager_variable_name.resolve(context)
+            try:
+                self.page_num = int(self.page_variable.resolve(context))
+            except ValueError:
+                self.page_num = 1
+            has_next = self.pager_inst.page(self.page_num).has_next()
+            has_previous = self.pager_inst.page(self.page_num).has_previous()
+            sub_context = {
+                    'pager': self.pager_inst,
+                    'page': self.pager_inst.page(self.page_num),
+                    'current_page':self.page_num,
+                    'has_next':has_next,
+                    'has_previous':has_previous
+                    }
+            return template.loader.render_to_string(self.pager_template,
+                                                    sub_context)
+        except template.VariableDoesNotExist:
+            return ''
+
+
+def do_create_page_control(parser,token):
+    """Creates a page control for a view, using a paginator and the
+    current page.
+
+    Syntax::
+
+        {% create_page_control [pagination_instance] [current_page] %}
+
+    Example usage::
+
+        {% create_page_control latest 2 %}
+
+        {% create_page_control year_list 1 %}
+    """
+    try:
+        tag_name, pager_inst, page = token.contents.split()
+    except ValueError:
+        raise template.TemplateSyntaxError("create_page_control takes 2 args")
+    return PaginationControlNode(pager_inst,page)
+
+register.tag('create_pager_control',do_create_page_control)