Refactored some code in SelectDateWidget.

This commit is contained in:
Tomasz Wysocki 2014-04-03 16:26:57 +02:00 committed by Tim Graham
parent 395d75ea6b
commit ea5a984704
1 changed files with 9 additions and 22 deletions

View File

@ -23,22 +23,17 @@ RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
def _parse_date_fmt(): def _parse_date_fmt():
fmt = get_format('DATE_FORMAT') fmt = get_format('DATE_FORMAT')
escaped = False escaped = False
output = []
for char in fmt: for char in fmt:
if escaped: if escaped:
escaped = False escaped = False
elif char == '\\': elif char == '\\':
escaped = True escaped = True
elif char in 'Yy': elif char in 'Yy':
output.append('year') yield 'year'
#if not self.first_select: self.first_select = 'year'
elif char in 'bEFMmNn': elif char in 'bEFMmNn':
output.append('month') yield 'month'
#if not self.first_select: self.first_select = 'month'
elif char in 'dj': elif char in 'dj':
output.append('day') yield 'day'
#if not self.first_select: self.first_select = 'day'
return output
class SelectDateWidget(Widget): class SelectDateWidget(Widget):
@ -86,29 +81,21 @@ class SelectDateWidget(Widget):
match = RE_DATE.match(value) match = RE_DATE.match(value)
if match: if match:
year_val, month_val, day_val = [int(v) for v in match.groups()] year_val, month_val, day_val = [int(v) for v in match.groups()]
html = {}
choices = [(i, i) for i in self.years] 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)) 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)] 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 = [] output = []
for field in _parse_date_fmt(): for field in _parse_date_fmt():
if field == 'year': output.append(html[field])
output.append(year_html)
elif field == 'month':
output.append(month_html)
elif field == 'day':
output.append(day_html)
return mark_safe('\n'.join(output)) return mark_safe('\n'.join(output))
def id_for_label(self, id_): def id_for_label(self, id_):
first_select = None for first_select in _parse_date_fmt():
field_list = _parse_date_fmt()
if field_list:
first_select = field_list[0]
if first_select is not None:
return '%s_%s' % (id_, first_select) return '%s_%s' % (id_, first_select)
else: else:
return '%s_month' % id_ return '%s_month' % id_