Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Work with fresh kwargs in tag render methods.

Manipulating self.kwargs in render methods caused problems when using
more than one pager per template context. It probably wasn't thread-safe
either.
  • Loading branch information...
commit b4a03c401eb54efd94691f9c7ace5eda1c500fde 1 parent 14eeb56
@fairview fairview authored
Showing with 14 additions and 17 deletions.
  1. +14 −17 bootstrap-pagination/templatetags/bootstrap_pagination.py
View
31 bootstrap-pagination/templatetags/bootstrap_pagination.py
@@ -1,16 +1,14 @@
import re
from django.core.urlresolvers import reverse, NoReverseMatch
-from django.template import Context, Node, Library
+from django.template import Context, Node, Library, TemplateSyntaxError, VariableDoesNotExist
from django.template.loader import get_template
-from django.template import FilterExpression
-from django.template.defaultfilters import stringfilter
-from django.utils.encoding import force_unicode
from django.conf import settings
from django.http import QueryDict
register = Library()
+
def strToBool(val):
"""
Helper function to turn a string representation of "true" into
@@ -18,10 +16,11 @@ def strToBool(val):
"""
return val.lower() == "true"
+
def get_page_url(page_num, current_app, url_view_name, url_extra_args, url_extra_kwargs, url_param_name, url_get_params):
"""
Helper function to return a valid URL string given the template tag parameters
- """
+ """
if url_view_name is not None:
# Add page param to the kwargs list. Overrides any previously set parameter of the same name.
url_extra_kwargs[url_param_name] = page_num
@@ -44,9 +43,10 @@ def get_page_url(page_num, current_app, url_view_name, url_extra_args, url_extra
if (len(url_get_params) > 0):
url += '?' + url_get_params.urlencode()
-
+
return url
+
class BootstrapPagerNode(Node):
def __init__(self, page, kwargs):
self.page = page
@@ -54,15 +54,15 @@ def __init__(self, page, kwargs):
def render(self, context):
page = self.page.resolve(context)
- kwargs = self.kwargs
+ kwargs = {}
# Retrieve variable instances from context where necessary
- for argname, argvalue in kwargs.items():
+ for argname, argvalue in self.kwargs.items():
try:
kwargs[argname] = argvalue.resolve(context)
except AttributeError:
kwargs[argname] = argvalue
- except template.VariableDoesNotExist:
+ except VariableDoesNotExist:
kwargs[argname] = None
centered = strToBool(kwargs.get("centered", "false"))
@@ -88,7 +88,6 @@ def render(self, context):
if page.has_next():
next_page_url = get_page_url(page.next_page_number(), context.current_app, url_view_name, url_extra_args, url_extra_kwargs, url_param_name, url_get_params)
-
return get_template("bootstrap-pagination/pager.html").render(
Context({
'page': page,
@@ -101,6 +100,7 @@ def render(self, context):
'next_page_url': next_page_url
}, autoescape=False))
+
class BootstrapPaginationNode(Node):
"""
Render the Bootstrap pagination bar with the given parameters
@@ -111,15 +111,15 @@ def __init__(self, page, kwargs):
def render(self, context):
page = self.page.resolve(context)
- kwargs = self.kwargs
+ kwargs = {}
# Retrieve variable instances from context where necessary
- for argname, argvalue in kwargs.items():
+ for argname, argvalue in self.kwargs.items():
try:
kwargs[argname] = argvalue.resolve(context)
except AttributeError:
kwargs[argname] = argvalue
- except template.VariableDoesNotExist:
+ except VariableDoesNotExist:
kwargs[argname] = None
# Unpack our keyword arguments, substituting defaults where necessary
@@ -195,7 +195,6 @@ def render(self, context):
if page.has_next():
next_page_url = get_page_url(page.next_page_number(), context.current_app, url_view_name, url_extra_args, url_extra_kwargs, url_param_name, url_get_params)
-
return get_template("bootstrap-pagination/pagination.html").render(
Context({
'page': page,
@@ -213,6 +212,7 @@ def render(self, context):
'next_page_url': next_page_url
}, autoescape=False))
+
@register.tag
def bootstrap_paginate(parser, token):
"""
@@ -297,11 +297,9 @@ def bootstrap_paginate(parser, token):
name, value = match.groups()
kwargs[name] = parser.compile_filter(value)
-
return BootstrapPaginationNode(page, kwargs)
-
@register.tag
def bootstrap_pager(parser, token):
"""
@@ -366,5 +364,4 @@ def bootstrap_pager(parser, token):
name, value = match.groups()
kwargs[name] = parser.compile_filter(value)
-
return BootstrapPagerNode(page, kwargs)
Please sign in to comment.
Something went wrong with that request. Please try again.