Ticket #12986: 12986-2.diff
File 12986-2.diff, 5.0 KB (added by , 15 years ago) |
---|
-
django/forms/extras/widgets.py
13 13 14 14 __all__ = ('SelectDateWidget',) 15 15 16 RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')17 18 16 class SelectDateWidget(Widget): 19 17 """ 20 18 A Widget that splits date input into three <select> boxes. 21 19 22 20 This also serves as an example of a Widget that has more than one HTML 23 21 element and hence implements value_from_datadict. 22 23 If format localization is used (USE_L10N = True), combination of %Y, %m and %d directives in default 24 input format is needed for invalid dates to be rendered. 24 25 """ 25 26 none_value = (0, '---') 26 27 month_field = '%s_month' … … 36 37 else: 37 38 this_year = datetime.date.today().year 38 39 self.years = range(this_year, this_year+10) 39 40 40 41 def render(self, name, value, attrs=None): 41 42 try: 42 43 year_val, month_val, day_val = value.year, value.month, value.day 43 44 except AttributeError: 44 45 year_val = month_val = day_val = None 45 46 if isinstance(value, basestring): 46 match = RE_DATE.match(value) 47 if match: 48 year_val, month_val, day_val = [int(v) for v in match.groups()] 49 47 input_format = get_format('DATE_INPUT_FORMATS')[0] 48 try: 49 val = datetime.datetime.strptime(value, input_format) 50 year_val, month_val, day_val = val.year, val.month, val.day 51 except ValueError: 52 # When selected day is invalid in month convert input format to regex and receive year, day, month values 53 # so the failed date still can be rendered. 54 format_to_re_dic = { 55 '%Y': r'(?P<y>\d{4})', 56 '%m': r'(?P<m>\d\d?)', 57 '%d': r'(?P<d>\d\d?)' 58 } 59 re_str = input_format 60 for k, v in f_to_re_dic.iteritems(): 61 re_str = re_str.replace(k, v) 62 match = re.match('^' + re_str + '$', value) 63 if match: 64 year_val, month_val, day_val = int(match.group('y')), int(match.group('m')), int(match.group('d')) 50 65 choices = [(i, i) for i in self.years] 51 66 year_html = self.create_select(name, self.year_field, value, year_val, choices) 52 67 choices = MONTHS.items() … … 81 96 if y == m == d == "0": 82 97 return None 83 98 if y and m and d: 84 if settings.USE_L10N: 85 input_format = get_format('DATE_INPUT_FORMATS')[0] 86 try: 87 date_value = datetime.date(int(y), int(m), int(d)) 88 except ValueError: 89 pass 90 else: 91 return date_value.strftime(input_format) 92 else: 93 return '%s-%s-%s' % (y, m, d) 99 input_format = get_format('DATE_INPUT_FORMATS')[0] 100 return input_format.replace('%Y', y).replace('%m', m).replace('%d', d) 94 101 return data.get(name, None) 95 102 96 103 def create_select(self, name, field, value, val, choices): -
tests/regressiontests/forms/widgets.py
1267 1267 >>> deactivate() 1268 1268 >>> settings.USE_L10N = False 1269 1269 1270 1271 # SelectDateWidget 1272 >>> from django.forms.extras import SelectDateWidget 1273 >>> w = SelectDateWidget() 1274 >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') 1275 '2010-8-13' 1276 >>> res = w.render('date', '2010-08-13') 1277 >>> u'<option value="13" selected="selected">13</option>' in res 1278 True 1279 >>> u'<option value="2010" selected="selected">2010</option>' in res 1280 True 1281 >>> u'<option value="8" selected="selected">August</option>' in res 1282 True 1283 >>> from django.utils import translation 1284 >>> translation.activate('nl') 1285 >>> from django.conf import settings 1286 >>> settings.USE_L10N=True 1287 >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') 1288 '13-8-2010' 1289 >>> res = w.render('date', '13-08-2010') 1290 >>> u'<option value="13" selected="selected">13</option>' in res 1291 True 1292 >>> u'<option value="2010" selected="selected">2010</option>' in res 1293 True 1294 >>> u'<option value="8" selected="selected">augustus</option>' in res 1295 True 1296 >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') 1297 '13-8-2010' 1298 >>> res = w.render('date', '31-2-2010') 1299 >>> u'<option value="31" selected="selected">31</option>' in res 1300 True 1301 >>> u'<option value="2010" selected="selected">2010</option>' in res 1302 True 1303 >>> u'<option value="2" selected="selected">februari</option>' in res 1304 True 1270 1305 """ 1271 1306 1272 1307