Code

Ticket #13237: chioces.multigroup.diff

File chioces.multigroup.diff, 1.7 KB (added by IsakovAN, 4 years ago)
Line 
1diff -u -r django.orig/forms/widgets.py django/forms/widgets.py
2--- django.orig/forms/widgets.py        2009-09-11 13:08:07.000000000 +0400
3+++ django/forms/widgets.py     2010-03-28 23:47:45.853144271 +0400
4@@ -414,17 +414,19 @@
5             return u'<option value="%s"%s>%s</option>' % (
6                 escape(option_value), selected_html,
7                 conditional_escape(force_unicode(option_label)))
8+        def render_group(option_value, option_label):
9+            return u'<optgroup label="%s">%s</optgroup>' %(escape(force_unicode(option_value)),''.join(process_list(option_label)))
10+        def process_list(l):
11+            output = []
12+            for option_value, option_label in l:
13+                if isinstance(option_label, (list, tuple)):
14+                    output.append(render_group(option_value, option_label))
15+                else:
16+                    output.append(render_option(option_value, option_label))
17+            return output
18         # Normalize to strings.
19         selected_choices = set([force_unicode(v) for v in selected_choices])
20-        output = []
21-        for option_value, option_label in chain(self.choices, choices):
22-            if isinstance(option_label, (list, tuple)):
23-                output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
24-                for option in option_label:
25-                    output.append(render_option(*option))
26-                output.append(u'</optgroup>')
27-            else:
28-                output.append(render_option(option_value, option_label))
29+        output = process_list(chain(self.choices, choices))
30         return u'\n'.join(output)
31 
32 class NullBooleanSelect(Select):