Fixed #25731 -- Removed unused choices kwarg for Select.render()

This commit is contained in:
jpic 2016-01-25 05:15:42 +01:00 committed by Tim Graham
parent 468d8211df
commit 926e90132d
5 changed files with 40 additions and 51 deletions

View File

@ -37,7 +37,7 @@ class FilteredSelectMultiple(forms.SelectMultiple):
self.is_stacked = is_stacked self.is_stacked = is_stacked
super(FilteredSelectMultiple, self).__init__(attrs, choices) super(FilteredSelectMultiple, self).__init__(attrs, choices)
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None):
if attrs is None: if attrs is None:
attrs = {} attrs = {}
attrs['class'] = 'selectfilter' attrs['class'] = 'selectfilter'
@ -46,7 +46,7 @@ class FilteredSelectMultiple(forms.SelectMultiple):
attrs['data-field-name'] = self.verbose_name attrs['data-field-name'] = self.verbose_name
attrs['data-is-stacked'] = int(self.is_stacked) attrs['data-is-stacked'] = int(self.is_stacked)
output = super(FilteredSelectMultiple, self).render(name, value, attrs, choices) output = super(FilteredSelectMultiple, self).render(name, value, attrs)
return mark_safe(output) return mark_safe(output)

View File

@ -514,12 +514,12 @@ class Select(Widget):
memo[id(self)] = obj memo[id(self)] = obj
return obj return obj
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None):
if value is None: if value is None:
value = '' value = ''
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select{}>', flatatt(final_attrs))] output = [format_html('<select{}>', flatatt(final_attrs))]
options = self.render_options(choices, [value]) options = self.render_options([value])
if options: if options:
output.append(options) output.append(options)
output.append('</select>') output.append('</select>')
@ -541,11 +541,11 @@ class Select(Widget):
selected_html, selected_html,
force_text(option_label)) force_text(option_label))
def render_options(self, choices, selected_choices): def render_options(self, selected_choices):
# Normalize to strings. # Normalize to strings.
selected_choices = set(force_text(v) for v in selected_choices) selected_choices = set(force_text(v) for v in selected_choices)
output = [] output = []
for option_value, option_label in chain(self.choices, choices): for option_value, option_label in self.choices:
if isinstance(option_label, (list, tuple)): if isinstance(option_label, (list, tuple)):
output.append(format_html('<optgroup label="{}">', force_text(option_value))) output.append(format_html('<optgroup label="{}">', force_text(option_value)))
for option in option_label: for option in option_label:
@ -566,12 +566,12 @@ class NullBooleanSelect(Select):
('3', ugettext_lazy('No'))) ('3', ugettext_lazy('No')))
super(NullBooleanSelect, self).__init__(attrs, choices) super(NullBooleanSelect, self).__init__(attrs, choices)
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None):
try: try:
value = {True: '2', False: '3', '2': '2', '3': '3'}[value] value = {True: '2', False: '3', '2': '2', '3': '3'}[value]
except KeyError: except KeyError:
value = '1' value = '1'
return super(NullBooleanSelect, self).render(name, value, attrs, choices) return super(NullBooleanSelect, self).render(name, value, attrs)
def value_from_datadict(self, data, files, name): def value_from_datadict(self, data, files, name):
value = data.get(name) value = data.get(name)
@ -586,12 +586,12 @@ class NullBooleanSelect(Select):
class SelectMultiple(Select): class SelectMultiple(Select):
allow_multiple_selected = True allow_multiple_selected = True
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None):
if value is None: if value is None:
value = [] value = []
final_attrs = self.build_attrs(attrs, name=name) final_attrs = self.build_attrs(attrs, name=name)
output = [format_html('<select multiple="multiple"{}>', flatatt(final_attrs))] output = [format_html('<select multiple="multiple"{}>', flatatt(final_attrs))]
options = self.render_options(choices, value) options = self.render_options(value)
if options: if options:
output.append(options) output.append(options)
output.append('</select>') output.append('</select>')
@ -625,7 +625,7 @@ class ChoiceInput(SubWidget):
def __str__(self): def __str__(self):
return self.render() return self.render()
def render(self, name=None, value=None, attrs=None, choices=()): def render(self, name=None, value=None, attrs=None):
if self.id_for_label: if self.id_for_label:
label_for = format_html(' for="{}"', self.id_for_label) label_for = format_html(' for="{}"', self.id_for_label)
else: else:

View File

@ -483,6 +483,11 @@ Miscellaneous
* The default error views now raise ``TemplateDoesNotExist`` if a nonexistent * The default error views now raise ``TemplateDoesNotExist`` if a nonexistent
``template_name`` is specified. ``template_name`` is specified.
* The unused ``choices`` keyword argument of the ``Select`` and
``SelectMultiple`` widgets' ``render()`` method is removed. The ``choices``
argument of the ``render_options()`` method is also removed, making
``selected_choices`` the first argument.
.. _deprecated-features-1.10: .. _deprecated-features-1.10:
Features deprecated in 1.10 Features deprecated in 1.10

