Refs #15667 -- Removed choices argument from some RendererMixin methods.

RendererMixin will soon be removed but this removal and the corresponding
test changes stand on their own.
This commit is contained in:
Tim Graham 2016-04-28 18:48:52 -04:00
parent f951bb78cb
commit 86573861a9
4 changed files with 53 additions and 38 deletions

View File

@ -753,20 +753,19 @@ class RendererMixin(object):
self.renderer = renderer self.renderer = renderer
super(RendererMixin, self).__init__(*args, **kwargs) super(RendererMixin, self).__init__(*args, **kwargs)
def subwidgets(self, name, value, attrs=None, choices=()): def subwidgets(self, name, value, attrs=None):
for widget in self.get_renderer(name, value, attrs, choices): for widget in self.get_renderer(name, value, attrs):
yield widget yield widget
def get_renderer(self, name, value, attrs=None, choices=()): def get_renderer(self, name, value, attrs=None):
"""Returns an instance of the renderer.""" """Returns an instance of the renderer."""
if value is None: if value is None:
value = self._empty_value value = self._empty_value
final_attrs = self.build_attrs(attrs) final_attrs = self.build_attrs(attrs)
choices = list(chain(self.choices, choices)) return self.renderer(name, value, final_attrs, self.choices)
return self.renderer(name, value, final_attrs, choices)
def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None):
return self.get_renderer(name, value, attrs, choices).render() return self.get_renderer(name, value, attrs).render()
def id_for_label(self, id_): def id_for_label(self, id_):
# Widgets using this RendererMixin are made of a collection of # Widgets using this RendererMixin are made of a collection of

View File

