Projet

Général

Profil

0005-wcs-add-link-entry-in-card-custom-schema-60370.patch

Lauréline Guérin, 07 janvier 2022 11:14

Télécharger (13,6 ko)

Voir les différences:

Subject: [PATCH 5/5] wcs: add link entry in card custom schema (#60370)

 combo/apps/wcs/models.py                      | 35 ++++++++---
 combo/apps/wcs/templates/combo/wcs/card.html  |  4 ++
 .../wcs/manager/card-infos-cell-form.html     | 22 +++++++
 combo/manager/static/js/combo.manager.js      | 42 +++++++++++++-
 tests/test_wcs.py                             | 58 +++++++++++++++++++
 5 files changed, 151 insertions(+), 10 deletions(-)
combo/apps/wcs/models.py
1058 1058
            if self.title_type == 'auto' or self.title_type == 'manual' and not extra_context['title']:
1059 1059
                extra_context['title'] = '%s - %s' % (self.cached_title, extra_context['card'].get('text'))
1060 1060

  
1061
            def render_template(item, template_key, template_context, target_key, target_context):
1062
                if not item.get(template_key):
1063
                    return
1064
                try:
1065
                    target_context['card'][target_key][item[template_key]] = Template(
1066
                        item[template_key]
1067
                    ).render(template_context)
1068
                except (VariableDoesNotExist, TemplateSyntaxError):
1069
                    return
1070

  
1061 1071
            extra_context['card']['custom_fields'] = {}
1072
            extra_context['card']['urls'] = {}
1062 1073
            if self.custom_schema:
1063 1074
                for item in self.custom_schema.get('cells') or []:
1064
                    if item.get('varname') != '@custom@':
1075
                    if item.get('varname') not in ['@custom@', '@link@']:
1065 1076
                        continue
1066 1077
                    if not item.get('template'):
1067 1078
                        continue
1068
                    try:
1069
                        extra_context['card']['custom_fields'][item['template']] = Template(
1070
                            item['template']
1071
                        ).render(custom_context)
1072
                    except (VariableDoesNotExist, TemplateSyntaxError):
1073
                        pass
1079
                    render_template(
1080
                        item=item,
1081
                        template_key='template',
1082
                        template_context=custom_context,
1083
                        target_key='custom_fields',
1084
                        target_context=extra_context,
1085
                    )
1086

  
1087
                    if item['varname'] == '@link@':
1088
                        render_template(
1089
                            item=item,
1090
                            template_key='url_template',
1091
                            template_context=custom_context,
1092
                            target_key='urls',
1093
                            target_context=extra_context,
1094
                        )
1074 1095

  
1075 1096
        return extra_context
1076 1097

  
combo/apps/wcs/templates/combo/wcs/card.html
28 28
            <div class="value">{{ value }}</div>
29 29
          {% endif %}
30 30
          {% endwith %}
31
        {% elif item.varname == "@link@" and item.url_template and item.template %}
32
          {% with card.custom_fields|get:item.template|force_escape as link_label and card.urls|get:item.url_template|force_escape as link_url %}
33
          <div class="value"><a href="{{ link_url }}"{% if item.display_mode == 'button' %} class="pk-button"{% endif %}>{{ link_label }}</a></div>
34
          {% endwith %}
31 35
        {% else %}
32 36
          {% with fields_by_varnames|get:item.varname as field %}
33 37
          {% with card.fields|get:item.varname as value %}
combo/apps/wcs/templates/combo/wcs/manager/card-infos-cell-form.html
44 44
        <select name="entry_type" data-dynamic-display-parent="true">
45 45
          <option value="@field@">{% trans "Card field" %}</option>
46 46
          <option value="@custom@">{% trans "Custom" %}</option>
47
          <option value="@link@">{% trans "Link" %}</option>
47 48
        </select>
48 49
      </label>
49 50
    </p>
......
90 91
        </select>
91 92
      </label>
92 93
    </p>
94
    <p>
95
      <label>
96
        {% trans "Label template" %}
97
        <textarea name="link_label_template" data-dynamic-display-child-of="entry_type" data-dynamic-display-value="@link@" style="resize: vertical;"></textarea>
98
      </label>
99
    </p>
100
    <p>
101
      <label>
102
        {% trans "URL template" %}
103
        <textarea name="link_url_template" data-dynamic-display-child-of="entry_type" data-dynamic-display-value="@link@" style="resize: vertical;"></textarea>
104
      </label>
105
    </p>
106
    <p>
107
      <label>
108
        {% trans "Display mode" %}
109
        <select name="link_display_mode" data-dynamic-display-child-of="entry_type" data-dynamic-display-value="@link@">
110
          <option value="link">{% trans "Link" %}</option>
111
          <option value="button">{% trans "Button" %}</option>
112
        </select>
113
      </label>
114
    </p>
93 115
    <p>
94 116
      <label>
95 117
        {% trans "Size" %}
combo/manager/static/js/combo.manager.js
495 495
    this.grid_cell_form.field_display_mode = this.grid_cell_form[3];
496 496
    this.grid_cell_form.custom_template = this.grid_cell_form[4];
497 497
    this.grid_cell_form.custom_display_mode = this.grid_cell_form[5];
498
    this.grid_cell_form.size = this.grid_cell_form[6];
498
    this.grid_cell_form.link_label_template = this.grid_cell_form[6];
499
    this.grid_cell_form.link_url_template = this.grid_cell_form[7];
500
    this.grid_cell_form.link_display_mode = this.grid_cell_form[8];
501
    this.grid_cell_form.size = this.grid_cell_form[9];
499 502
    const varname_select = this.grid_cell_form.field_varname;
500 503
    this.cardSchema.fields.forEach(function(el, id) {
501 504
      if (el.varname) {
......
567 570
    // set cell text
568 571
    let schema_field = _self.field_with_varname(schema_cell.varname);
569 572
    let cell_text = "";
570
    if (schema_field || schema_cell.varname == '@custom@') {
571
      const cell_content = schema_cell.varname == '@custom@' ? (schema_cell.template || '') + ' (' + gettext('Custom') + ')' : schema_field.label;
573
    if (schema_field || schema_cell.varname == '@custom@' || schema_cell.varname == '@link@') {
574
      let cell_content = "";
575
      if (schema_cell.varname == '@custom@') {
576
        cell_content = (schema_cell.template || '') + ' (' + gettext('Custom') + ')';
577
      } else if (schema_cell.varname == '@link@') {
578
        cell_content = (schema_cell.template || '') + ': ' + (schema_cell.url_template || '') + ' (' + gettext('Link') + ')';
579
      } else {
580
        cell_content = schema_field.label;
581
      }
572 582
      cell_text += $('<span/>').addClass(schema_cell.display_mode).text(cell_content).html();
573 583
      cell_text += '<span class="cell-meta">';
574 584
      let cell_display_mode_label = '';
575 585
      if (schema_cell.varname == '@custom@') {
576 586
        cell_display_mode_label = $(this.grid_cell_form).find('select[name="custom_display_mode"] option[value="' + schema_cell.display_mode + '"]').text();
587
      } else if (schema_cell.varname == '@link@') {
588
        cell_display_mode_label = $(this.grid_cell_form).find('select[name="link_display_mode"] option[value="' + schema_cell.display_mode + '"]').text();
577 589
      } else {
578 590
        cell_display_mode_label = $(this.grid_cell_form).find('select[name="field_content"] option[value="' + schema_cell.field_content + '"]').text();
579 591
        if (schema_cell.field_content != 'label-and-value') {
......
615 627
      this.grid_cell_form.field_display_mode.value = '';
616 628
      this.grid_cell_form.custom_template.value = grid_cell.dataset.template || '';
617 629
      this.grid_cell_form.custom_display_mode.value = grid_cell.dataset.display_mode;
630
      this.grid_cell_form.link_label_template.value = '';
631
      this.grid_cell_form.link_url_template.value = '';
632
      this.grid_cell_form.link_display_mode.value = '';
633
    } else if (grid_cell.dataset.varname == '@link@') {
634
      this.grid_cell_form.entry_type.value = '@link@';
635
      this.grid_cell_form.field_varname.value = '';
636
      this.grid_cell_form.field_content.value = '';
637
      this.grid_cell_form.field_display_mode.value = '';
638
      this.grid_cell_form.custom_template.value = '';
639
      this.grid_cell_form.custom_display_mode.value = '';
640
      this.grid_cell_form.link_label_template.value = grid_cell.dataset.template || '';
641
      this.grid_cell_form.link_url_template.value = grid_cell.dataset.url_template || '';
642
      this.grid_cell_form.link_display_mode.value = grid_cell.dataset.display_mode;
618 643
    } else {
619 644
      this.grid_cell_form.entry_type.value = '@field@';
620 645
      this.grid_cell_form.field_varname.value = grid_cell.dataset.varname;
......
622 647
      this.grid_cell_form.field_display_mode.value = grid_cell.dataset.display_mode;
623 648
      this.grid_cell_form.custom_template.value = '';
624 649
      this.grid_cell_form.custom_display_mode.value = '';
650
      this.grid_cell_form.link_label_template.value = '';
651
      this.grid_cell_form.link_url_template.value = '';
652
      this.grid_cell_form.link_display_mode.value = '';
625 653
    }
626 654
    this.grid_cell_form.size.value = grid_cell.dataset.cell_size;
627 655
    const cell_id = $(grid_cell).index();
......
638 666
      delete schema_cell.field_content;
639 667
      schema_cell.display_mode = form_datas.custom_display_mode;
640 668
      schema_cell.template = form_datas.custom_template;
669
      delete schema_cell.url_template;
670
    } else if (form_datas.entry_type == '@link@') {
671
      schema_cell.varname = '@link@';
672
      delete schema_cell.field_content;
673
      schema_cell.display_mode = form_datas.link_display_mode;
674
      schema_cell.template = form_datas.link_label_template;
675
      schema_cell.url_template = form_datas.link_url_template;
641 676
    } else {
642 677
      schema_cell.varname = form_datas.field_varname;
643 678
      schema_cell.field_content = form_datas.field_content;
644 679
      schema_cell.display_mode = form_datas.field_display_mode;
645 680
      delete schema_cell.template;
681
      delete schema_cell.url_template;
646 682
    }
647 683
    schema_cell.cell_size = form_datas.cell_size;
648 684
    return schema_cell
tests/test_wcs.py
2299 2299
        assert '<p class="label">Foo bar baz X%sY</p>' % i in cell_resp
2300 2300

  
2301 2301

  
2302
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
2303
def test_card_cell_render_custom_schema_link_entry(mock_send, context, app):
2304
    page = Page.objects.create(title='xxx', template_name='standard')
2305
    cell = WcsCardInfosCell.objects.create(
2306
        page=page,
2307
        placeholder='content',
2308
        order=0,
2309
        carddef_reference='default:card_model_1',
2310
        custom_schema={
2311
            'cells': [
2312
                {
2313
                    'varname': '@link@',
2314
                    'url_template': '/foo/bar/{{ card.fields.related_structured.id }}/',
2315
                    'template': '{{ card.fields.fielda }} - {{ card.fields.related }}',
2316
                    'display_mode': 'link',
2317
                },
2318
            ]
2319
        },
2320
    )
2321

  
2322
    context['card_model_1_id'] = 11
2323
    request = RequestFactory().get('/')
2324
    cell.modify_global_context(context, request)
2325
    cell.repeat_index = 0
2326
    context['synchronous'] = True  # to get fresh content
2327

  
2328
    result = cell.render(context)
2329
    assert PyQuery(result).find('div.value a').text() == 'a - Foo Bar'
2330
    assert PyQuery(result).find('div.value a').attr['href'] == '/foo/bar/42/'
2331
    assert PyQuery(result).find('div.value a').attr['class'] is None
2332

  
2333
    cell.custom_schema['cells'][0]['display_mode'] = 'button'
2334
    cell.save()
2335
    result = cell.render(context)
2336
    assert PyQuery(result).find('div.value a').text() == 'a - Foo Bar'
2337
    assert PyQuery(result).find('div.value a').attr['href'] == '/foo/bar/42/'
2338
    assert PyQuery(result).find('div.value a').attr['class'] == 'pk-button'
2339

  
2340
    cell.custom_schema['cells'][0][
2341
        'url_template'
2342
    ] = '{{ site_base }}/foo/bar/{{ card.fields.related_structured.id }}/'
2343
    cell.custom_schema['cells'][0]['template'] = '<b>{{ card.fields.fielda }}</b> - {{ card.fields.related }}'
2344
    cell.card_ids = '11'
2345
    cell.save()
2346
    resp = app.get(page.get_online_url())
2347
    assert len(resp.context['cells']) == 1
2348
    extra_ctx = re.findall(r'data-extra-context="(.*)"', resp.text)
2349
    cell_url = reverse(
2350
        'combo-public-ajax-page-cell',
2351
        kwargs={'page_pk': page.pk, 'cell_reference': cell.get_reference()},
2352
    )
2353
    cell_resp = app.get(cell_url + '?ctx=' + extra_ctx[0])
2354
    assert (
2355
        '<div class="value"><a href="http://testserver/foo/bar/42/" class="pk-button">&lt;b&gt;a&lt;/b&gt; - Foo Bar</a></div>'
2356
        in cell_resp
2357
    )
2358

  
2359

  
2302 2360
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
2303 2361
def test_card_cell_render_identifier(mock_send, nocache, app):
2304 2362
    page = Page.objects.create(
2305
-