Projet

Général

Profil

0005-wcs-configure-card-field-display-in-case-of-empty-va.patch

Lauréline Guérin, 10 janvier 2022 14:26

Télécharger (17,9 ko)

Voir les différences:

Subject: [PATCH 5/5] wcs: configure card field display in case of empty value
 (#58802)

 combo/apps/wcs/models.py                      |  17 ++-
 .../combo/wcs/card-field-as-text.html         |   6 +
 .../combo/wcs/card-field-as-title.html        |  13 ++
 combo/apps/wcs/templates/combo/wcs/card.html  |  23 ++-
 .../wcs/manager/card-infos-cell-form.html     |  16 +++
 combo/manager/static/js/combo.manager.js      |  32 ++++-
 tests/test_wcs.py                             | 135 +++++++++++++++++-
 7 files changed, 212 insertions(+), 30 deletions(-)
 create mode 100644 combo/apps/wcs/templates/combo/wcs/card-field-as-text.html
 create mode 100644 combo/apps/wcs/templates/combo/wcs/card-field-as-title.html
combo/apps/wcs/models.py
1087 1087
            if cell.get('varname') == '@custom@':
1088 1088
                if cell.get('display_mode') == 'value':
1089 1089
                    cell['display_mode'] = 'text'
1090
            elif not cell.get('field_content'):
1091
                if cell.get('display_mode') == 'title':
1092
                    cell['field_content'] = 'value'
1093
                else:
1094
                    cell['field_content'] = cell.get('display_mode') or 'value'
1095
                if cell.get('display_mode') in ['label', 'value', 'label-and-value']:
1096
                    cell['display_mode'] = 'text'
1090
            else:
1091
                if not cell.get('field_content'):
1092
                    if cell.get('display_mode') == 'title':
1093
                        cell['field_content'] = 'value'
1094
                    else:
1095
                        cell['field_content'] = cell.get('display_mode') or 'value'
1096
                    if cell.get('display_mode') in ['label', 'value', 'label-and-value']:
1097
                        cell['display_mode'] = 'text'
1098
                if not cell.get('empty_value'):
1099
                    cell['empty_value'] = '@empty@'
1097 1100

  
1098 1101
        return custom_schema
1099 1102

  
combo/apps/wcs/templates/combo/wcs/card-field-as-text.html
1
{% if item.field_content == "label" or item.field_content == "label-and-value" %}
2
  <p class="label">{{ field.label }}</p>
3
{% endif %}
4
{% if item.field_content == "value" or item.field_content == "label-and-value" %}
5
  {% include "combo/wcs/card-field-value.html" %}
6
{% endif %}
combo/apps/wcs/templates/combo/wcs/card-field-as-title.html
1
{% if item.field_content == "label" %}
2
  {% if value or item.empty_value != '@skip@' %}
3
    <{{ title_tag }}>{{ field.label }}</{{ title_tag }}>
4
  {% endif %}
5
{% elif item.field_content == "value" %}
6
  {% if value or item.empty_value == '@empty@' %}
7
    <{{ title_tag }}>{% include "combo/wcs/card-field-value.html" with mode="inline" %}</{{ title_tag }}>
8
  {% elif item.empty_value != '@skip@' and item.empty_value != '@empty@' %}
9
    {% with item.empty_value as value %}
10
      <{{ title_tag }}>{% include "combo/wcs/card-field-value.html" with mode="inline" %}</{{ title_tag }}>
11
    {% endwith %}
12
  {% endif %}
13
{% endif %}
combo/apps/wcs/templates/combo/wcs/card.html
32 32
          {% with fields_by_varnames|get:item.varname as field %}
33 33
          {% with card.fields|get:item.varname as value %}
34 34
            {% if item.display_mode == "title" %}
35
              {% if item.field_content == "label" %}
36
              <h3>{{ field.label }}</h3>
37
              {% elif item.field_content == "value" %}
38
              <h3>{% include "combo/wcs/card-field-value.html" with mode="inline" %}</h3>
39
              {% endif %}
35
              {% include "combo/wcs/card-field-as-title.html" with title_tag="h3" %}
40 36
            {% elif item.display_mode == "subtitle" %}
41
              {% if item.field_content == "label" %}
42
              <h4>{{ field.label }}</h4>
43
              {% elif item.field_content == "value" %}
44
              <h4>{% include "combo/wcs/card-field-value.html" with mode="inline" %}</h4>
45
              {% endif %}
37
              {% include "combo/wcs/card-field-as-title.html" with title_tag="h4" %}
46 38
            {% elif item.display_mode == "text" %}
47
              {% if item.field_content == "label" or item.field_content == "label-and-value" %}
48
                <p class="label">{{ field.label }}</p>
49
              {% endif %}
50
              {% if item.field_content == "value" or item.field_content == "label-and-value" %}
51
                {% include "combo/wcs/card-field-value.html" %}
39
              {% if value or item.empty_value == '@empty@' %}
40
                {% include "combo/wcs/card-field-as-text.html" %}
41
              {% elif item.empty_value != '@skip@' and item.empty_value != '@empty@' %}
42
                {% with item.empty_value as value %}
43
                  {% include "combo/wcs/card-field-as-text.html" %}
44
                {% endwith %}
52 45
              {% endif %}
53 46
            {% endif %}
54 47
          {% endwith %}
combo/apps/wcs/templates/combo/wcs/manager/card-infos-cell-form.html
73 73
        </select>
74 74
      </label>
75 75
    </p>
76
    <p>
77
      <label>
78
        {% trans "Empty value display mode" %}
79
        <select name="field_empty_display_mode" data-dynamic-display-child-of="entry_type" data-dynamic-display-value="@field@">
80
          <option value="@empty@">{% trans "Display as empty" %}</option>
81
          <option value="@skip@">{% trans "Hide" %}</option>
82
          <option value="@custom@">{% trans "Display a custom text" %}</option>
83
        </select>
84
      </label>
85
    </p>
86
    <p>
87
      <label>
88
        {% trans "Empty value custom text" %}
89
        <input name="field_empty_text" data-dynamic-display-child-of="entry_type" data-dynamic-display-value="@field@" />
90
      </label>
91
    </p>
76 92
    <p>
77 93
      <label>
78 94
        {% trans "Value template" %}
combo/manager/static/js/combo.manager.js
493 493
    this.grid_cell_form.field_varname= this.grid_cell_form[1];
494 494
    this.grid_cell_form.field_content = this.grid_cell_form[2];
495 495
    this.grid_cell_form.field_display_mode = this.grid_cell_form[3];
496
    this.grid_cell_form.custom_template = this.grid_cell_form[4];
497
    this.grid_cell_form.custom_display_mode = this.grid_cell_form[5];
498
    this.grid_cell_form.size = this.grid_cell_form[6];
496
    this.grid_cell_form.field_empty_display_mode = this.grid_cell_form[4];
497
    this.grid_cell_form.field_empty_text = this.grid_cell_form[5];
498
    this.grid_cell_form.custom_template = this.grid_cell_form[6];
499
    this.grid_cell_form.custom_display_mode = this.grid_cell_form[7];
500
    this.grid_cell_form.size = this.grid_cell_form[8];
499 501
    const varname_select = this.grid_cell_form.field_varname;
500 502
    this.cardSchema.fields.forEach(function(el, id) {
501 503
      if (el.varname) {
......
514 516
        $(_self.grid_cell_form.field_display_mode).addClass('field-hidden').parent().parent().show();
515 517
      }
516 518
    });
519
    const field_empty_display_mode_select = this.grid_cell_form.field_empty_display_mode;
520
    $(field_empty_display_mode_select).on('change', function() {
521
      if ($(this).hasClass('field-hidden') || $(this).val() != '@custom@') {
522
        $(_self.grid_cell_form.field_empty_text).removeClass('field-hidden').parent().parent().hide();
523
      } else {
524
        $(_self.grid_cell_form.field_empty_text).addClass('field-hidden').parent().parent().show();
525
      }
526
    });
517 527
  },
