misc: add |getlist template filter (#51184)
This commit is contained in:
parent
58de5909fe
commit
a11d858b0d
|
@ -314,6 +314,18 @@ def get(obj, key):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@register.filter
|
||||||
|
def getlist(mapping, key):
|
||||||
|
if mapping is None:
|
||||||
|
return []
|
||||||
|
mapping = list(mapping)
|
||||||
|
for value in mapping:
|
||||||
|
try:
|
||||||
|
yield value.get(key)
|
||||||
|
except AttributeError:
|
||||||
|
yield None
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def split(string, separator=' '):
|
def split(string, separator=' '):
|
||||||
return (force_text(string) or '').split(separator)
|
return (force_text(string) or '').split(separator)
|
||||||
|
|
|
@ -136,6 +136,41 @@ def test_get():
|
||||||
assert t.render(context) == 'hello'
|
assert t.render(context) == 'hello'
|
||||||
|
|
||||||
|
|
||||||
|
def test_getlist():
|
||||||
|
# nothing in context
|
||||||
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context()
|
||||||
|
assert t.render(context) == ''
|
||||||
|
# non value
|
||||||
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': None})
|
||||||
|
assert t.render(context) == ''
|
||||||
|
# not a list
|
||||||
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': 'foo'})
|
||||||
|
assert t.render(context) == 'None,None,None,'
|
||||||
|
# not a list of dict
|
||||||
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': ['foo']})
|
||||||
|
assert t.render(context) == 'None,'
|
||||||
|
|
||||||
|
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]})
|
||||||
|
assert t.render(context) == 'bar,baz,'
|
||||||
|
t = Template('{% for v in values|getlist:"unknown" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]})
|
||||||
|
assert t.render(context) == 'None,None,'
|
||||||
|
t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]})
|
||||||
|
assert t.render(context) == 'bar,baz,'
|
||||||
|
t = Template('{% for v in values|getlist:"k"|getlist:"unknown" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]})
|
||||||
|
assert t.render(context) == 'None,None,'
|
||||||
|
t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}')
|
||||||
|
context = Context({'values': [{'k': None}, {'k': {'v': 'baz'}}]})
|
||||||
|
assert t.render(context) == 'None,baz,'
|
||||||
|
|
||||||
|
|
||||||
def test_split():
|
def test_split():
|
||||||
t = Template('{% for x in plop|split %}{{x}}<br>{% endfor %}')
|
t = Template('{% for x in plop|split %}{{x}}<br>{% endfor %}')
|
||||||
assert t.render(Context({'plop': 'ab cd ef'})) == 'ab<br>cd<br>ef<br>'
|
assert t.render(Context({'plop': 'ab cd ef'})) == 'ab<br>cd<br>ef<br>'
|
||||||
|
|
|
@ -56,7 +56,7 @@ class MockedRequestResponse(mock.Mock):
|
||||||
|
|
||||||
|
|
||||||
def mocked_requests_send(request, **kwargs):
|
def mocked_requests_send(request, **kwargs):
|
||||||
data = [{'id': 1}, {'id': 2}] # fake result
|
data = [{'id': 1, 'fields': {'foo': 'bar'}}, {'id': 2, 'fields': {'foo': 'baz'}}] # fake result
|
||||||
return MockedRequestResponse(content=json.dumps({'data': data}))
|
return MockedRequestResponse(content=json.dumps({'data': data}))
|
||||||
|
|
||||||
|
|
||||||
|
@ -311,3 +311,23 @@ def test_filter_by_status(mock_send, context, nocache):
|
||||||
t = Template('{% load wcs %}{{ cards|objects:"foo"|filter_by_status:"foobar"|list }}')
|
t = Template('{% load wcs %}{{ cards|objects:"foo"|filter_by_status:"foobar"|list }}')
|
||||||
t.render(context)
|
t.render(context)
|
||||||
assert 'filter=foobar&' in mock_send.call_args_list[0][0][0].url
|
assert 'filter=foobar&' in mock_send.call_args_list[0][0][0].url
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('combo.apps.wcs.models.requests.send', side_effect=mocked_requests_send)
|
||||||
|
def test_getlist(mock_send, context, nocache):
|
||||||
|
t = Template('{% load wcs %}{% for v in cards|objects:"foo"|getlist:"id" %}{{ v }},{% endfor %}')
|
||||||
|
t.render(context)
|
||||||
|
assert t.render(context) == "1,2,"
|
||||||
|
t = Template('{% load wcs %}{% for v in cards|objects:"foo"|getlist:"fields" %}{{ v }},{% endfor %}')
|
||||||
|
t.render(context)
|
||||||
|
assert t.render(context) == "{'foo': 'bar'},{'foo': 'baz'},"
|
||||||
|
t = Template(
|
||||||
|
'{% load wcs %}{% for v in cards|objects:"foo"|getlist:"fields"|getlist:"foo" %}{{ v }},{% endfor %}'
|
||||||
|
)
|
||||||
|
t.render(context)
|
||||||
|
assert t.render(context) == "bar,baz,"
|
||||||
|
t = Template(
|
||||||
|
'{% load wcs %}{% for v in cards|objects:"foo"|getlist:"fields"|getlist:"unknown" %}{{ v }},{% endfor %}'
|
||||||
|
)
|
||||||
|
t.render(context)
|
||||||
|
assert t.render(context) == "None,None,"
|
||||||
|
|
Loading…
Reference in New Issue