class SortCodeWidget Widget first_field s_first second_field s_second

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class SortCodeWidget(Widget):
'''
'''
first_field = '%s_first'
second_field = '%s_second'
third_field = '%s_third'
def render(self, name, value, attrs=None):
try:
first_val, second_val, third_val = value.first, value.second, value.third
except AttributeError:
#if not first_val and not second_val and not third_val:
first_val = second_val = third_val = None
if isinstance(value, basestring):
match = RE_SCODE.match(value)
if match:
first_val, second_val, third_val = [unicode(v) for v in match.groups()]
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
local_attrs = self.build_attrs(id=self.first_field % id_)
input_html = TextInput(attrs={'size':'4','maxlength':'2'}).render(self.first_field % name, first_val, local_attrs)
output.append(input_html)
local_attrs['id'] = self.second_field % id_
input_html = TextInput(attrs={'size':'4','maxlength':'2'}).render(self.second_field % name, second_val, local_attrs)
output.append(input_html)
local_attrs['id'] = self.third_field % id_
input_html = TextInput(attrs={'size':'4','maxlength':'2'}).render(self.third_field % name, third_val, local_attrs)
output.append(input_html)
return mark_safe(u'\n'.join(output))
def id_for_label(self, id_):
return '%s_month' % id_
id_for_label = classmethod(id_for_label)
def value_from_datadict(self, data, files, name):
f, s, t = data.get(self.first_field % name), data.get(self.second_field % name), data.get(self.third_field % name)
if f and s and t:
return '%s%s%s' % (f, s, t)
return data.get(name, None)