518 528
  grid_cell__form_dialog: function(button_label, callback){
519 529
    const _self = this;
......
523 533
      open: function( event, ui ) {
524 534
        $(document).trigger('combo:dialog-loaded');
525 535
        $(_self.grid_cell_form.field_content).change();
536
        $(_self.grid_cell_form.field_empty_display_mode).change();
526 537
      },
527 538
      buttons: [
528 539
        {
......
613 624
      this.grid_cell_form.field_varname.value = '';
614 625
      this.grid_cell_form.field_content.value = '';
615 626
      this.grid_cell_form.field_display_mode.value = '';
627
      this.grid_cell_form.field_empty_display_mode.value = '';
628
      this.grid_cell_form.field_empty_display_mode_select.value = '';
616 629
      this.grid_cell_form.custom_template.value = grid_cell.dataset.template || '';
617 630
      this.grid_cell_form.custom_display_mode.value = grid_cell.dataset.display_mode;
618 631
    } else {
......
624 637
      } else {
625 638
        this.grid_cell_form.field_display_mode.value = grid_cell.dataset.display_mode;
626 639
      }
640
      if (grid_cell.dataset.empty_value != '@skip@' && grid_cell.dataset.empty_value != '@empty@') {
641
        this.grid_cell_form.field_empty_display_mode.value = '@custom@';
642
        this.grid_cell_form.field_empty_text.value = grid_cell.dataset.empty_value || '';
643
      } else {
644
        this.grid_cell_form.field_empty_display_mode.value = grid_cell.dataset.empty_value;
645
        this.grid_cell_form.field_empty_text.value = '';
646
      }
627 647
      this.grid_cell_form.custom_template.value = '';
628 648
      this.grid_cell_form.custom_display_mode.value = '';
629 649
    }
......
640 660
    if (form_datas.entry_type == '@custom@') {
641 661
      schema_cell.varname = '@custom@';
642 662
      delete schema_cell.field_content;
663
      delete schema_cell.empty_value;
643 664
      schema_cell.display_mode = form_datas.custom_display_mode;
644 665
      schema_cell.template = form_datas.custom_template;
645 666
    } else {
646 667
      schema_cell.varname = form_datas.field_varname;
647 668
      schema_cell.field_content = form_datas.field_content;
648 669
      schema_cell.display_mode = form_datas.field_display_mode;
670
      if (form_datas.field_empty_display_mode == '@custom@') {
671
        schema_cell.empty_value = form_datas.field_empty_text;
672
      } else {
673
        schema_cell.empty_value = form_datas.field_empty_display_mode;
674
      }
649 675
      delete schema_cell.template;
650 676
    }
651 677
    schema_cell.cell_size = form_datas.cell_size;
tests/test_wcs.py
193 193
            {'label': 'Field F', 'varname': 'fieldf', 'type': 'text', 'pre': True},
194 194
            {'label': 'Field G', 'varname': 'fieldg', 'type': 'email'},
195 195
            {'label': 'Field H', 'varname': 'fieldh', 'type': 'string'},
196
            {'label': 'Empty', 'varname': 'empty', 'type': 'string'},
196 197
            {'label': 'Related', 'varname': 'related', 'type': 'item'},
197 198
            {'label': 'Page', 'type': 'page'},
198 199
            {'label': 'Comment', 'type': 'comment'},
......
1712 1713
    assert 'custom_schema' in form.fields
1713 1714
    assert form.initial['customize_display'] is True
1714 1715
    assert form.initial['custom_schema'] == {
1715
        'cells': [{'varname': 'foo', 'field_content': 'value', 'display_mode': 'text'}]
1716
        'cells': [
1717
            {'varname': 'foo', 'field_content': 'value', 'display_mode': 'text', 'empty_value': '@empty@'}
1718
        ]
1716 1719
    }
1717 1720

  
1718 1721
    app = login(app)
......
1732 1735
    }
