39 try: |
42 try: |
40 year_val, month_val, day_val = value.year, value.month, value.day |
43 year_val, month_val, day_val = value.year, value.month, value.day |
41 except AttributeError: |
44 except AttributeError: |
42 year_val = month_val = day_val = None |
45 year_val = month_val = day_val = None |
43 if isinstance(value, basestring): |
46 if isinstance(value, basestring): |
44 match = RE_DATE.match(value) |
47 if settings.USE_L10N: |
45 if match: |
48 try: |
46 year_val, month_val, day_val = [int(v) for v in match.groups()] |
49 input_format = get_format('DATE_INPUT_FORMATS')[0] |
|
50 # Python 2.4 compatibility: |
|
51 # v = datetime.datetime.strptime(value, input_format) |
|
52 # would be clearer, but datetime.strptime was added in |
|
53 # Python 2.5 |
|
54 v = datetime.datetime(*(time.strptime(value, input_format)[0:6])) |
|
55 year_val, month_val, day_val = v.year, v.month, v.day |
|
56 except ValueError: |
|
57 pass |
|
58 else: |
|
59 match = RE_DATE.match(value) |
|
60 if match: |
|
61 year_val, month_val, day_val = [int(v) for v in match.groups()] |
|
62 choices = [(i, i) for i in self.years] |
|
63 year_html = self.create_select(name, self.year_field, value, year_val, choices) |
|
64 choices = MONTHS.items() |
|
65 month_html = self.create_select(name, self.month_field, value, month_val, choices) |
|
66 choices = [(i, i) for i in range(1, 32)] |
|
67 day_html = self.create_select(name, self.day_field, value, day_val, choices) |
47 |
68 |
|
69 format = get_format('DATE_FORMAT') |
|
70 escaped = False |
48 output = [] |
71 output = [] |
49 |
72 for char in format: |
50 if 'id' in self.attrs: |
73 if escaped: |
51 id_ = self.attrs['id'] |
74 escaped = False |
52 else: |
75 elif char == '\\': |
53 id_ = 'id_%s' % name |
76 escaped = True |
54 |
77 elif char in 'Yy': |
55 month_choices = MONTHS.items() |
78 output.append(year_html) |
56 if not (self.required and value): |
79 elif char in 'bFMmNn': |
57 month_choices.append(self.none_value) |
80 output.append(month_html) |
58 month_choices.sort() |
81 elif char in 'dj': |
59 local_attrs = self.build_attrs(id=self.month_field % id_) |
82 output.append(day_html) |
60 s = Select(choices=month_choices) |
|
61 select_html = s.render(self.month_field % name, month_val, local_attrs) |
|
62 output.append(select_html) |
|
63 |
|
64 day_choices = [(i, i) for i in range(1, 32)] |
|
65 if not (self.required and value): |
|
66 day_choices.insert(0, self.none_value) |
|
67 local_attrs['id'] = self.day_field % id_ |
|
68 s = Select(choices=day_choices) |
|
69 select_html = s.render(self.day_field % name, day_val, local_attrs) |
|
70 output.append(select_html) |
|
71 |
|
72 year_choices = [(i, i) for i in self.years] |
|
73 if not (self.required and value): |
|
74 year_choices.insert(0, self.none_value) |
|
75 local_attrs['id'] = self.year_field % id_ |
|
76 s = Select(choices=year_choices) |
|
77 select_html = s.render(self.year_field % name, year_val, local_attrs) |
|
78 output.append(select_html) |
|
79 |
|
80 return mark_safe(u'\n'.join(output)) |
83 return mark_safe(u'\n'.join(output)) |
81 |
84 |
82 def id_for_label(self, id_): |
85 def id_for_label(self, id_): |
83 return '%s_month' % id_ |
86 return '%s_month' % id_ |
84 id_for_label = classmethod(id_for_label) |
87 id_for_label = classmethod(id_for_label) |
88 m = data.get(self.month_field % name) |
91 m = data.get(self.month_field % name) |
89 d = data.get(self.day_field % name) |
92 d = data.get(self.day_field % name) |
90 if y == m == d == "0": |
93 if y == m == d == "0": |
91 return None |
94 return None |
92 if y and m and d: |
95 if y and m and d: |
93 return '%s-%s-%s' % (y, m, d) |
96 if settings.USE_L10N: |
|
97 input_format = get_format('DATE_INPUT_FORMATS')[0] |
|
98 try: |
|
99 date_value = datetime.date(int(y), int(m), int(d)) |
|
100 except ValueError: |
|
101 pass |
|
102 else: |
|
103 return date_value.strftime(input_format) |
|
104 else: |
|
105 return '%s-%s-%s' % (y, m, d) |
94 return data.get(name, None) |
106 return data.get(name, None) |
|
107 |
|
108 def create_select(self, name, field, value, val, choices): |
|
109 if 'id' in self.attrs: |
|
110 id_ = self.attrs['id'] |
|
111 else: |
|
112 id_ = 'id_%s' % name |
|
113 if not (self.required and val): |
|
114 choices.insert(0, self.none_value) |
|
115 local_attrs = self.build_attrs(id=field % id_) |
|
116 s = Select(choices=choices) |
|
117 select_html = s.render(field % name, val, local_attrs) |
|
118 return select_html |
|
119 |