Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored some code in SelectDateWidget.

  • Loading branch information...
commit ea5a98470467d5a53f10caf77510c77504f733dd 1 parent 395d75e
@tomwys tomwys authored timgraham committed
Showing with 9 additions and 22 deletions.
  1. +9 −22 django/forms/extras/widgets.py
View
31 django/forms/extras/widgets.py
@@ -23,22 +23,17 @@
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 @@ def render(self, name, value, attrs=None):
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_
Please sign in to comment.
Something went wrong with that request. Please try again.