@ -23,8 +23,8 @@ class FormsWidgetTests(SimpleTestCase):
# RadioSelect uses a RadioFieldRenderer to render the individual radio inputs. # RadioSelect uses a RadioFieldRenderer to render the individual radio inputs.
# You can manipulate that object directly to customize the way the RadioSelect # You can manipulate that object directly to customize the way the RadioSelect
# is rendered. # is rendered.
w = RadioSelect() w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) r = w.get_renderer('beatle', 'J')
inp_set1 = [] inp_set1 = []
inp_set2 = [] inp_set2 = []
inp_set3 = [] inp_set3 = []
@ -62,8 +62,8 @@ beatle J G George False
beatle J R Ringo False""") beatle J R Ringo False""")
# A RadioFieldRenderer object also allows index access to individual RadioChoiceInput # A RadioFieldRenderer object also allows index access to individual RadioChoiceInput
w = RadioSelect() w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) r = w.get_renderer('beatle', 'J')
self.assertHTMLEqual(str(r[1]), '<label><input type="radio" name="beatle" value="P" /> Paul</label>') self.assertHTMLEqual(str(r[1]), '<label><input type="radio" name="beatle" value="P" /> Paul</label>')
self.assertHTMLEqual( self.assertHTMLEqual(
str(r[0]), str(r[0]),
@ -86,9 +86,9 @@ beatle J R Ringo False""")
class MyRenderer(RadioFieldRenderer): class MyRenderer(RadioFieldRenderer):
def render(self): def render(self):
return '<br />\n'.join(six.text_type(choice) for choice in self) return '<br />\n'.join(six.text_type(choice) for choice in self)
w = RadioSelect(renderer=MyRenderer) w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), renderer=MyRenderer)
self.assertHTMLEqual( self.assertHTMLEqual(
w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), w.render('beatle', 'G'),
"""<label><input type="radio" name="beatle" value="J" /> John</label><br /> """<label><input type="radio" name="beatle" value="J" /> John</label><br />
<label><input type="radio" name="beatle" value="P" /> Paul</label><br /> <label><input type="radio" name="beatle" value="P" /> Paul</label><br />
<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br /> <label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />
@ -98,9 +98,9 @@ beatle J R Ringo False""")
# Or you can use custom RadioSelect fields that use your custom renderer. # Or you can use custom RadioSelect fields that use your custom renderer.
class CustomRadioSelect(RadioSelect): class CustomRadioSelect(RadioSelect):
renderer = MyRenderer renderer = MyRenderer
w = CustomRadioSelect() w = CustomRadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
self.assertHTMLEqual( self.assertHTMLEqual(
w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), w.render('beatle', 'G'),
"""<label><input type="radio" name="beatle" value="J" /> John</label><br /> """<label><input type="radio" name="beatle" value="J" /> John</label><br />
<label><input type="radio" name="beatle" value="P" /> Paul</label><br /> <label><input type="radio" name="beatle" value="P" /> Paul</label><br />
<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br /> <label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />
@ -112,10 +112,8 @@ beatle J R Ringo False""")
# str is just to test some Python 2 issue with bytestrings # str is just to test some Python 2 issue with bytestrings
outer_html = str('<div{id_attr}>{content}</div>') outer_html = str('<div{id_attr}>{content}</div>')
inner_html = '<p>{choice_value}{sub_widgets}</p>' inner_html = '<p>{choice_value}{sub_widgets}</p>'
w = RadioSelect(renderer=MyRenderer) w = RadioSelect(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), renderer=MyRenderer)
output = w.render('beatle', 'J', output = w.render('beatle', 'J', attrs={'id': 'bar'})
choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')),
attrs={'id': 'bar'})
self.assertIsInstance(output, SafeData) self.assertIsInstance(output, SafeData)
self.assertHTMLEqual( self.assertHTMLEqual(
output, output,
@ -128,17 +126,36 @@ beatle J R Ringo False""")
def test_subwidget(self): def test_subwidget(self):
# Each subwidget tag gets a separate ID when the widget has an ID specified # Each subwidget tag gets a separate ID when the widget has an ID specified
self.assertHTMLEqual("\n".join(c.tag() for c in CheckboxSelectMultiple(attrs={'id': 'abc'}).subwidgets('letters', list('ac'), choices=zip(list('abc'), list('ABC')))), """<input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" /> self.assertHTMLEqual(
"\n".join(
c.tag() for c in CheckboxSelectMultiple(
attrs={'id': 'abc'},
choices=zip(list('abc'), list('ABC'))
).subwidgets('letters', list('ac'))
),
"""<input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" />
<input type="checkbox" name="letters" value="b" id="abc_1" /> <input type="checkbox" name="letters" value="b" id="abc_1" />
<input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" />""") <input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" />""")
# Each subwidget tag does not get an ID if the widget does not have an ID specified # Each subwidget tag does not get an ID if the widget does not have an ID specified
self.assertHTMLEqual("\n".join(c.tag() for c in CheckboxSelectMultiple().subwidgets('letters', list('ac'), choices=zip(list('abc'), list('ABC')))), """<input checked="checked" type="checkbox" name="letters" value="a" /> self.assertHTMLEqual(
"\n".join(c.tag() for c in CheckboxSelectMultiple(
choices=zip(list('abc'), list('ABC')),
).subwidgets('letters', list('ac'))),
"""<input checked="checked" type="checkbox" name="letters" value="a" />
<input type="checkbox" name="letters" value="b" /> <input type="checkbox" name="letters" value="b" />
<input checked="checked" type="checkbox" name="letters" value="c" />""") <input checked="checked" type="checkbox" name="letters" value="c" />""")
# The id_for_label property of the subwidget should return the ID that is used on the subwidget's tag # The id_for_label property of the subwidget should return the ID that is used on the subwidget's tag
self.assertHTMLEqual("\n".join('<input type="checkbox" name="letters" value="%s" id="%s" />' % (c.choice_value, c.id_for_label) for c in CheckboxSelectMultiple(attrs={'id': 'abc'}).subwidgets('letters', [], choices=zip(list('abc'), list('ABC')))), """<input type="checkbox" name="letters" value="a" id="abc_0" /> self.assertHTMLEqual(
"\n".join(
'<input type="checkbox" name="letters" value="%s" id="%s" />'
% (c.choice_value, c.id_for_label) for c in CheckboxSelectMultiple(
attrs={'id': 'abc'},
choices=zip(list('abc'), list('ABC')),
).subwidgets('letters', [])
),
"""<input type="checkbox" name="letters" value="a" id="abc_0" />
<input type="checkbox" name="letters" value="b" id="abc_1" /> <input type="checkbox" name="letters" value="b" id="abc_1" />
<input type="checkbox" name="letters" value="c" id="abc_2" />""") <input type="checkbox" name="letters" value="c" id="abc_2" />""")

View File

