Code

Ticket #5116: newforms_widgets_selectm_disable.diff

File newforms_widgets_selectm_disable.diff, 1.6 KB (added by Thomas Güttler <hv@…>, 7 years ago)
Line 
1Index: newforms/widgets.py
2===================================================================
3--- newforms/widgets.py (Revision 5830)
4+++ newforms/widgets.py (Arbeitskopie)
5@@ -200,20 +201,23 @@
6         return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
7 
8 class SelectMultiple(Widget):
9-    def __init__(self, attrs=None, choices=()):
10+    def __init__(self, attrs=None, choices=(), disabled=None):
11         # choices can be any iterable
12         self.attrs = attrs or {}
13         self.choices = choices
14-
15+        self.disabled = disabled or set()
16+       
17     def render(self, name, value, attrs=None, choices=()):
18         if value is None: value = []
19         final_attrs = self.build_attrs(attrs, name=name)
20         output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
21         str_values = set([force_unicode(v) for v in value]) # Normalize to strings.
22         for option_value, option_label in chain(self.choices, choices):
23+            disabled_html = (option_value in self.disabled) and ' disabled="disabled"' or ''
24             option_value = force_unicode(option_value)
25             selected_html = (option_value in str_values) and ' selected="selected"' or ''
26-            output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(force_unicode(option_label))))
27+            output.append(u'<option value="%s"%s%s>%s</option>' % (
28+                escape(option_value), selected_html, disabled_html, escape(force_unicode(option_label))))
29         output.append(u'</select>')
30         return u'\n'.join(output)
31