Skip to content
This repository
Browse code

Cleaned up button template tags

ref #167
ref #171
  • Loading branch information...
commit 9e009c1d52355778582fcfbe75c7db274f0153f4 1 parent baa3b59
Alen Mujezinovic authored February 01, 2013
16  socialregistration/contrib/openid/templatetags/openid.py
... ...
@@ -1,5 +1,5 @@
1 1
 from django import template
2  
-
  2
+from socialregistration.templatetags import resolve, get_bits
3 3
 
4 4
 register = template.Library()
5 5
 
@@ -17,8 +17,7 @@ def openid_form(parser, token):
17 17
 
18 18
     """
19 19
 
20  
-    bits = token.split_contents()
21  
-    bits = [bit.replace("'","").replace('"','') for bit in bits[1:]]
  20
+    bits = get_bits(token)
22 21
 
23 22
     if len(bits) > 1:
24 23
         return FormNode(bits[0], bits[1:])
@@ -32,20 +31,13 @@ def __init__(self, provider, params = []):
32 31
         self.params = params
33 32
 
34 33
     def render(self, context):
35  
-        def resolve(what):
36  
-            try:
37  
-                return template.Variable(what).resolve(context)
38  
-            except template.VariableDoesNotExist:
39  
-                return what
40  
-
41 34
         if self.provider:
42  
-            provider = resolve(self.provider)
  35
+            provider = resolve(self.provider, context)
43 36
         else:
44 37
             provider = None
45 38
 
46 39
         if self.params:
47  
-            params = [resolve(bit) for bit in self.params]
48  
-            button = ''.join(params)
  40
+            button = ''.join([resolve(bit, context) for bit in self.params])
49 41
         else:
50 42
             button = None
51 43
 
37  socialregistration/templatetags/__init__.py
@@ -3,32 +3,33 @@
3 3
 
4 4
 register = template.Library()
5 5
 
  6
+def resolve(what, context):
  7
+    try:
  8
+        return template.Variable(what).resolve(context)
  9
+    except template.VariableDoesNotExist:
  10
+        return what
  11
+
  12
+def get_bits(token):
  13
+    bits = token.split_contents()
  14
+    return [bit.replace("'","").replace('"','') for bit in bits[1:]]
  15
+
6 16
 def button(template_name):
7 17
     def tag(parser, token):
8  
-        bits = token.split_contents()
9  
-        if len(bits) > 1:
10  
-            return ButtonTag(template_name, *bits[1:])
11  
-        else:
12  
-            # No custom button
13  
-            return ButtonTag(template_name)
  18
+        bits = get_bits(token)
  19
+        return ButtonTag(template_name, get_bits(token))
14 20
     return tag
15 21
 
16 22
 class ButtonTag(template.Node):
17  
-    def __init__(self, template_name, *input):
18  
-        self.template = template_name
19  
-        self.input = input
  23
+    def __init__(self, template_name, params = []):
  24
+        self.template_name = template_name
  25
+        self.params = params
20 26
 
21 27
     def render(self, context):
22  
-        output = []
23  
-        for bit in self.input:
24  
-            if not (bit[0] == bit[-1] and bit[0] in ('"', "'")):
25  
-                output.append(template.Variable(bit).resolve(context))
26  
-            else:
27  
-                output.append(bit[1:-1])
28  
-        self.button = ''.join(output)
29  
-
30 28
         if not 'request' in context:
31 29
             raise AttributeError(_("Please add 'django.core.context_processors.request' "
32 30
                 "'to your settings.TEMPLATE_CONTEXT_PROCESSORS'"))
33 31
 
34  
-        return template.loader.render_to_string(self.template, {'button': self.button, 'next': context.get('next', None)}, context)
  32
+        button = ''.join([resolve(bit, context) for bit in self.params])
  33
+        
  34
+        return template.loader.render_to_string(self.template_name, {
  35
+                'button': button}, context)
9  socialregistration/tests.py
@@ -27,9 +27,14 @@ def test_tag_renders_correctly(self):
27 27
         
28 28
         self.assertTrue('form' in template.Template(tpl).render(template.Context({'request': None})))
29 29
         
30  
-        tpl = """{%% load %s %%}{%% %s 'custom/button/url.jpg' %%}""" % (load, button)
  30
+        tpl = """{%% load %s %%}{%% %s STATIC_URL 'custom/button/url.jpg' %%}""" % (load, button)
31 31
         
32  
-        self.assertTrue('custom/button/url.jpg' in template.Template(tpl).render(template.Context({'request': None})))
  32
+        rendered = template.Template(tpl).render(template.Context({
  33
+                    'request': None,
  34
+                    'STATIC_URL': '/static/'}))
  35
+
  36
+        self.assertTrue('custom/button/url.jpg' in rendered)
  37
+        self.assertTrue('/static/' in rendered)
33 38
 
34 39
 
35 40
 def get_mock_func(func):

0 notes on commit 9e009c1

Please sign in to comment.
Something went wrong with that request. Please try again.