View File

@ -10,14 +10,14 @@ from .base import WidgetTest
class SelectTest(WidgetTest): class SelectTest(WidgetTest):
widget = Select() widget = Select
nested_widget = Select(choices=( nested_widget = Select(choices=(
('outer1', 'Outer 1'), ('outer1', 'Outer 1'),
('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))),
)) ))
def test_render(self): def test_render(self):
self.check_html(self.widget, 'beatle', 'J', choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', html=(
"""<select name="beatle"> """<select name="beatle">
<option value="J" selected="selected">John</option> <option value="J" selected="selected">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -30,7 +30,7 @@ class SelectTest(WidgetTest):
""" """
If the value is None, none of the options are selected. If the value is None, none of the options are selected.
""" """
self.check_html(self.widget, 'beatle', None, choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatle', None, html=(
"""<select name="beatle"> """<select name="beatle">
<option value="J">John</option> <option value="J">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -44,7 +44,7 @@ class SelectTest(WidgetTest):
If the value corresponds to a label (but not to an option value), none If the value corresponds to a label (but not to an option value), none
of the options are selected. of the options are selected.
""" """
self.check_html(self.widget, 'beatle', 'John', choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatle', 'John', html=(
"""<select name="beatle"> """<select name="beatle">
<option value="J">John</option> <option value="J">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -59,7 +59,7 @@ class SelectTest(WidgetTest):
""" """
choices = [('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra')] choices = [('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra')]
self.check_html(self.widget, 'choices', '0', choices=choices, html=( self.check_html(self.widget(choices=choices), 'choices', '0', html=(
"""<select name="choices"> """<select name="choices">
<option value="0" selected="selected">0</option> <option value="0" selected="selected">0</option>
<option value="1">1</option> <option value="1">1</option>
@ -90,8 +90,8 @@ class SelectTest(WidgetTest):
The value is compared to its str(). The value is compared to its str().
""" """
self.check_html( self.check_html(
self.widget, 'num', 2, self.widget(choices=[('1', '1'), ('2', '2'), ('3', '3')]),
choices=[('1', '1'), ('2', '2'), ('3', '3')], 'num', 2,
html=( html=(
"""<select name="num"> """<select name="num">
<option value="1">1</option> <option value="1">1</option>
@ -101,8 +101,8 @@ class SelectTest(WidgetTest):
), ),
) )
self.check_html( self.check_html(
self.widget, 'num', '2', self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
choices=[(1, 1), (2, 2), (3, 3)], 'num', '2',
html=( html=(
"""<select name="num"> """<select name="num">
<option value="1">1</option> <option value="1">1</option>
@ -112,8 +112,8 @@ class SelectTest(WidgetTest):
), ),
) )
self.check_html( self.check_html(
self.widget, 'num', 2, self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
choices=[(1, 1), (2, 2), (3, 3)], 'num', 2,
html=( html=(
"""<select name="num"> """<select name="num">
<option value="1">1</option> <option value="1">1</option>
@ -162,25 +162,9 @@ class SelectTest(WidgetTest):
</select>""" </select>"""
)) ))
def test_choices_constuctor_and_render(self):
"""
If 'choices' is passed to both the constructor and render(), then
they'll both be in the output.
"""
widget = Select(choices=[(1, 1), (2, 2), (3, 3)])
self.check_html(widget, 'num', 2, choices=[(4, 4), (5, 5)], html=(
"""<select name="num">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>"""
))
def test_choices_escaping(self): def test_choices_escaping(self):
choices = (('bad', 'you & me'), ('good', mark_safe('you &gt; me'))) choices = (('bad', 'you & me'), ('good', mark_safe('you &gt; me')))
self.check_html(self.widget, 'escape', None, choices=choices, html=( self.check_html(self.widget(choices=choices), 'escape', None, html=(
"""<select name="escape"> """<select name="escape">
<option value="bad">you &amp; me</option> <option value="bad">you &amp; me</option>
<option value="good">you &gt; me</option> <option value="good">you &gt; me</option>
@ -189,8 +173,8 @@ class SelectTest(WidgetTest):
def test_choices_unicode(self): def test_choices_unicode(self):
self.check_html( self.check_html(
self.widget, 'email', 'ŠĐĆŽćžšđ', self.widget(choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]),
choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')], 'email', 'ŠĐĆŽćžšđ',
html=( html=(
"""<select name="email"> """<select name="email">
<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected"> <option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">

View File

@ -4,11 +4,11 @@ from .base import WidgetTest
class SelectMultipleTest(WidgetTest): class SelectMultipleTest(WidgetTest):
widget = SelectMultiple() widget = SelectMultiple
numeric_choices = (('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra')) numeric_choices = (('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra'))
def test_render_selected(self): def test_render_selected(self):
self.check_html(self.widget, 'beatles', ['J'], choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=(
"""<select multiple="multiple" name="beatles"> """<select multiple="multiple" name="beatles">
<option value="J" selected="selected">John</option> <option value="J" selected="selected">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -18,7 +18,7 @@ class SelectMultipleTest(WidgetTest):
)) ))
def test_render_multiple_selected(self): def test_render_multiple_selected(self):
self.check_html(self.widget, 'beatles', ['J', 'P'], choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatles', ['J', 'P'], html=(
"""<select multiple="multiple" name="beatles"> """<select multiple="multiple" name="beatles">
<option value="J" selected="selected">John</option> <option value="J" selected="selected">John</option>
<option value="P" selected="selected">Paul</option> <option value="P" selected="selected">Paul</option>
@ -31,7 +31,7 @@ class SelectMultipleTest(WidgetTest):
""" """
If the value is None, none of the options are selected. If the value is None, none of the options are selected.
""" """
self.check_html(self.widget, 'beatles', None, choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatles', None, html=(
"""<select multiple="multiple" name="beatles"> """<select multiple="multiple" name="beatles">
<option value="J">John</option> <option value="J">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -45,7 +45,7 @@ class SelectMultipleTest(WidgetTest):
If the value corresponds to a label (but not to an option value), none If the value corresponds to a label (but not to an option value), none
of the options are selected. of the options are selected.
""" """
self.check_html(self.widget, 'beatles', ['John'], choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatles', ['John'], html=(
"""<select multiple="multiple" name="beatles"> """<select multiple="multiple" name="beatles">
<option value="J">John</option> <option value="J">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -58,7 +58,7 @@ class SelectMultipleTest(WidgetTest):
""" """
Multiple options with the same value can be selected (#8103). Multiple options with the same value can be selected (#8103).
""" """
self.check_html(self.widget, 'choices', ['0'], choices=self.numeric_choices, html=( self.check_html(self.widget(choices=self.numeric_choices), 'choices', ['0'], html=(
"""<select multiple="multiple" name="choices"> """<select multiple="multiple" name="choices">
<option value="0" selected="selected">0</option> <option value="0" selected="selected">0</option>
<option value="1">1</option> <option value="1">1</option>
@ -73,7 +73,7 @@ class SelectMultipleTest(WidgetTest):
If multiple values are given, but some of them are not valid, the valid If multiple values are given, but some of them are not valid, the valid
ones are selected. ones are selected.
""" """
self.check_html(self.widget, 'beatles', ['J', 'G', 'foo'], choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatles', ['J', 'G', 'foo'], html=(
"""<select multiple="multiple" name="beatles"> """<select multiple="multiple" name="beatles">
<option value="J" selected="selected">John</option> <option value="J" selected="selected">John</option>
<option value="P">Paul</option> <option value="P">Paul</option>
@ -85,7 +85,7 @@ class SelectMultipleTest(WidgetTest):
def test_compare_string(self): def test_compare_string(self):
choices = [('1', '1'), ('2', '2'), ('3', '3')] choices = [('1', '1'), ('2', '2'), ('3', '3')]
self.check_html(self.widget, 'nums', [2], choices=choices, html=( self.check_html(self.widget(choices=choices), 'nums', [2], html=(
"""<select multiple="multiple" name="nums"> """<select multiple="multiple" name="nums">
<option value="1">1</option> <option value="1">1</option>
<option value="2" selected="selected">2</option> <option value="2" selected="selected">2</option>
@ -93,7 +93,7 @@ class SelectMultipleTest(WidgetTest):
</select>""" </select>"""
)) ))
self.check_html(self.widget, 'nums', ['2'], choices=choices, html=( self.check_html(self.widget(choices=choices), 'nums', ['2'], html=(
"""<select multiple="multiple" name="nums"> """<select multiple="multiple" name="nums">
<option value="1">1</option> <option value="1">1</option>
<option value="2" selected="selected">2</option> <option value="2" selected="selected">2</option>
@ -101,7 +101,7 @@ class SelectMultipleTest(WidgetTest):
</select>""" </select>"""
)) ))
self.check_html(self.widget, 'nums', [2], choices=choices, html=( self.check_html(self.widget(choices=choices), 'nums', [2], html=(
"""<select multiple="multiple" name="nums"> """<select multiple="multiple" name="nums">
<option value="1">1</option> <option value="1">1</option>
<option value="2" selected="selected">2</option> <option value="2" selected="selected">2</option>