Ticket #9124: selectdatewidget.diff

File selectdatewidget.diff, 7.7 KB (added by Bernd Schlapsi, 16 years ago)

Patch to add the requested feature for selectdatewidget

  • django/forms/extras/widgets.py

     
    2020    This also serves as an example of a Widget that has more than one HTML
    2121    element and hence implements value_from_datadict.
    2222    """
     23    none_value = (0, '---')
    2324    month_field = '%s_month'
    2425    day_field = '%s_day'
    2526    year_field = '%s_year'
    2627
    27     def __init__(self, attrs=None, years=None):
     28    def __init__(self, attrs=None, years=None, required=True):
    2829        # years is an optional list/tuple of years to use in the "year" select box.
    2930        self.attrs = attrs or {}
     31        self.required= required
    3032        if years:
    3133            self.years = years
    3234        else:
     
    5153            id_ = 'id_%s' % name
    5254
    5355        month_choices = MONTHS.items()
     56        if not self.required or not value:
     57            month_choices.append(self.none_value)
    5458        month_choices.sort()
    5559        local_attrs = self.build_attrs(id=self.month_field % id_)
    5660        select_html = Select(choices=month_choices).render(self.month_field % name, month_val, local_attrs)
    5761        output.append(select_html)
    5862
    5963        day_choices = [(i, i) for i in range(1, 32)]
     64        if not self.required or not value:
     65            day_choices.insert(0, self.none_value)
    6066        local_attrs['id'] = self.day_field % id_
    6167        select_html = Select(choices=day_choices).render(self.day_field % name, day_val, local_attrs)
    6268        output.append(select_html)
    6369
    6470        year_choices = [(i, i) for i in self.years]
     71        if not self.required or not value:
     72            year_choices.insert(0, self.none_value)
    6573        local_attrs['id'] = self.year_field % id_
    6674        select_html = Select(choices=year_choices).render(self.year_field % name, year_val, local_attrs)
    6775        output.append(select_html)
     
    7482
    7583    def value_from_datadict(self, data, files, name):
    7684        y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
     85        if y == m == d == "0":
     86            return None
    7787        if y and m and d:
    7888            return '%s-%s-%s' % (y, m, d)
    7989        return data.get(name, None)
  • tests/regressiontests/forms/extra.py

     
    2323>>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'))
    2424>>> print w.render('mydate', '')
    2525<select name="mydate_month" id="id_mydate_month">
     26<option value="0">---</option>
    2627<option value="1">January</option>
    2728<option value="2">February</option>
    2829<option value="3">March</option>
     
    3738<option value="12">December</option>
    3839</select>
    3940<select name="mydate_day" id="id_mydate_day">
     41<option value="0">---</option>
    4042<option value="1">1</option>
    4143<option value="2">2</option>
    4244<option value="3">3</option>
     
    7072<option value="31">31</option>
    7173</select>
    7274<select name="mydate_year" id="id_mydate_year">
     75<option value="0">---</option>
    7376<option value="2007">2007</option>
    7477<option value="2008">2008</option>
    7578<option value="2009">2009</option>
     
    213216
    214217Using a SelectDateWidget in a form:
    215218
     219>>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False)
     220>>> print w.render('mydate', '')
     221<select name="mydate_month" id="id_mydate_month">
     222<option value="0">---</option>
     223<option value="1">January</option>
     224<option value="2">February</option>
     225<option value="3">March</option>
     226<option value="4">April</option>
     227<option value="5">May</option>
     228<option value="6">June</option>
     229<option value="7">July</option>
     230<option value="8">August</option>
     231<option value="9">September</option>
     232<option value="10">October</option>
     233<option value="11">November</option>
     234<option value="12">December</option>
     235</select>
     236<select name="mydate_day" id="id_mydate_day">
     237<option value="0">---</option>
     238<option value="1">1</option>
     239<option value="2">2</option>
     240<option value="3">3</option>
     241<option value="4">4</option>
     242<option value="5">5</option>
     243<option value="6">6</option>
     244<option value="7">7</option>
     245<option value="8">8</option>
     246<option value="9">9</option>
     247<option value="10">10</option>
     248<option value="11">11</option>
     249<option value="12">12</option>
     250<option value="13">13</option>
     251<option value="14">14</option>
     252<option value="15">15</option>
     253<option value="16">16</option>
     254<option value="17">17</option>
     255<option value="18">18</option>
     256<option value="19">19</option>
     257<option value="20">20</option>
     258<option value="21">21</option>
     259<option value="22">22</option>
     260<option value="23">23</option>
     261<option value="24">24</option>
     262<option value="25">25</option>
     263<option value="26">26</option>
     264<option value="27">27</option>
     265<option value="28">28</option>
     266<option value="29">29</option>
     267<option value="30">30</option>
     268<option value="31">31</option>
     269</select>
     270<select name="mydate_year" id="id_mydate_year">
     271<option value="0">---</option>
     272<option value="2007">2007</option>
     273<option value="2008">2008</option>
     274<option value="2009">2009</option>
     275<option value="2010">2010</option>
     276<option value="2011">2011</option>
     277<option value="2012">2012</option>
     278<option value="2013">2013</option>
     279<option value="2014">2014</option>
     280<option value="2015">2015</option>
     281<option value="2016">2016</option>
     282</select>
     283>>> print w.render('mydate', '2010-04-15')
     284<select name="mydate_month" id="id_mydate_month">
     285<option value="0">---</option>
     286<option value="1">January</option>
     287<option value="2">February</option>
     288<option value="3">March</option>
     289<option value="4" selected="selected">April</option>
     290<option value="5">May</option>
     291<option value="6">June</option>
     292<option value="7">July</option>
     293<option value="8">August</option>
     294<option value="9">September</option>
     295<option value="10">October</option>
     296<option value="11">November</option>
     297<option value="12">December</option>
     298</select>
     299<select name="mydate_day" id="id_mydate_day">
     300<option value="0">---</option>
     301<option value="1">1</option>
     302<option value="2">2</option>
     303<option value="3">3</option>
     304<option value="4">4</option>
     305<option value="5">5</option>
     306<option value="6">6</option>
     307<option value="7">7</option>
     308<option value="8">8</option>
     309<option value="9">9</option>
     310<option value="10">10</option>
     311<option value="11">11</option>
     312<option value="12">12</option>
     313<option value="13">13</option>
     314<option value="14">14</option>
     315<option value="15" selected="selected">15</option>
     316<option value="16">16</option>
     317<option value="17">17</option>
     318<option value="18">18</option>
     319<option value="19">19</option>
     320<option value="20">20</option>
     321<option value="21">21</option>
     322<option value="22">22</option>
     323<option value="23">23</option>
     324<option value="24">24</option>
     325<option value="25">25</option>
     326<option value="26">26</option>
     327<option value="27">27</option>
     328<option value="28">28</option>
     329<option value="29">29</option>
     330<option value="30">30</option>
     331<option value="31">31</option>
     332</select>
     333<select name="mydate_year" id="id_mydate_year">
     334<option value="0">---</option>
     335<option value="2007">2007</option>
     336<option value="2008">2008</option>
     337<option value="2009">2009</option>
     338<option value="2010" selected="selected">2010</option>
     339<option value="2011">2011</option>
     340<option value="2012">2012</option>
     341<option value="2013">2013</option>
     342<option value="2014">2014</option>
     343<option value="2015">2015</option>
     344<option value="2016">2016</option>
     345</select>
    216346>>> class GetDate(Form):
    217347...     mydate = DateField(widget=SelectDateWidget)
    218348>>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})
Back to Top