Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Cleaned up some un-DRYness by adding Widget.build_attrs(). …

…Also slightly changed flatatt to include a leading space, so the spaces don't have to be hard-coded each time you embed flatatt() results in HTML. Thanks, SmileyChris. Refs #3023

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4071 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5a597d3bbb5c868ed6b48555f3b4f708dc7b032f 1 parent cdec3d8
Adrian Holovaty authored November 15, 2006

Showing 1 changed file with 19 additions and 23 deletions. Show diff stats Hide diff stats

  1. 42  django/newforms/widgets.py
42  django/newforms/widgets.py
@@ -16,9 +16,9 @@
16 16
 except NameError:
17 17
     from sets import Set as set # Python 2.3 fallback
18 18
 
19  
-# Converts a dictionary to a single string with key="value", XML-style.
20  
-# Assumes keys do not need to be XML-escaped.
21  
-flatatt = lambda attrs: ' '.join(['%s="%s"' % (k, escape(v)) for k, v in attrs.items()])
  19
+# Converts a dictionary to a single string with key="value", XML-style with
  20
+# a leading space. Assumes keys do not need to be XML-escaped.
  21
+flatatt = lambda attrs: ''.join([' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
22 22
 
23 23
 class Widget(object):
24 24
     requires_data_list = False # Determines whether render()'s 'value' argument should be a list.
@@ -28,16 +28,20 @@ def __init__(self, attrs=None):
28 28
     def render(self, name, value):
29 29
         raise NotImplementedError
30 30
 
  31
+    def build_attrs(self, extra_attrs=None, **kwargs):
  32
+        attrs = dict(self.attrs, **kwargs)
  33
+        if extra_attrs:
  34
+            attrs.update(extra_attrs)
  35
+        return attrs
  36
+
31 37
 class Input(Widget):
32 38
     "Base class for all <input> widgets (except type='checkbox', which is special)"
33 39
     input_type = None # Subclasses must define this.
34 40
     def render(self, name, value, attrs=None):
35 41
         if value is None: value = ''
36  
-        final_attrs = dict(self.attrs, type=self.input_type, name=name)
37  
-        if attrs:
38  
-            final_attrs.update(attrs)
  42
+        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
39 43
         if value != '': final_attrs['value'] = value # Only add the 'value' attribute if a value is non-empty.
40  
-        return u'<input %s />' % flatatt(final_attrs)
  44
+        return u'<input%s />' % flatatt(final_attrs)
41 45
 
42 46
 class TextInput(Input):
43 47
     input_type = 'text'
@@ -54,18 +58,14 @@ class FileInput(Input):
54 58
 class Textarea(Widget):
55 59
     def render(self, name, value, attrs=None):
56 60
         if value is None: value = ''
57  
-        final_attrs = dict(self.attrs, name=name)
58  
-        if attrs:
59  
-            final_attrs.update(attrs)
60  
-        return u'<textarea %s>%s</textarea>' % (flatatt(final_attrs), escape(value))
  61
+        final_attrs = self.build_attrs(attrs, name=name)
  62
+        return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
61 63
 
62 64
 class CheckboxInput(Widget):
63 65
     def render(self, name, value, attrs=None):
64  
-        final_attrs = dict(self.attrs, type='checkbox', name=name)
65  
-        if attrs:
66  
-            final_attrs.update(attrs)
  66
+        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
67 67
         if value: final_attrs['checked'] = 'checked'
68  
-        return u'<input %s />' % flatatt(final_attrs)
  68
+        return u'<input%s />' % flatatt(final_attrs)
69 69
 
70 70
 class Select(Widget):
71 71
     def __init__(self, attrs=None, choices=()):
@@ -75,10 +75,8 @@ def __init__(self, attrs=None, choices=()):
75 75
 
76 76
     def render(self, name, value, attrs=None, choices=()):
77 77
         if value is None: value = ''
78  
-        final_attrs = dict(self.attrs, name=name)
79  
-        if attrs:
80  
-            final_attrs.update(attrs)
81  
-        output = [u'<select %s>' % flatatt(final_attrs)]
  78
+        final_attrs = self.build_attrs(attrs, name=name)
  79
+        output = [u'<select%s>' % flatatt(final_attrs)]
82 80
         str_value = str(value) # Normalize to string.
83 81
         for option_value, option_label in chain(self.choices, choices):
84 82
             selected_html = (str(option_value) == str_value) and ' selected="selected"' or ''
@@ -95,10 +93,8 @@ def __init__(self, attrs=None, choices=()):
95 93
 
96 94
     def render(self, name, value, attrs=None, choices=()):
97 95
         if value is None: value = []
98  
-        final_attrs = dict(self.attrs, name=name)
99  
-        if attrs:
100  
-            final_attrs.update(attrs)
101  
-        output = [u'<select multiple="multiple" %s>' % flatatt(final_attrs)]
  96
+        final_attrs = self.build_attrs(attrs, name=name)
  97
+        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
102 98
         str_values = set([str(v) for v in value]) # Normalize to strings.
103 99
         for option_value, option_label in chain(self.choices, choices):
104 100
             selected_html = (str(option_value) in str_values) and ' selected="selected"' or ''

0 notes on commit 5a597d3

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