19 | | def _parse_date_fmt(): |
20 | | fmt = get_format('DATE_FORMAT') |
21 | | escaped = False |
22 | | output = [] |
23 | | for char in fmt: |
24 | | if escaped: |
25 | | escaped = False |
26 | | elif char == '\\': |
27 | | escaped = True |
28 | | elif char in 'Yy': |
29 | | output.append('year') |
30 | | #if not self.first_select: self.first_select = 'year' |
31 | | elif char in 'bEFMmNn': |
32 | | output.append('month') |
33 | | #if not self.first_select: self.first_select = 'month' |
34 | | elif char in 'dj': |
35 | | output.append('day') |
36 | | #if not self.first_select: self.first_select = 'day' |
37 | | return output |
| 19 | |
| 20 | def _parse_format(fmt=None): |
| 21 | """ |
| 22 | Sort the order of date selectors according to the date_format attribute. |
| 23 | If date_format is not given, default to get_format('DATE_FORMAT')[0] |
| 24 | """ |
| 25 | if not fmt: |
| 26 | return _parse_format(re.subn(r'\\.', '', |
| 27 | get_format('DATE_INPUT_FORMATS')[0])[0]) |
| 28 | def pos(s): |
| 29 | match = re.search('[' + s + ']', fmt) |
| 30 | return match.start() + 1 if match else 0 |
| 31 | dic = {'day': pos('dj'), 'month': pos('bEFMmNn'), 'year': pos('yY')} |
| 32 | return sorted(filter(None, dic.keys()), key=dic.get) |
| 33 | |
51 | | def __init__(self, attrs=None, years=None, required=True): |
52 | | # years is an optional list/tuple of years to use in the "year" select box. |
| 48 | def __init__(self, attrs=None, years=None, required=True, |
| 49 | date_format=None, none_values=None): |
| 50 | # years is an optional list/tuple of years |
| 51 | # to use in the "year" select box. |
77 | | year_val, month_val, day_val = [int(v) for v in match.groups()] |
78 | | choices = [(i, i) for i in self.years] |
79 | | year_html = self.create_select(name, self.year_field, value, year_val, choices) |
80 | | choices = MONTHS.items() |
81 | | month_html = self.create_select(name, self.month_field, value, month_val, choices) |
82 | | choices = [(i, i) for i in range(1, 32)] |
83 | | day_html = self.create_select(name, self.day_field, value, day_val, choices) |
84 | | |
85 | | output = [] |
86 | | for field in _parse_date_fmt(): |
87 | | if field == 'year': |
88 | | output.append(year_html) |
89 | | elif field == 'month': |
90 | | output.append(month_html) |
91 | | elif field == 'day': |
92 | | output.append(day_html) |
| 80 | values_list = (int(v) for v in match.groups()) |
| 81 | values = get_names_dict(values_list) |
| 82 | |
| 83 | choices = get_names_dict([ |
| 84 | [(i, i) for i in range(1, 32)], |
| 85 | MONTHS.items(), |
| 86 | [(i, i) for i in self.years], |
| 87 | ]) |
| 88 | create_select_short = lambda n: self.create_select( |
| 89 | name, self.fields[n], value, values.get(n), choices[n], n |
| 90 | ) |
| 91 | |
| 92 | htmls = get_names_dict([create_select_short(n) for n in self.names]) |
| 93 | date_format = self.date_format or self._parse_format() |
| 94 | output = [htmls[field] for field in date_format] |
112 | | if y and m and d: |
113 | | if settings.USE_L10N: |
114 | | input_format = get_format('DATE_INPUT_FORMATS')[0] |
115 | | try: |
116 | | date_value = datetime.date(int(y), int(m), int(d)) |
117 | | except ValueError: |
118 | | return '%s-%s-%s' % (y, m, d) |
119 | | else: |
120 | | date_value = datetime_safe.new_date(date_value) |
121 | | return date_value.strftime(input_format) |
122 | | else: |
123 | | return '%s-%s-%s' % (y, m, d) |
124 | | return data.get(name, None) |
| 110 | if not (y and m and d): |
| 111 | return data.get(name) |
| 112 | if not settings.USE_L10N: |
| 113 | return '%s-%s-%s' % (y, m, d) |
| 114 | input_format = get_format('DATE_INPUT_FORMATS')[0] |
| 115 | try: |
| 116 | date_value = datetime.date(int(y), int(m), int(d)) |
| 117 | date_value = datetime_safe.new_date(date_value) |
| 118 | return date_value.strftime(input_format) |
| 119 | except ValueError: |
| 120 | return '%s-%s-%s' % (y, m, d) |
139 | | input_format = get_format('DATE_INPUT_FORMATS')[0] |
140 | | data = datetime_safe.datetime.strptime(data, input_format).date() |
141 | | return super(SelectDateWidget, self)._has_changed(initial, data) |
142 | | No newline at end of file |
| 135 | try: |
| 136 | input_format = get_format('DATE_INPUT_FORMATS')[0] |
| 137 | data = datetime_safe.datetime.strptime(data, input_format).date() |
| 138 | except (TypeError, ValueError): |
| 139 | pass |
| 140 | return super(SelectDateWidget, self)._has_changed(initial, data) |