0001-misc-add-getlist-template-filter-51184.patch
combo/public/templatetags/combo.py | ||
---|---|---|
314 | 314 |
return None |
315 | 315 | |
316 | 316 | |
317 |
@register.filter |
|
318 |
def getlist(mapping, key): |
|
319 |
if mapping is None: |
|
320 |
return [] |
|
321 |
mapping = list(mapping) |
|
322 |
for value in mapping: |
|
323 |
try: |
|
324 |
yield value.get(key) |
|
325 |
except AttributeError: |
|
326 |
yield None |
|
327 | ||
328 | ||
317 | 329 |
@register.filter |
318 | 330 |
def split(string, separator=' '): |
319 | 331 |
return (force_text(string) or '').split(separator) |
tests/test_public_templatetags.py | ||
---|---|---|
136 | 136 |
assert t.render(context) == 'hello' |
137 | 137 | |
138 | 138 | |
139 |
def test_getlist(): |
|
140 |
# nothing in context |
|
141 |
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}') |
|
142 |
context = Context() |
|
143 |
assert t.render(context) == '' |
|
144 |
# non value |
|
145 |
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}') |
|
146 |
context = Context({'values': None}) |
|
147 |
assert t.render(context) == '' |
|
148 |
# not a list |
|
149 |
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}') |
|
150 |
context = Context({'values': 'foo'}) |
|
151 |
assert t.render(context) == 'None,None,None,' |
|
152 |
# not a list of dict |
|
153 |
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}') |
|
154 |
context = Context({'values': ['foo']}) |
|
155 |
assert t.render(context) == 'None,' |
|
156 | ||
157 |
t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}') |
|
158 |
context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]}) |
|
159 |
assert t.render(context) == 'bar,baz,' |
|
160 |
t = Template('{% for v in values|getlist:"unknown" %}{{ v }},{% endfor %}') |
|
161 |
context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]}) |
|
162 |
assert t.render(context) == 'None,None,' |
|
163 |
t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}') |
|
164 |
context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]}) |
|
165 |
assert t.render(context) == 'bar,baz,' |
|
166 |
t = Template('{% for v in values|getlist:"k"|getlist:"unknown" %}{{ v }},{% endfor %}') |
|
167 |
context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]}) |
|
168 |
assert t.render(context) == 'None,None,' |
|
169 |
t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}') |
|
170 |
context = Context({'values': [{'k': None}, {'k': {'v': 'baz'}}]}) |
|
171 |
assert t.render(context) == 'None,baz,' |
|
172 | ||
173 | ||
139 | 174 |
def test_split(): |
140 | 175 |
t = Template('{% for x in plop|split %}{{x}}<br>{% endfor %}') |
141 | 176 |
assert t.render(Context({'plop': 'ab cd ef'})) == 'ab<br>cd<br>ef<br>' |
tests/test_wcs_templatetags.py | ||
---|---|---|
56 | 56 | |
57 | 57 | |
58 | 58 |
def mocked_requests_send(request, **kwargs): |
59 |
data = [{'id': 1}, {'id': 2}] # fake result
|
|
59 |
data = [{'id': 1, 'fields': {'foo': 'bar'}}, {'id': 2, 'fields': {'foo': 'baz'}}] # fake result
|
|
60 | 60 |
return MockedRequestResponse(content=json.dumps({'data': data})) |
61 | 61 | |
62 | 62 | |
... | ... | |
311 | 311 |
t = Template('{% load wcs %}{{ cards|objects:"foo"|filter_by_status:"foobar"|list }}') |
312 | 312 |
t.render(context) |
313 | 313 |
assert 'filter=foobar&' in mock_send.call_args_list[0][0][0].url |
314 | ||
315 | ||
316 |
@mock.patch('combo.apps.wcs.models.requests.send', side_effect=mocked_requests_send) |
|
317 |
def test_getlist(mock_send, context, nocache): |
|
318 |
t = Template('{% load wcs %}{% for v in cards|objects:"foo"|getlist:"id" %}{{ v }},{% endfor %}') |
|
319 |
t.render(context) |
|
320 |
assert t.render(context) == "1,2," |
|
321 |
t = Template('{% load wcs %}{% for v in cards|objects:"foo"|getlist:"fields" %}{{ v }},{% endfor %}') |
|
322 |
t.render(context) |
|
323 |
assert t.render(context) == "{'foo': 'bar'},{'foo': 'baz'}," |
|
324 |
t = Template( |
|
325 |
'{% load wcs %}{% for v in cards|objects:"foo"|getlist:"fields"|getlist:"foo" %}{{ v }},{% endfor %}' |
|
326 |
) |
|
327 |
t.render(context) |
|
328 |
assert t.render(context) == "bar,baz," |
|
329 |
t = Template( |
|
330 |
'{% load wcs %}{% for v in cards|objects:"foo"|getlist:"fields"|getlist:"unknown" %}{{ v }},{% endfor %}' |
|
331 |
) |
|
332 |
t.render(context) |
|
333 |
assert t.render(context) == "None,None," |
|
314 |
- |