Code

Ticket #5917: newforms.extra.widgets.py.5.diff

File newforms.extra.widgets.py.5.diff, 2.0 KB (added by Camille Harang <mammique@…>, 6 years ago)

Fix + ID's 5

Line 
1--- /home/mammique/Desktop/widgets.py   2007-11-16 22:31:38.000000000 +0100
2+++ ../django-trunk/django/newforms/extras/widgets.py   2007-11-17 00:57:24.000000000 +0100
3@@ -30,8 +30,17 @@
4             self.years = range(this_year, this_year+10)
5 
6     def render(self, name, value, attrs=None):
7+
8+        has_id = attrs and 'id' in attrs
9+        final_attrs = self.build_attrs(attrs, name=name)
10+
11+        def select_id(name_field):
12+            if has_id: return dict(final_attrs, id=name_field % attrs['id'])
13+            else: return None
14+
15         try:
16-            value = datetime.date(*map(int, value.split('-')))
17+            if isinstance(value, unicode):
18+                value = datetime.date(*map(int, value.split('-')))
19             year_val, month_val, day_val = value.year, value.month, value.day
20         except (AttributeError, TypeError, ValueError):
21             year_val = month_val = day_val = None
22@@ -40,15 +49,18 @@
23 
24         month_choices = MONTHS.items()
25         month_choices.sort()
26-        select_html = Select(choices=month_choices).render(self.month_field % name, month_val)
27+        select_html = Select(select_id(self.month_field),
28+                             choices=month_choices).render(self.month_field % name, month_val)
29         output.append(select_html)
30 
31         day_choices = [(i, i) for i in range(1, 32)]
32-        select_html = Select(choices=day_choices).render(self.day_field % name, day_val)
33+        select_html = Select(select_id(self.day_field),
34+                             choices=day_choices).render(self.day_field % name, day_val)
35         output.append(select_html)
36 
37         year_choices = [(i, i) for i in self.years]
38-        select_html = Select(choices=year_choices).render(self.year_field % name, year_val)
39+        select_html = Select(select_id(self.year_field),
40+                             choices=year_choices).render(self.year_field % name, year_val)
41         output.append(select_html)
42 
43         return u'\n'.join(output)