wcs: do not show A anchor if URL and label are empty (#62137)
gitea-wip/combo/pipeline/head Build started... Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Lauréline Guérin 2022-08-30 16:43:32 +02:00
parent 77fa421ef4
commit f8fcd6b31a
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 98 additions and 35 deletions

View File

@ -37,7 +37,9 @@
<div class="{{ item.cell_size|default:"" }}">
{% with item.page|default:item.url_template as url_key %}
{% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
<div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
{% if link_label and link_url %}
<div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
{% endif %}
{% endwith %}
{% endwith %}
</div>

View File

@ -9,7 +9,11 @@
{% if item.template and item.page|default:item.url_template %}
{% with item.page|default:item.url_template as url_key %}
{% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
{% if not ul_display %}<td>{% endif %}<a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a>{% if not ul_display %}</td>{% endif %}
{% if not ul_display %}<td>{% endif %}
{% if link_label and link_url %}
<a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a>
{% endif %}
{% if not ul_display %}</td>{% endif %}
{% endwith %}
{% endwith %}
{% endif %}

View File

@ -15,8 +15,14 @@
{% if custom_schema.grid_headers %}
<thead>
{% for item in custom_schema.cells %}
{% if item.varname == "@custom@" or item.varname == "@link@" %}
{% if item.varname == "@custom@" %}
{% if item.template %}
<th>{{ item.header|default:"" }}</th>
{% endif %}
{% elif item.varname == "@link@" %}
{% if item.template and item.page|default:item.url_template %}
<th>{{ item.header|default:"" }}</th>
{% endif %}
{% else %}
{% if item.varname %}
{% with fields_by_varnames|get:item.varname as field %}

View File

@ -1056,6 +1056,19 @@ def test_card_cell_table_mode_render_custom_schema_link_entry(mock_send, context
cell.save()
test('<i>a</i> - Foo Bar', '/foo/bar/42/', 'pk-button')
# empty label or empty url: no link in output
cell.custom_schema['cells'][0]['url_template'] = '{{ None|default:"" }}'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('ul li a') == []
assert PyQuery(result).find('table tr td a') == []
cell.custom_schema['cells'][0]['url_template'] = 'foo/bar'
cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('ul li a') == []
assert PyQuery(result).find('table tr td a') == []
# check with page link
root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
page1 = Page.objects.create(
@ -1074,7 +1087,9 @@ def test_card_cell_table_mode_render_custom_schema_link_entry(mock_send, context
parent=other_root_page,
)
cell.custom_schema['cells'][0]['url_template'] = ''
cell.custom_schema['cells'][0]['page'] = page1.pk
cell.custom_schema['cells'][0]['template'] = '{{ card.fields.fielda }} - {{ card.fields.related }}'
cell.save()
test('<i>a</i> - Foo Bar', '/root/card/11/', 'pk-button')
@ -1082,6 +1097,19 @@ def test_card_cell_table_mode_render_custom_schema_link_entry(mock_send, context
cell.save()
test('<i>a</i> - Foo Bar', '/other-root/card-bis/11/', 'pk-button')
# empty label or empty url: no link in output
cell.custom_schema['cells'][0]['page'] = 0
cell.save()
result = cell.render(context)
assert PyQuery(result).find('ul li a') == []
assert PyQuery(result).find('table tr td a') == []
cell.custom_schema['cells'][0]['page'] = page1.pk
cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('ul li a') == []
assert PyQuery(result).find('table tr td a') == []
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
@pytest.mark.parametrize('with_headers', [True, False])
@ -1095,12 +1123,28 @@ def test_card_cell_table_mode_render_with_headers(mock_send, context, with_heade
custom_schema={
'grid_headers': with_headers,
'cells': [
{'varname': '@custom@', 'template': "<b>Foo</b> bar'baz {{ card.fields.fielde }}"},
{'varname': '@custom@', 'template': "foo bar"},
{'varname': '@custom@', 'template': "foo bar bis", "header": "My Custom Header"},
{'varname': '@custom@', 'template': ""}, # not displayed
{'varname': 'fieldb'},
{'varname': 'user:name'},
{'varname': 'user:email'},
{'varname': 'user:first_name'},
{'varname': 'user:last_name'},
{'varname': '@link@', 'template': "Foo", 'url_template': 'http://foo/bar', 'header': 'Link'},
{'varname': '@link@', 'template': "Bar", 'url_template': '{# empty #}', 'header': 'Link Bis'},
{
'varname': '@link@',
'template': "",
'url_template': 'http://foo/bar',
'header': 'Link Not Displayed',
},
{
'varname': '@link@',
'template': "Bar",
'url_template': '',
'header': 'Link Bis Not Displayed',
},
{'varname': 'user:unknown'},
],
},
@ -1112,37 +1156,20 @@ def test_card_cell_table_mode_render_with_headers(mock_send, context, with_heade
cell.modify_global_context(context, request)
context['synchronous'] = True # to get fresh content
def test(value):
result = cell.render(context)
if with_headers:
assert len(PyQuery(result).find('table thead th')) == 6
assert PyQuery(result).find('table thead th:nth-child(1)').text() == value
assert PyQuery(result).find('table thead th:nth-child(2)').text() == 'Field B'
assert PyQuery(result).find('table thead th:nth-child(3)').text() == 'Name'
assert PyQuery(result).find('table thead th:nth-child(4)').text() == 'Email'
assert PyQuery(result).find('table thead th:nth-child(5)').text() == 'First name'
assert PyQuery(result).find('table thead th:nth-child(6)').text() == 'Last name'
else:
assert PyQuery(result).find('table thead') == []
test('')
cell.custom_schema['cells'][0]['header'] = 'My custom header'
cell.save()
test('My custom header')
cell.custom_schema['cells'][0] = {
'varname': '@link@',
'url_template': '/foo/bar/',
'template': 'Foo bar',
'display_mode': 'link',
}
cell.save()
test('')
cell.custom_schema['cells'][0]['header'] = 'My custom header'
cell.save()
test('My custom header')
result = cell.render(context)
if with_headers:
assert len(PyQuery(result).find('table thead th')) == 9
assert PyQuery(result).find('table thead th:nth-child(1)').text() == ''
assert PyQuery(result).find('table thead th:nth-child(2)').text() == 'My Custom Header'
assert PyQuery(result).find('table thead th:nth-child(3)').text() == 'Field B'
assert PyQuery(result).find('table thead th:nth-child(4)').text() == 'Name'
assert PyQuery(result).find('table thead th:nth-child(5)').text() == 'Email'
assert PyQuery(result).find('table thead th:nth-child(6)').text() == 'First name'
assert PyQuery(result).find('table thead th:nth-child(7)').text() == 'Last name'
assert PyQuery(result).find('table thead th:nth-child(8)').text() == 'Link'
assert PyQuery(result).find('table thead th:nth-child(9)').text() == 'Link Bis'
else:
assert PyQuery(result).find('table thead') == []
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
@ -2064,6 +2091,17 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
in cell_resp
)
# empty label or empty url: no link in output
cell.custom_schema['cells'][0]['url_template'] = '{{ None|default:"" }}'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a') == []
cell.custom_schema['cells'][0]['url_template'] = 'foo/bar'
cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a') == []
# check with page link
root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
page1 = Page.objects.create(
@ -2082,7 +2120,9 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
parent=other_root_page,
)
cell.custom_schema['cells'][0]['url_template'] = ''
cell.custom_schema['cells'][0]['page'] = page1.pk
cell.custom_schema['cells'][0]['template'] = 'Foo'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a').attr['href'] == '/root/card/11/'
@ -2092,6 +2132,17 @@ def test_card_cell_card_mode_render_custom_schema_link_entry(mock_send, context,
result = cell.render(context)
assert PyQuery(result).find('.value a').attr['href'] == '/other-root/card-bis/11/'
# empty label or empty url: no link in output
cell.custom_schema['cells'][0]['page'] = 0
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a') == []
cell.custom_schema['cells'][0]['page'] = page1.pk
cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
cell.save()
result = cell.render(context)
assert PyQuery(result).find('.value a') == []
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
def test_card_cell_card_mode_render_all_cards(mock_send, nocache, app):