diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-07-19 01:22:26 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-07-19 01:22:26 +0000 |
| commit | 46786b4193e04d398532bbfc3dcf63c03c1793cb (patch) | |
| tree | 8dfb9330ca0e377d89c29e3f67076231bbbbbc05 /django/forms/extras | |
| parent | 39af2738fd64ba7f4c4af0783590e2b2b7b88460 (diff) | |
Fixed #7741: django.newforms is now django.forms. This is obviously a backwards-incompatible change. There's a warning upon import of django.newforms itself, but deeper imports will raise errors.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7971 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms/extras')
| -rw-r--r-- | django/forms/extras/__init__.py | 1 | ||||
| -rw-r--r-- | django/forms/extras/widgets.py | 79 |
2 files changed, 80 insertions, 0 deletions
diff --git a/django/forms/extras/__init__.py b/django/forms/extras/__init__.py new file mode 100644 index 0000000000..a7f6a9b3f6 --- /dev/null +++ b/django/forms/extras/__init__.py @@ -0,0 +1 @@ +from widgets import * diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py new file mode 100644 index 0000000000..ffa7ba2de2 --- /dev/null +++ b/django/forms/extras/widgets.py @@ -0,0 +1,79 @@ +""" +Extra HTML Widget classes +""" + +import datetime +import re + +from django.forms.widgets import Widget, Select +from django.utils.dates import MONTHS +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. + + This also serves as an example of a Widget that has more than one HTML + element and hence implements value_from_datadict. + """ + month_field = '%s_month' + day_field = '%s_day' + year_field = '%s_year' + + def __init__(self, attrs=None, years=None): + # years is an optional list/tuple of years to use in the "year" select box. + self.attrs = attrs or {} + if years: + self.years = years + else: + this_year = datetime.date.today().year + self.years = range(this_year, this_year+10) + + def render(self, name, value, attrs=None): + try: + year_val, month_val, day_val = value.year, value.month, value.day + 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() + 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)] + 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] + 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: + return '%s-%s-%s' % (y, m, d) + return data.get(name, None) |