@ -4,10 +4,10 @@ from .base import WidgetTest
class CheckboxSelectMultipleTest(WidgetTest): class CheckboxSelectMultipleTest(WidgetTest):
widget = CheckboxSelectMultiple() widget = CheckboxSelectMultiple
def test_render_value(self): def test_render_value(self):
self.check_html(self.widget, 'beatles', ['J'], choices=self.beatles, html=( self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=(
"""<ul> """<ul>
<li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
@ -17,7 +17,7 @@ class CheckboxSelectMultipleTest(WidgetTest):
)) ))
def test_render_value_multiple(self): def test_render_value_multiple(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=(
"""<ul> """<ul>
<li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li> <li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
@ -30,7 +30,7 @@ class CheckboxSelectMultipleTest(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=(
"""<ul> """<ul>
<li><label><input type="checkbox" name="beatles" value="J" /> John</label></li> <li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
<li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li> <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
@ -73,8 +73,8 @@ class CheckboxSelectMultipleTest(WidgetTest):
</ul> </ul>
""" """
self.check_html( self.check_html(
self.widget, 'nestchoice', ('vinyl', 'dvd'), self.widget(choices=nested_choices), 'nestchoice', ('vinyl', 'dvd'),
choices=nested_choices, attrs={'id': 'media'}, html=html, attrs={'id': 'media'}, html=html,
) )
def test_separate_ids(self): def test_separate_ids(self):
@ -93,14 +93,13 @@ class CheckboxSelectMultipleTest(WidgetTest):
</li> </li>
</ul> </ul>
""" """
self.check_html(self.widget, 'letters', ['a', 'c'], choices=choices, attrs={'id': 'abc'}, html=html) self.check_html(self.widget(choices=choices), 'letters', ['a', 'c'], attrs={'id': 'abc'}, html=html)
def test_separate_ids_constructor(self): def test_separate_ids_constructor(self):
""" """
Each input gets a separate ID when the ID is passed to the constructor. Each input gets a separate ID when the ID is passed to the constructor.
""" """
widget = CheckboxSelectMultiple(attrs={'id': 'abc'}) widget = CheckboxSelectMultiple(attrs={'id': 'abc'}, choices=[('a', 'A'), ('b', 'B'), ('c', 'C')])
choices = [('a', 'A'), ('b', 'B'), ('c', 'C')]
html = """ html = """
<ul id="abc"> <ul id="abc">
<li> <li>
@ -112,4 +111,4 @@ class CheckboxSelectMultipleTest(WidgetTest):
</li> </li>
</ul> </ul>
""" """
self.check_html(widget, 'letters', ['a', 'c'], choices=choices, html=html) self.check_html(widget, 'letters', ['a', 'c'], html=html)

View File

@ -4,10 +4,10 @@ from .base import WidgetTest
class RadioSelectTest(WidgetTest): class RadioSelectTest(WidgetTest):
widget = RadioSelect() widget = RadioSelect
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=(
"""<ul> """<ul>
<li><label><input checked="checked" type="radio" name="beatle" value="J" /> John</label></li> <li><label><input checked="checked" type="radio" name="beatle" value="J" /> John</label></li>
<li><label><input type="radio" name="beatle" value="P" /> Paul</label></li> <li><label><input type="radio" name="beatle" value="P" /> Paul</label></li>
@ -44,7 +44,7 @@ class RadioSelectTest(WidgetTest):
</ul> </ul>
""" """
self.check_html( self.check_html(
self.widget, 'nestchoice', 'dvd', choices=nested_choices, self.widget(choices=nested_choices), 'nestchoice', 'dvd',
attrs={'id': 'media'}, html=html, attrs={'id': 'media'}, html=html,
) )
@ -53,7 +53,7 @@ class RadioSelectTest(WidgetTest):
Attributes provided at instantiation are passed to the constituent Attributes provided at instantiation are passed to the constituent
inputs. inputs.
""" """
widget = RadioSelect(attrs={'id': 'foo'}) widget = RadioSelect(attrs={'id': 'foo'}, choices=self.beatles)
html = """ html = """
<ul id="foo"> <ul id="foo">
<li> <li>
@ -64,7 +64,7 @@ class RadioSelectTest(WidgetTest):
<li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li> <li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
</ul> </ul>
""" """
self.check_html(widget, 'beatle', 'J', choices=self.beatles, html=html) self.check_html(widget, 'beatle', 'J', html=html)
def test_render_attrs(self): def test_render_attrs(self):
""" """
@ -81,4 +81,4 @@ class RadioSelectTest(WidgetTest):
<li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li> <li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
</ul> </ul>
""" """
self.check_html(self.widget, 'beatle', 'J', choices=self.beatles, attrs={'id': 'bar'}, html=html) self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', attrs={'id': 'bar'}, html=html)