Ticket #5851: 5851.diff

File 5851.diff, 4.7 KB (added by Koen Biermans, 13 years ago)

patch allow list of attrs into multiwidget

  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 1fbef98..910916b 100644
    a b class MultiWidget(Widget):  
    795795    """
    796796    def __init__(self, widgets, attrs=None):
    797797        self.widgets = [isinstance(w, type) and w() or w for w in widgets]
    798         super(MultiWidget, self).__init__(attrs)
     798        if attrs is not None:
     799            self.attrs = copy.deepcopy(attrs)
     800        else:
     801            self.attrs = {}
    799802
    800803    def render(self, name, value, attrs=None):
    801804        if self.is_localized:
    class MultiWidget(Widget):  
    806809        if not isinstance(value, list):
    807810            value = self.decompress(value)
    808811        output = []
    809         final_attrs = self.build_attrs(attrs)
    810         id_ = final_attrs.get('id', None)
    811812        for i, widget in enumerate(self.widgets):
    812813            try:
    813814                widget_value = value[i]
    814815            except IndexError:
    815816                widget_value = None
     817            if isinstance(self.attrs, list):
     818                try:
     819                    final_attrs= dict(self.attrs[i])
     820                except IndexError:
     821                    final_attrs = {}
     822            else:
     823                final_attrs = dict(self.attrs)
     824            if attrs is not None:
     825                if isinstance(attrs, list):
     826                    try:
     827                        final_attrs.update(attrs[i])
     828                    except IndexError:
     829                        pass
     830                else:
     831                    final_attrs.update(attrs)
     832            id_ = final_attrs.get('id', None)
    816833            if id_:
    817834                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
    818835            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
    class SplitDateTimeWidget(MultiWidget):  
    875892    """
    876893
    877894    def __init__(self, attrs=None, date_format=None, time_format=None):
    878         widgets = (DateInput(attrs=attrs, format=date_format),
    879                    TimeInput(attrs=attrs, format=time_format))
     895        widgets = (DateInput(format=date_format),
     896                   TimeInput(format=time_format))
    880897        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
    881898
    882899    def decompress(self, value):
  • tests/regressiontests/forms/tests/widgets.py

    diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
    index 2499b7a..edeccad 100644
    a b beatle J R Ringo False""")  
    870870        w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'})
    871871        self.assertHTMLEqual(w.render('name', ['john', 'lennon']), u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />')
    872872
     873        # test passing list of attrs, each widget gets the right attrs
     874        w = MyMultiWidget(widgets=(TextInput, TextInput),attrs=[{'class': 'big'}, {'class': 'small'}])
     875        self.assertHTMLEqual(w.render('name', ['john', 'lennon']), u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />')
     876
     877        # test incomplete list of attrs
     878        w = MyMultiWidget(widgets=(TextInput, TextInput),attrs=[{'class': 'big'},])
     879        self.assertHTMLEqual(w.render('name', ['john', 'lennon']), u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" value="lennon" name="name_1" />')
     880
    873881        w = MyMultiWidget(widgets=(TextInput(), TextInput()))
    874882
    875883        # test with no initial data
    beatle J R Ringo False""")  
    896904        w = SplitDateTimeWidget(attrs={'class': 'pretty'})
    897905        self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />')
    898906
     907        # different attrs for the subwidgets
     908        w = SplitDateTimeWidget(attrs=[{'class': 'pretty'}, {'class': 'ugly'}])
     909        self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="ugly" value="07:30:00" name="date_1" />')
     910
    899911        # Use 'date_format' and 'time_format' to change the way a value is displayed.
    900912        w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M')
    901913        self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), u'<input type="text" name="date_0" value="10/01/2006" /><input type="text" name="date_1" value="07:30" />')
Back to Top