0001-misc-avoid-crashes-when-getlist-filter-not-used-on-a.patch
tests/test_templates.py | ||
---|---|---|
952 | 952 |
assert tmpl.render({'list': None}) == '' # list is not iterable |
953 | 953 |
assert tmpl.render({'list': '123'}) == '' # consider string as not iterable |
954 | 954 |
assert tmpl.render({}) == '' |
955 | ||
956 | ||
957 |
def test_getlist(): |
|
958 |
class FakeBlock: |
|
959 |
def getlist(self, key): |
|
960 |
return {'foo': ['foo1', 'foo2'], 'bar': ['bar1', 'bar2']}[key] |
|
961 |
tmpl = Template('{% for x in egg|getlist:coin %}{{x}}{% endfor %}') |
|
962 |
assert tmpl.render({'egg': FakeBlock(), 'coin': 'foo'}) == 'foo1foo2' |
|
963 |
assert tmpl.render({'egg': FakeBlock(), 'coin': 'bar'}) == 'bar1bar2' |
|
964 |
tmpl = Template('{{ egg|getlist:"foo"|length }}') |
|
965 |
assert tmpl.render({'egg': FakeBlock()}) == '2' |
|
966 |
assert tmpl.render({}) == '0' |
|
967 |
assert tmpl.render({'egg': None}) == '0' |
|
968 |
assert tmpl.render({'egg': 'spam'}) == '0' |
|
969 |
assert tmpl.render({'egg': 42}) == '0' |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
65 | 65 | |
66 | 66 |
@register.filter |
67 | 67 |
def getlist(mapping, key): |
68 |
if not mapping or not hasattr(mapping, 'getlist'): |
|
69 |
return [] |
|
68 | 70 |
return mapping.getlist(key) |
69 | 71 | |
70 | 72 | |
71 |
- |