Widget для checkbox'а с выпадающим описанием выбранного пункта

 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from django.template.loader import render_to_string
class ChoiceFieldRenderer(StrAndUnicode):
def __init__(self, name, value, final_attrs, choices, attrs=None):
self.name, self.value, self.final_attrs, self.attrs = name, value, final_attrs, attrs
self.choices = choices
self.choice_widget = forms.CheckboxInput
self.has_id = self.attrs and 'id' in self.attrs
def __iter__(self):
for i, (option_value, option_label_obj) in enumerate(self.choices):
option_label = option_label_obj.name
if self.has_id:
self.final_attrs = dict(self.final_attrs, id='%s_%s' % (self.attrs['id'], i))
label_for = u' for="%s"' % self.final_attrs['id']
else:
label_for = ''
cb = self.choice_widget(self.final_attrs, check_test=lambda value: value in self.value)
option_value = force_unicode(option_value)
option_label = conditional_escape(force_unicode(option_label))
rendered_cb = cb.render(self.name, option_value)
rendered_popup = render_to_string('sms/popup_contacts_descr.html', { 'contact': option_label_obj, 'counter': option_value })
if not option_label_obj.mobile_phone_number: option_label_obj.mobile_phone_number = '>>>'
yield mark_safe(u'<div class="contactHeader"><a href="#" class="phoneNumber" onclick="toggleContact(%s);return false;">%s</a><label%s class="na
me">%s<a href="#">%s</a></label></div>' % \
(option_value, option_label_obj.mobile_phone_number, label_for, rendered_cb, option_label)) + rendered_popup
def __getitem__(self, idx):
option_value, option_label_obj = self.choices[idx] # Let the IndexError propogate
option_label = option_label_obj.name
if self.has_id:
self.final_attrs = dict(self.final_attrs, id='%s_%s' % (self.attrs['id'], idx))
label_for = u' for="%s"' % self.final_attrs['id']
else:
label_for = ''
cb = self.choice_widget(self.final_attrs, check_test=lambda value: value in self.value)
option_value = force_unicode(option_value)
option_label = conditional_escape(force_unicode(option_label))
rendered_cb = cb.render(self.name, option_value)
rendered_popup = render_to_string('sms/popup_contacts_descr.html', { 'contact': option_label_obj, 'counter': option_value })
if not option_label_obj.mobile_phone_number: option_label_obj.mobile_phone_number = '>>>'
return mark_safe(u'<div class="contactHeader"><a href="#" class="phoneNumber" onclick="toggleContact(%s);return false;">%s</a><label%s class="name">%s<a href="#">%s</a></label></div>' % \
(option_value, option_label_obj.mobile_phone_number, label_for, rendered_cb, option_label)) + rendered_popup
def __unicode__(self):
return self.render()
def render(self):
"Outputs a <ul> for this set of choice fields."
return mark_safe(u'%s' % u'\n'.join([u'%s' % force_unicode(w) for w in self]))
class ContactsCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
renderer = ChoiceFieldRenderer
def get_renderer(self, name, value, attrs=None, choices=()):
"""Returns an instance of the renderer."""
if value is None: value = []
final_attrs = self.build_attrs(attrs, name=name)
str_values = set([force_unicode(v) for v in value])
choices = list(chain(self.choices, choices))
return self.renderer(name, str_values, final_attrs, choices, attrs)
def render(self, name, value, attrs=None, choices=()):
return self.get_renderer(name, value, attrs, choices).render()
def iter(self, name, value, attrs=None, choices=()):
for i in self.get_renderer(name, value, attrs, choices):
yield i