Fixed #12986 -- Ensured that SelectDateField repopulates correctly when USE_L10N=True and locale has a default date input format other than %Y-%m-%d. Thanks to wim@go2people.nl for the report, and walteralini for his draft patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13041 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-04-28 15:39:26 +00:00
parent 9d8492c169
commit 5926a26e92
2 changed files with 88 additions and 5 deletions

View File

@ -43,10 +43,17 @@ class SelectDateWidget(Widget):
except AttributeError: except AttributeError:
year_val = month_val = day_val = None year_val = month_val = day_val = None
if isinstance(value, basestring): if isinstance(value, basestring):
if settings.USE_L10N:
try:
input_format = get_format('DATE_INPUT_FORMATS')[0]
v = datetime.datetime.strptime(value, input_format)
year_val, month_val, day_val = v.year, v.month, v.day
except ValueError:
pass
else:
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()]
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) year_html = self.create_select(name, self.year_field, value, year_val, choices)
choices = MONTHS.items() choices = MONTHS.items()
@ -98,7 +105,7 @@ class SelectDateWidget(Widget):
id_ = self.attrs['id'] id_ = self.attrs['id']
else: else:
id_ = 'id_%s' % name id_ = 'id_%s' % name
if not (self.required and value): if not (self.required and val):
choices.insert(0, self.none_value) choices.insert(0, self.none_value)
local_attrs = self.build_attrs(id=field % id_) local_attrs = self.build_attrs(id=field % id_)
s = Select(choices=choices) s = Select(choices=choices)

View File

@ -364,6 +364,82 @@ True
2008-04-01 2008-04-01
USE_L10N tests
>>> from django.utils import translation
>>> translation.activate('nl')
>>> from django.conf import settings
>>> settings.USE_L10N=True
>>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date')
'13-08-2010'
>>> print w.render('date', '13-08-2010')
<select name="date_day" id="id_date_day">
<option value="0">---</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="date_month" id="id_date_month">
<option value="0">---</option>
<option value="1">januari</option>
<option value="2">februari</option>
<option value="3">maart</option>
<option value="4">april</option>
<option value="5">mei</option>
<option value="6">juni</option>
<option value="7">juli</option>
<option value="8" selected="selected">augustus</option>
<option value="9">september</option>
<option value="10">oktober</option>
<option value="11">november</option>
<option value="12">december</option>
</select>
<select name="date_year" id="id_date_year">
<option value="0">---</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010" selected="selected">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
>>> translation.deactivate()
# MultiWidget and MultiValueField ############################################# # MultiWidget and MultiValueField #############################################
# MultiWidgets are widgets composed of other widgets. They are usually # MultiWidgets are widgets composed of other widgets. They are usually
# combined with MultiValueFields - a field that is composed of other fields. # combined with MultiValueFields - a field that is composed of other fields.