1733 1736
    assert cell.get_custom_schema() == {
1734 1737
        'cells': [
1735
            {'varname': 'some-field', 'field_content': 'label', 'display_mode': 'text', 'cell_size': 'foobar'}
1738
            {
1739
                'varname': 'some-field',
1740
                'field_content': 'label',
1741
                'display_mode': 'text',
1742
                'empty_value': '@empty@',
1743
                'cell_size': 'foobar',
1744
            }
1736 1745
        ]
1737 1746
    }
1738 1747
    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'value'}]}
1739 1748
    assert cell.get_custom_schema() == {
1740
        'cells': [{'varname': 'some-field', 'field_content': 'value', 'display_mode': 'text'}]
1749
        'cells': [
1750
            {
1751
                'varname': 'some-field',
1752
                'field_content': 'value',
1753
                'display_mode': 'text',
1754
                'empty_value': '@empty@',
1755
            }
1756
        ]
1741 1757
    }
1742 1758
    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'label-and-value'}]}
1743 1759
    assert cell.get_custom_schema() == {
1744
        'cells': [{'varname': 'some-field', 'field_content': 'label-and-value', 'display_mode': 'text'}]
1760
        'cells': [
1761
            {
1762
                'varname': 'some-field',
1763
                'field_content': 'label-and-value',
1764
                'display_mode': 'text',
1765
                'empty_value': '@empty@',
1766
            }
1767
        ]
1745 1768
    }
1746 1769
    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'title'}]}
