summaryrefslogtreecommitdiff
path: root/django/newforms/extras
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2008-03-22 19:20:19 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2008-03-22 19:20:19 +0000
commit1d46d20038a8473fc2e75f832c770f17d558d0fc (patch)
tree0d233d922f1757bbaeb04a2969c4392cb4191d9c /django/newforms/extras
parent7aa851c05d80ef75581975939ff5c48b87bcae08 (diff)
newforms-admin: Merged from trunk up to [7350].
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7351 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms/extras')
-rw-r--r--django/newforms/extras/widgets.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/django/newforms/extras/widgets.py b/django/newforms/extras/widgets.py
index 0097ba3f54..e3ef1d7f69 100644
--- a/django/newforms/extras/widgets.py
+++ b/django/newforms/extras/widgets.py
@@ -3,6 +3,7 @@ Extra HTML Widget classes
"""
import datetime
+import re
from django.newforms.widgets import Widget, Select
from django.utils.dates import MONTHS
@@ -10,6 +11,8 @@ from django.utils.safestring import mark_safe
__all__ = ('SelectDateWidget',)
+RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
+
class SelectDateWidget(Widget):
"""
A Widget that splits date input into three <select> boxes.
@@ -32,28 +35,43 @@ class SelectDateWidget(Widget):
def render(self, name, value, attrs=None):
try:
- value = datetime.date(*map(int, value.split('-')))
year_val, month_val, day_val = value.year, value.month, value.day
- except (AttributeError, TypeError, ValueError):
+ except AttributeError:
year_val = month_val = day_val = None
+ if isinstance(value, basestring):
+ match = RE_DATE.match(value)
+ if match:
+ year_val, month_val, day_val = [int(v) for v in match.groups()]
output = []
+ if 'id' in self.attrs:
+ id_ = self.attrs['id']
+ else:
+ id_ = 'id_%s' % name
+
month_choices = MONTHS.items()
month_choices.sort()
- select_html = Select(choices=month_choices).render(self.month_field % name, month_val)
+ local_attrs = self.build_attrs(id=self.month_field % id_)
+ select_html = Select(choices=month_choices).render(self.month_field % name, month_val, local_attrs)
output.append(select_html)
day_choices = [(i, i) for i in range(1, 32)]
- select_html = Select(choices=day_choices).render(self.day_field % name, day_val)
+ local_attrs['id'] = self.day_field % id_
+ select_html = Select(choices=day_choices).render(self.day_field % name, day_val, local_attrs)
output.append(select_html)
year_choices = [(i, i) for i in self.years]
- select_html = Select(choices=year_choices).render(self.year_field % name, year_val)
+ local_attrs['id'] = self.year_field % id_
+ select_html = Select(choices=year_choices).render(self.year_field % name, year_val, local_attrs)
output.append(select_html)
return mark_safe(u'\n'.join(output))
+ def id_for_label(self, id_):
+ return '%s_month' % id_
+ id_for_label = classmethod(id_for_label)
+
def value_from_datadict(self, data, files, name):
y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
if y and m and d: