Code

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

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

Fix + ID's 3 (same as Fix + ID's 2 but uses isinstance, this is the good one, sorry for flooding)

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-16 22:36:06.000000000 +0100
3@@ -30,25 +30,39 @@
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 
23         output = []
24 
25+        print self.month_field % u'ntsf'
26+
27         month_choices = MONTHS.items()
28         month_choices.sort()
29-        select_html = Select(choices=month_choices).render(self.month_field % name, month_val)
30+        select_html = Select(select_id(self.month_field),
31+                             choices=month_choices).render(self.month_field % name, month_val)
32         output.append(select_html)
33 
34         day_choices = [(i, i) for i in range(1, 32)]
35-        select_html = Select(choices=day_choices).render(self.day_field % name, day_val)
36+        select_html = Select(select_id(self.month_field),
37+                             choices=day_choices).render(self.day_field % name, day_val)
38         output.append(select_html)
39 
40         year_choices = [(i, i) for i in self.years]
41-        select_html = Select(choices=year_choices).render(self.year_field % name, year_val)
42+        select_html = Select(select_id(self.year_field),
43+                             choices=year_choices).render(self.year_field % name, year_val)
44         output.append(select_html)
45 
46         return u'\n'.join(output)