Ticket #7132: 7132.selectmultiple-fix.2.diff

File 7132.selectmultiple-fix.2.diff, 4.9 KB (added by Alex Gaynor, 17 years ago)

Added tests on CheckboxSelectMultiple

  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 9464107..27c4095 100644
    a b from django.utils.datastructures import MultiValueDict  
    77from django.utils.text import capfirst, truncate_words
    88from django.utils.translation import ugettext as _
    99from django.utils.safestring import mark_safe
     10from django.utils.encoding import force_unicode
    1011from django.conf import settings
    1112
    1213class FilteredSelectMultiple(forms.SelectMultiple):
    class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):  
    135136        if value:
    136137            return [value]
    137138        return None
     139   
     140    def _has_changed(self, initial, data):
     141        if data is None:
     142            data = []
     143        if len(initial) != len(data):
     144            return True
     145        for pk1, pk2 in zip(initial, data):
     146            if force_unicode(pk1) != force_unicode(pk2):
     147                return True
     148        return False
    138149
    139150class RelatedFieldWidgetWrapper(object):
    140151    """
  • django/newforms/widgets.py

    diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
    index 16e6173..3e70972 100644
    a b class SelectMultiple(Widget):  
    401401        if isinstance(data, MultiValueDict):
    402402            return data.getlist(name)
    403403        return data.get(name, None)
     404   
     405    def _has_changed(self, initial, data):
     406        if data is None:
     407            data = []
     408        if len(initial) != len(data):
     409            return True
     410        for pk1, pk2 in zip(initial, data):
     411            if force_unicode(pk1) != force_unicode(pk2):
     412                return True
     413        return False
    404414
    405415class RadioInput(StrAndUnicode):
    406416    """
  • tests/regressiontests/admin_widgets/models.py

    diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
    index 7207e8e..e32a200 100644
    a b Currently: <a target="_blank" href="%(MEDIA_URL)stest">test</a> <br />Change: <i  
    6666>>> w = ManyToManyRawIdWidget(rel)
    6767>>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
    6868<input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>
     69>>> w._has_changed([], None)
     70False
     71>>> w._has_changed([1, 2], [u'1', u'2'])
     72False
     73>>> w._has_changed([1, 2], [u'1'])
     74True
     75>>> w._has_changed([1, 2], [u'1', u'3'])
     76True
    6977
    7078""" % {
    7179    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
  • tests/regressiontests/forms/widgets.py

    diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
    index 4944a26..63dd7fe 100644
    a b If 'choices' is passed to both the constructor and render(), then they'll both b  
    612612>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
    613613u'<select multiple="multiple" name="nums">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>'
    614614
     615# Test the usage of _has_changed
     616>>> w._has_changed([], None)
     617False
     618>>> w._has_changed([1, 2], [u'1', u'2'])
     619False
     620>>> w._has_changed([1, 2], [u'1'])
     621True
     622>>> w._has_changed([1, 2], [u'1', u'3'])
     623True
     624
    615625# RadioSelect Widget ##########################################################
    616626
    617627>>> w = RadioSelect()
    If 'choices' is passed to both the constructor and render(), then they'll both b  
    914924>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
    915925u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
    916926
     927>>> w._has_changed([], None)
     928False
     929>>> w._has_changed([1, 2], [u'1', u'2'])
     930False
     931>>> w._has_changed([1, 2], [u'1'])
     932True
     933>>> w._has_changed([1, 2], [u'1', u'3'])
     934True
     935
    917936# MultiWidget #################################################################
    918937
    919938>>> class MyMultiWidget(MultiWidget):
Back to Top