Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow methods to not override existing ones.

  • Loading branch information...
commit 83632c098797c1eaa1591b56b9a7c0f5a121eb1c 1 parent ec1754e
@davedash davedash authored
View
14 jingo/__init__.py
@@ -131,14 +131,24 @@ class Register(object):
def __init__(self, env):
self.env = env
+ # TODO: this should be overridable too.
def filter(self, f):
"""Adds the decorated function to Jinja's filter library."""
self.env.filters[f.__name__] = f
return f
- def function(self, f):
+ def function(self, f=None, override=True):
"""Adds the decorated function to Jinja's global namespace."""
- self.env.globals[f.__name__] = f
+ def decorator(f):
+ @functools.wraps(f)
+ def wrapper(*args, **kw):
+ return f(*args, **kw)
+ return self.function(wrapper, override)
+
+ if not f:
+ return decorator
+ if override or f.__name__ not in self.env.globals:
+ self.env.globals[f.__name__] = f
return f
def inclusion_tag(self, template):
View
2  jingo/helpers.py
@@ -76,7 +76,7 @@ def field_attrs(field_inst, **kwargs):
return field_inst
-@register.function
+@register.function(override=False)
def url(viewname, *args, **kwargs):
"""Return URL using django's ``reverse()`` function."""
return reverse(viewname, args=args, kwargs=kwargs)
View
23 jingo/tests/test_helpers.py
@@ -4,11 +4,12 @@
from collections import namedtuple
from jinja2 import Markup
+from mock import patch
from nose.tools import eq_
import jingo
from jingo import helpers
-
+from jingo import register
def render(s, context={}):
t = jingo.env.from_string(s)
@@ -127,3 +128,23 @@ def test_url():
eq_(s, "/url/1/foo/")
s = render('{{ url("url-kwargs", word="bar", num=1) }}')
eq_(s, "/url/1/bar/")
+
+
+def url(x, *y, **z):
+ return '/' + x + '!'
+
+@patch('django.conf.settings')
+def test_custom_url(s):
+ # register our url method
+ register.function(url)
+ # re-register Jinja's
+ register.function(helpers.url, override=False)
+
+ # urls defined in jingo/tests/urls.py
+ s = render('{{ url("url-args", 1, "foo") }}')
+ eq_(s, "/url-args!")
+ s = render('{{ url("url-kwargs", word="bar", num=1) }}')
+ eq_(s, "/url-kwargs!")
+
+ # teardown
+ register.function(helpers.url, override=True)
Please sign in to comment.
Something went wrong with that request. Please try again.