Ticket #5917: 5917.diff

File 5917.diff, 3.6 KB (added by SmileyChris, 8 years ago)

Allow widget to receive a datetime and also render invalid dates (validation doesn't belong in the widget)

  • django/newforms/extras/widgets.py

     
    33"""
    44
    55import datetime
     6import re
    67
    78from django.newforms.widgets import Widget, Select
    89from django.utils.dates import MONTHS
     
    1011
    1112__all__ = ('SelectDateWidget',)
    1213
     14RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
     15
    1316class SelectDateWidget(Widget):
    1417    """
    1518    A Widget that splits date input into three <select> boxes.
     
    3235
    3336    def render(self, name, value, attrs=None):
    3437        try:
    35             value = datetime.date(*map(int, value.split('-')))
    3638            year_val, month_val, day_val = value.year, value.month, value.day
    37         except (AttributeError, TypeError, ValueError):
     39        except AttributeError:
    3840            year_val = month_val = day_val = None
     41            if isinstance(value, basestring):
     42                match = RE_DATE.match(value)
     43                if match:
     44                    year_val, month_val, day_val = [int(v) for v in match.groups()]
    3945
    4046        output = []
    4147
  • tests/regressiontests/forms/extra.py

     
    144144<option value="2016">2016</option>
    145145</select>
    146146
     147Accepts a datetime or a string:
     148
     149>>> w.render('mydate', datetime.date(2010, 4, 15)) == w.render('mydate', '2010-04-15')
     150True
     151
     152Invalid dates still render the failed date:
     153>>> print w.render('mydate', '2010-02-31')
     154<select name="mydate_month">
     155<option value="1">January</option>
     156<option value="2" selected="selected">February</option>
     157<option value="3">March</option>
     158<option value="4">April</option>
     159<option value="5">May</option>
     160<option value="6">June</option>
     161<option value="7">July</option>
     162<option value="8">August</option>
     163<option value="9">September</option>
     164<option value="10">October</option>
     165<option value="11">November</option>
     166<option value="12">December</option>
     167</select>
     168<select name="mydate_day">
     169<option value="1">1</option>
     170<option value="2">2</option>
     171<option value="3">3</option>
     172<option value="4">4</option>
     173<option value="5">5</option>
     174<option value="6">6</option>
     175<option value="7">7</option>
     176<option value="8">8</option>
     177<option value="9">9</option>
     178<option value="10">10</option>
     179<option value="11">11</option>
     180<option value="12">12</option>
     181<option value="13">13</option>
     182<option value="14">14</option>
     183<option value="15">15</option>
     184<option value="16">16</option>
     185<option value="17">17</option>
     186<option value="18">18</option>
     187<option value="19">19</option>
     188<option value="20">20</option>
     189<option value="21">21</option>
     190<option value="22">22</option>
     191<option value="23">23</option>
     192<option value="24">24</option>
     193<option value="25">25</option>
     194<option value="26">26</option>
     195<option value="27">27</option>
     196<option value="28">28</option>
     197<option value="29">29</option>
     198<option value="30">30</option>
     199<option value="31" selected="selected">31</option>
     200</select>
     201<select name="mydate_year">
     202<option value="2007">2007</option>
     203<option value="2008">2008</option>
     204<option value="2009">2009</option>
     205<option value="2010" selected="selected">2010</option>
     206<option value="2011">2011</option>
     207<option value="2012">2012</option>
     208<option value="2013">2013</option>
     209<option value="2014">2014</option>
     210<option value="2015">2015</option>
     211<option value="2016">2016</option>
     212</select>
     213
    147214Using a SelectDateWidget in a form:
    148215
    149216>>> class GetDate(Form):
Back to Top