Projet

Général

Profil

0001-wcs-do-not-show-A-anchor-if-URL-and-label-are-empty-.patch

Lauréline Guérin, 13 septembre 2022 11:49

Télécharger (7,66 ko)

Voir les différences:

Subject: [PATCH] wcs: do not show A anchor if URL and label are empty (#62137)

 combo/apps/wcs/templates/combo/wcs/card.html  |  4 +-
 .../wcs/templates/combo/wcs/cards-field.html  |  6 ++-
 combo/apps/wcs/templates/combo/wcs/cards.html |  8 ++-
 tests/wcs/test_card.py                        | 52 +++++++++++++++++++
 4 files changed, 67 insertions(+), 3 deletions(-)
combo/apps/wcs/templates/combo/wcs/card.html
37 37
          <div class="{{ item.cell_size|default:"" }}">
38 38
            {% with item.page|default:item.url_template as url_key %}
39 39
            {% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
40
            <div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
40
            {% if link_label and link_url %}
41
                <div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
42
            {% endif %}
41 43
            {% endwith %}
42 44
            {% endwith %}
43 45
          </div>
combo/apps/wcs/templates/combo/wcs/cards-field.html
9 9
  {% if item.template and item.page|default:item.url_template %}
10 10
    {% with item.page|default:item.url_template as url_key %}
11 11
    {% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:url_key|force_escape as link_url %}
12
    {% 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 %}
12
    {% if not ul_display %}<td>{% endif %}
13
    {% if link_label and link_url %}
14
    <a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a>
15
    {% endif %}
16
    {% if not ul_display %}</td>{% endif %}
13 17
    {% endwith %}
14 18
    {% endwith %}
15 19
  {% endif %}
combo/apps/wcs/templates/combo/wcs/cards.html
15 15
  {% if custom_schema.grid_headers %}
16 16
  <thead>
17 17
    {% for item in custom_schema.cells %}
18
    {% if item.varname == "@custom@" or item.varname == "@link@" %}
18
    {% if item.varname == "@custom@" %}
19
      {% if item.template %}
19 20
      <th>{{ item.header|default:"" }}</th>
21
      {% endif %}
22
    {% elif item.varname == "@link@" %}
23
      {% if item.template and item.page|default:item.url_template %}
24
      <th>{{ item.header|default:"" }}</th>
25
      {% endif %}
20 26
    {% else %}
21 27
      {% if item.varname %}
22 28
      {% with fields_by_varnames|get:item.varname as field %}
tests/wcs/test_card.py
1043 1043
    cell.save()
1044 1044
    test('<i>a</i> - Foo Bar', '/foo/bar/42/', 'pk-button')
1045 1045

  
1046
    # empty label or empty url: no link in output
1047
    cell.custom_schema['cells'][0]['url_template'] = '{{ None|default:"" }}'
1048
    cell.save()
1049
    result = cell.render(context)
1050
    assert PyQuery(result).find('ul li a') == []
1051
    assert PyQuery(result).find('table tr td a') == []
1052
    cell.custom_schema['cells'][0]['url_template'] = 'foo/bar'
1053
    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
1054
    cell.save()
1055
    result = cell.render(context)
1056
    assert PyQuery(result).find('ul li a') == []
1057
    assert PyQuery(result).find('table tr td a') == []
1058

  
1046 1059
    # check with page link
1047 1060
    root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
1048 1061
    page1 = Page.objects.create(
......
1061 1074
        parent=other_root_page,
1062 1075
    )
1063 1076

  
1077
    cell.custom_schema['cells'][0]['url_template'] = ''
1064 1078
    cell.custom_schema['cells'][0]['page'] = page1.pk
1079
    cell.custom_schema['cells'][0]['template'] = '{{ card.fields.fielda }} - {{ card.fields.related }}'
1065 1080
    cell.save()
1066 1081
    test('<i>a</i> - Foo Bar', '/root/card/11/', 'pk-button')
1067 1082

  
......
1069 1084
    cell.save()
1070 1085
    test('<i>a</i> - Foo Bar', '/other-root/card-bis/11/', 'pk-button')
1071 1086

  
1087
    # empty label or empty url: no link in output
1088
    cell.custom_schema['cells'][0]['page'] = 0
1089
    cell.save()
1090
    result = cell.render(context)
1091
    assert PyQuery(result).find('ul li a') == []
1092
    assert PyQuery(result).find('table tr td a') == []
1093
    cell.custom_schema['cells'][0]['page'] = page1.pk
1094
    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
1095
    cell.save()
1096
    result = cell.render(context)
1097
    assert PyQuery(result).find('ul li a') == []
1098
    assert PyQuery(result).find('table tr td a') == []
1099

  
1072 1100

  
1073 1101
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
1074 1102
@pytest.mark.parametrize('with_headers', [True, False])
......
2046 2074
        in cell_resp
2047 2075
    )
2048 2076

  
2077
    # empty label or empty url: no link in output
2078
    cell.custom_schema['cells'][0]['url_template'] = '{{ None|default:"" }}'
2079
    cell.save()
2080
    result = cell.render(context)
2081
    assert PyQuery(result).find('.value a') == []
2082
    cell.custom_schema['cells'][0]['url_template'] = 'foo/bar'
2083
    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
2084
    cell.save()
2085
    result = cell.render(context)
2086
    assert PyQuery(result).find('.value a') == []
2087

  
2049 2088
    # check with page link
2050 2089
    root_page = Page.objects.create(title='Root', slug='root', template_name='standard')
2051 2090
    page1 = Page.objects.create(
......
2064 2103
        parent=other_root_page,
2065 2104
    )
2066 2105

  
2106
    cell.custom_schema['cells'][0]['url_template'] = ''
2067 2107
    cell.custom_schema['cells'][0]['page'] = page1.pk
2108
    cell.custom_schema['cells'][0]['template'] = 'Foo'
2068 2109
    cell.save()
2069 2110
    result = cell.render(context)
2070 2111
    assert PyQuery(result).find('.value a').attr['href'] == '/root/card/11/'
......
2074 2115
    result = cell.render(context)
2075 2116
    assert PyQuery(result).find('.value a').attr['href'] == '/other-root/card-bis/11/'
2076 2117

  
2118
    # empty label or empty url: no link in output
2119
    cell.custom_schema['cells'][0]['page'] = 0
2120
    cell.save()
2121
    result = cell.render(context)
2122
    assert PyQuery(result).find('.value a') == []
2123
    cell.custom_schema['cells'][0]['page'] = page1.pk
2124
    cell.custom_schema['cells'][0]['template'] = '{{ None|default:"" }}'
2125
    cell.save()
2126
    result = cell.render(context)
2127
    assert PyQuery(result).find('.value a') == []
2128

  
2077 2129

  
2078 2130
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
2079 2131
def test_card_cell_card_mode_render_all_cards(mock_send, nocache, app):
2080
-