Permalink
Browse files

enable paginator tag to use custom template

  • Loading branch information...
1 parent ce28e0c commit 5ca2046cca60b5c1ea3159a30d0a0a1d23ab056c @HonzaKral HonzaKral committed Apr 12, 2012
Showing with 55 additions and 30 deletions.
  1. +33 −24 ella/core/templatetags/pagination.py
  2. +22 −6 test_ella/test_core/test_templatetags.py
@@ -1,36 +1,20 @@
from urllib import urlencode
from django import template
+from django.template.loader import render_to_string
from django.utils.encoding import smart_str
register = template.Library()
+def _do_paginator(context, adjacent_pages, template_name):
+ if template_name is None:
+ template_name = 'inclusion_tags/paginator.html'
+ else:
+ template_name = 'inclusion_tags/paginator_%s.html' % template_name
-@register.inclusion_tag('inclusion_tags/paginator.html', takes_context=True)
-def paginator(context, adjacent_pages=2):
- """
- Renders a ``inclusion_tags/paginator.html`` template with additional
- pagination context. To be used in conjunction with the ``object_list`` generic
- view.
-
- Adds pagination context variables for use in displaying first, adjacent pages and
- last page links in addition to those created by the ``object_list`` generic
- view.
-
- Taken from http://www.djangosnippets.org/snippets/73/
-
- Syntax::
-
- {% paginator[ <NUMBER_OF_ADJACENT_PAGES] %}
-
- Examples::
-
- {% paginator %}
- {% paginator 5 %}
- """
if not 'page' in context:
# improper use of paginator tag, bail out
- return {}
+ return template_name, {}
query_params = '?p='
if 'request' in context:
@@ -44,7 +28,7 @@ def paginator(context, adjacent_pages=2):
page.paginator.num_pages))
page_numbers = range(s, min(page.paginator.num_pages, s + 2 * adjacent_pages) + 1)
- return {
+ return template_name, {
'query_params': query_params,
'page': page,
'results_per_page': page.paginator.per_page,
@@ -53,3 +37,28 @@ def paginator(context, adjacent_pages=2):
'show_last': page.paginator.num_pages not in page_numbers,
}
+
+@register.simple_tag(takes_context=True)
+def paginator(context, adjacent_pages=2, template_name=None):
+ """
+ Renders a ``inclusion_tags/paginator.html`` template with additional
+ pagination context. To be used in conjunction with the ``object_list`` generic
+ view.
+
+ Adds pagination context variables for use in displaying first, adjacent pages and
+ last page links in addition to those created by the ``object_list`` generic
+ view.
+
+ Taken from http://www.djangosnippets.org/snippets/73/
+
+ Syntax::
+
+ {% paginator[ <NUMBER_OF_ADJACENT_PAGES] %}
+
+ Examples::
+
+ {% paginator %}
+ {% paginator 5 %}
+ """
+ tname, context = _do_paginator(context, adjacent_pages, template_name)
+ return render_to_string(tname, context)
@@ -11,7 +11,7 @@
from django.core.paginator import Paginator
from ella.core.templatetags.core import listing_parse, ListingNode, _parse_box, BoxNode, EmptyNode
-from ella.core.templatetags.pagination import paginator
+from ella.core.templatetags.pagination import _do_paginator
from ella.core.models import Category
from ella.core.managers import ListingHandler
from ella.articles.models import Article
@@ -27,6 +27,10 @@ def setUp(self):
super(TestPaginate, self).setUp()
self.rf = RequestFactory()
+ def tearDown(self):
+ super(TestPaginate, self).tearDown()
+ template_loader.templates = {}
+
def test_all_querysting_is_included(self):
req = self.rf.get('/', {'using': 'custom_lh', 'other': 'param with spaces'})
page = Paginator(range(100), 10).page(2)
@@ -36,28 +40,40 @@ def test_all_querysting_is_included(self):
'page': page
}
- tools.assert_equals({
+ tools.assert_equals(('inclusion_tags/paginator.html', {
'page': page,
'page_numbers': [1, 2, 3, 4, 5],
'query_params': '?using=custom_lh&other=param+with+spaces&p=',
'results_per_page': 10,
'show_first': False,
'show_last': True
- }, paginator(context, 2))
+ }), _do_paginator(context, 2, None))
def test_always_include_given_number_of_pages(self):
page = Paginator(range(100), 9).page(1)
- tools.assert_equals({
+ tools.assert_equals(('inclusion_tags/paginator_special.html',{
'page': page,
'page_numbers': [1, 2, 3, 4, 5, 6, 7],
'query_params': '?p=',
'results_per_page': 9,
'show_first': False,
'show_last': True
- }, paginator({'page': page}, 3))
+ }), _do_paginator({'page': page}, 3, 'special'))
def test_dont_fail_on_missing_page(self):
- tools.assert_equals({}, paginator({}))
+ tools.assert_equals(('inclusion_tags/paginator.html', {}), _do_paginator({}, 2, None))
+
+ def test_proper_template_gets_rendered(self):
+ template_loader.templates['inclusion_tags/paginator_special.html'] = 'special'
+ t = template.Template('{% load pagination %}{% paginator template_name="special" %}')
+ tools.assert_equals('special', t.render(template.Context()))
+
+ def test_adjacent_places_get_passed_from_template(self):
+ page = Paginator(range(100), 9).page(1)
+ template_loader.templates['inclusion_tags/paginator.html'] = '{{ page_numbers|join:", "}}'
+ t = template.Template('{% load pagination %}{% paginator 1 %}')
+ tools.assert_equals('1, 2, 3', t.render(template.Context({'page': page})))
+

0 comments on commit 5ca2046

Please sign in to comment.