diff options
| author | Tomasz Wysocki <tomasz@pozytywnie.pl> | 2014-04-03 16:26:57 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-04-10 13:03:23 -0400 |
| commit | ea5a98470467d5a53f10caf77510c77504f733dd (patch) | |
| tree | dc587d714670226ab1eb3b18589e63656de15ed2 | |
| parent | 395d75ea6bd1ee5259fd5a3b3ac1b028739a22d2 (diff) | |
Refactored some code in SelectDateWidget.
| -rw-r--r-- | django/forms/extras/widgets.py | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py index 966ddd966c..ea26cbe4e4 100644 --- a/django/forms/extras/widgets.py +++ b/django/forms/extras/widgets.py @@ -23,22 +23,17 @@ RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$') def _parse_date_fmt(): fmt = get_format('DATE_FORMAT') escaped = False - output = [] for char in fmt: if escaped: escaped = False elif char == '\\': escaped = True elif char in 'Yy': - output.append('year') - #if not self.first_select: self.first_select = 'year' + yield 'year' elif char in 'bEFMmNn': - output.append('month') - #if not self.first_select: self.first_select = 'month' + yield 'month' elif char in 'dj': - output.append('day') - #if not self.first_select: self.first_select = 'day' - return output + yield 'day' class SelectDateWidget(Widget): @@ -86,29 +81,21 @@ class SelectDateWidget(Widget): match = RE_DATE.match(value) if match: year_val, month_val, day_val = [int(v) for v in match.groups()] + html = {} choices = [(i, i) for i in self.years] - year_html = self.create_select(name, self.year_field, value, year_val, choices) + html['year'] = self.create_select(name, self.year_field, value, year_val, choices) choices = list(six.iteritems(self.months)) - month_html = self.create_select(name, self.month_field, value, month_val, choices) + html['month'] = self.create_select(name, self.month_field, value, month_val, choices) choices = [(i, i) for i in range(1, 32)] - day_html = self.create_select(name, self.day_field, value, day_val, choices) + html['day'] = self.create_select(name, self.day_field, value, day_val, choices) output = [] for field in _parse_date_fmt(): - if field == 'year': - output.append(year_html) - elif field == 'month': - output.append(month_html) - elif field == 'day': - output.append(day_html) + output.append(html[field]) return mark_safe('\n'.join(output)) def id_for_label(self, id_): - first_select = None - field_list = _parse_date_fmt() - if field_list: - first_select = field_list[0] - if first_select is not None: + for first_select in _parse_date_fmt(): return '%s_%s' % (id_, first_select) else: return '%s_month' % id_ |
