Ticket #9216: widget_assert_list_or_tuple.diff

File widget_assert_list_or_tuple.diff, 2.0 KB (added by Thomas Güttler, 16 years ago)
  • tests/regressiontests/forms/media.py

     
    355355<script type="text/javascript" src="/path/to/js4"></script>
    356356<script type="text/javascript" src="/some/form/javascript"></script>
    357357
     358###############################################################
     359# Assertions: js and css attributes need a list or tuple
     360###############################################################
     361
     362# Test Assertion that you need to pass in a tuple or list
     363>>> class MyBrokenCSSWidget(MyWidget1):
     364...     class Media:
     365...         css={'all': 'foo.css'}
     366>>> widget=MyBrokenCSSWidget()
     367>>> unicode(widget.media)
     368Traceback (most recent call last):
     369...
     370AssertionError: Need tuple or list not <type 'str'>: foo.css
     371
     372# Test Assertion JS
     373>>> class MyBrokenJSWidget(MyWidget1):
     374...     class Media:
     375...         js='foo.js'
     376>>> widget=MyBrokenJSWidget()
     377>>> unicode(widget.media)
     378Traceback (most recent call last):
     379...
     380AssertionError: Need tuple or list not <type 'str'>: foo.js
     381
    358382>>> settings.MEDIA_URL = ORIGINAL_MEDIA_URL
  • django/forms/widgets.py

     
    8080
    8181    def add_js(self, data):
    8282        if data:
     83            assert isinstance(data, (tuple, list)), 'Need tuple or list not %s: %s' % (type(data), data)
    8384            self._js.extend([path for path in data if path not in self._js])
    8485
    8586    def add_css(self, data):
    8687        if data:
    8788            for medium, paths in data.items():
     89                assert isinstance(paths, (tuple, list)), 'Need tuple or list not %s: %s' % (type(paths), paths)
    8890                self._css.setdefault(medium, []).extend([path for path in paths if path not in self._css[medium]])
    8991
    9092    def __add__(self, other):
Back to Top