1747 1770
    assert cell.get_custom_schema() == {
1748
        'cells': [{'varname': 'some-field', 'field_content': 'value', 'display_mode': 'title'}]
1771
        'cells': [
1772
            {
1773
                'varname': 'some-field',
1774
                'field_content': 'value',
1775
                'display_mode': 'title',
1776
                'empty_value': '@empty@',
1777
            }
1778
        ]
1749 1779
    }
1750 1780

  
1751 1781
    cell.custom_schema = {
......
2217 2247
    assert PyQuery(result).find('span.value a').attr['href'] == 'https://www.example.net/'
2218 2248

  
2219 2249

  
2250
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
2251
def test_card_cell_render_custom_schema_card_empty_field(mock_send, context):
2252
    page = Page.objects.create(title='xxx', template_name='standard')
2253
    cell = WcsCardInfosCell.objects.create(
2254
        page=page,
2255
        placeholder='content',
2256
        order=0,
2257
        carddef_reference='default:card_model_1',
2258
        custom_schema={
2259
            'cells': [
2260
                {
2261
                    'varname': 'empty',
2262
                    'field_content': 'label-and-value',
2263
                    'display_mode': 'text',
2264
                    'empty_value': '@skip@',
2265
                }
2266
            ]
2267
        },
2268
    )
2269

  
2270
    context['card_model_1_id'] = 11
2271
    request = RequestFactory().get('/')
2272
    cell.modify_global_context(context, request)
2273
    cell.repeat_index = 0
2274
    context['synchronous'] = True  # to get fresh content
2275

  
2276
    result = cell.render(context)
2277
    assert PyQuery(result).find('p.label') == []
2278
    assert PyQuery(result).find('span.value') == []
2279

  
2280
    cell.custom_schema['cells'][0] = {
2281
        'varname': 'empty',
2282
        'field_content': 'label-and-value',
2283
        'display_mode': 'text',
2284
        'empty_value': '@empty@',
2285
    }
2286
    cell.save()
2287
    result = cell.render(context)
2288
    assert PyQuery(result).find('p.label').text() == 'Empty'
2289
    assert PyQuery(result).find('span.value').text() == ''
2290

  
2291
    cell.custom_schema['cells'][0] = {
2292
        'varname': 'empty',
2293
        'field_content': 'label-and-value',
2294
        'display_mode': 'text',
2295
        'empty_value': 'Custom text',
2296
    }
2297
    cell.save()
2298
    result = cell.render(context)
2299
    assert PyQuery(result).find('p.label').text() == 'Empty'
2300
    assert PyQuery(result).find('span.value').text() == 'Custom text'
2301

  
2302
    for field_content in ['label', 'value']:
2303
        for display_mode in ['text', 'title', 'subtitle']:
2304
            if display_mode == 'title':
2305
                html_tag = 'h3'
2306
            elif display_mode == 'subtitle':
2307
                html_tag = 'h4'
2308
            elif display_mode == 'text' and field_content == 'label':
2309
                html_tag = 'p.label'
2310
            elif display_mode == 'text' and field_content == 'value':
2311
                html_tag = 'span.value'
2312
            cell.custom_schema['cells'][0] = {
2313
                'varname': 'empty',
2314
                'field_content': field_content,
2315
                'display_mode': display_mode,
2316
                'empty_value': '@skip@',
2317
            }
2318
            cell.save()
2319
            result = cell.render(context)
2320
            assert PyQuery(result).find(html_tag) == []
2321

  
2322
            cell.custom_schema['cells'][0] = {
2323
                'varname': 'empty',
2324
                'field_content': field_content,
2325
                'display_mode': display_mode,
2326
                'empty_value': '@empty@',
2327
            }
2328
            cell.save()
2329
            result = cell.render(context)
2330
            assert PyQuery(result).find(html_tag).text() == ('Empty' if field_content == 'label' else '')
2331

  
2332
            cell.custom_schema['cells'][0] = {
2333
                'varname': 'empty',
2334
                'field_content': field_content,
2335
                'display_mode': display_mode,
2336
                'empty_value': 'Custom text',
2337
            }
2338
            cell.save()
2339
            result = cell.render(context)
2340
            assert PyQuery(result).find(html_tag).text() == (
2341
                'Empty' if field_content == 'label' else 'Custom text'
2342
            )
2343

  
2344

  
2220 2345
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
2221 2346
def test_card_cell_render_custom_schema_custom_entry(mock_send, context, app):
2222 2347
    page = Page.objects.create(title='xxx', template_name='standard')
2223
-