Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up button template tags

ref #167
ref #171
  • Loading branch information...
commit 9e009c1d52355778582fcfbe75c7db274f0153f4 1 parent baa3b59
@flashingpumpkin authored
View
16 socialregistration/contrib/openid/templatetags/openid.py
@@ -1,5 +1,5 @@
from django import template
-
+from socialregistration.templatetags import resolve, get_bits
register = template.Library()
@@ -17,8 +17,7 @@ def openid_form(parser, token):
"""
- bits = token.split_contents()
- bits = [bit.replace("'","").replace('"','') for bit in bits[1:]]
+ bits = get_bits(token)
if len(bits) > 1:
return FormNode(bits[0], bits[1:])
@@ -32,20 +31,13 @@ def __init__(self, provider, params = []):
self.params = params
def render(self, context):
- def resolve(what):
- try:
- return template.Variable(what).resolve(context)
- except template.VariableDoesNotExist:
- return what
-
if self.provider:
- provider = resolve(self.provider)
+ provider = resolve(self.provider, context)
else:
provider = None
if self.params:
- params = [resolve(bit) for bit in self.params]
- button = ''.join(params)
+ button = ''.join([resolve(bit, context) for bit in self.params])
else:
button = None
View
37 socialregistration/templatetags/__init__.py
@@ -3,32 +3,33 @@
register = template.Library()
+def resolve(what, context):
+ try:
+ return template.Variable(what).resolve(context)
+ except template.VariableDoesNotExist:
+ return what
+
+def get_bits(token):
+ bits = token.split_contents()
+ return [bit.replace("'","").replace('"','') for bit in bits[1:]]
+
def button(template_name):
def tag(parser, token):
- bits = token.split_contents()
- if len(bits) > 1:
- return ButtonTag(template_name, *bits[1:])
- else:
- # No custom button
- return ButtonTag(template_name)
+ bits = get_bits(token)
+ return ButtonTag(template_name, get_bits(token))
return tag
class ButtonTag(template.Node):
- def __init__(self, template_name, *input):
- self.template = template_name
- self.input = input
+ def __init__(self, template_name, params = []):
+ self.template_name = template_name
+ self.params = params
def render(self, context):
- output = []
- for bit in self.input:
- if not (bit[0] == bit[-1] and bit[0] in ('"', "'")):
- output.append(template.Variable(bit).resolve(context))
- else:
- output.append(bit[1:-1])
- self.button = ''.join(output)
-
if not 'request' in context:
raise AttributeError(_("Please add 'django.core.context_processors.request' "
"'to your settings.TEMPLATE_CONTEXT_PROCESSORS'"))
- return template.loader.render_to_string(self.template, {'button': self.button, 'next': context.get('next', None)}, context)
+ button = ''.join([resolve(bit, context) for bit in self.params])
+
+ return template.loader.render_to_string(self.template_name, {
+ 'button': button}, context)
View
9 socialregistration/tests.py
@@ -27,9 +27,14 @@ def test_tag_renders_correctly(self):
self.assertTrue('form' in template.Template(tpl).render(template.Context({'request': None})))
- tpl = """{%% load %s %%}{%% %s 'custom/button/url.jpg' %%}""" % (load, button)
+ tpl = """{%% load %s %%}{%% %s STATIC_URL 'custom/button/url.jpg' %%}""" % (load, button)
- self.assertTrue('custom/button/url.jpg' in template.Template(tpl).render(template.Context({'request': None})))
+ rendered = template.Template(tpl).render(template.Context({
+ 'request': None,
+ 'STATIC_URL': '/static/'}))
+
+ self.assertTrue('custom/button/url.jpg' in rendered)
+ self.assertTrue('/static/' in rendered)
def get_mock_func(func):
Please sign in to comment.
Something went wrong with that request. Please try again.