Projet

Général

Profil

0001-wcs-fix-card-cell-global-context-with-misordered-cel.patch

Lauréline Guérin, 11 juillet 2022 12:16

Télécharger (4,09 ko)

Voir les différences:

Subject: [PATCH] wcs: fix card cell global context with misordered cells
 (#67214)

 combo/apps/wcs/models.py |  2 +-
 combo/public/views.py    | 12 ++++++++++--
 tests/test_wcs.py        | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 3 deletions(-)
combo/apps/wcs/models.py
1287 1287
        if repeat_index is not None:
1288 1288
            try:
1289 1289
                return context.get(self.global_context_key)[repeat_index]
1290
            except IndexError:
1290
            except (IndexError, TypeError):
1291 1291
                return None
1292 1292

  
1293 1293
    def get_extra_manager_context(self):
combo/public/views.py
189 189
            )
190 190
        other_cells = [x for x in other_cells if x.is_visible(request)]
191 191
        other_cells.sort(key=lambda x: x.order)
192
        for other_cell in other_cells:
192
        # first, cells with slugs: other cells may need context from cells with slugs
193
        # (for example, cards cells with related ids)
194
        other_cells_with_slug = [c for c in other_cells if c.slug]
195
        other_cells_without_slug = [c for c in other_cells if not c.slug]
196
        for other_cell in other_cells_with_slug + other_cells_without_slug:
193 197
            if other_cell.get_reference() != cell.get_reference():
194 198
                other_cell.modify_global_context(context, request)
195 199
            elif cell.modify_global_context:
......
587 591
    if getattr(settings, 'COMBO_TEST_ALWAYS_RENDER_CELLS_SYNCHRONOUSLY', False):
588 592
        ctx['synchronous'] = True
589 593

  
590
    for cell in cells:
594
    # first, cells with slugs: other cells may need context from cells with slugs
595
    # (for example, cards cells with related ids)
596
    cells_with_slug = [c for c in cells if c.slug]
597
    cells_without_slug = [c for c in cells if not c.slug]
598
    for cell in cells_with_slug + cells_without_slug:
591 599
        if cell.modify_global_context:
592 600
            cell.modify_global_context(ctx, request)
593 601

  
tests/test_wcs.py
3446 3446
        ]
3447 3447
    )
3448 3448

  
3449
    # change cell ordering - cell with slug is after cell with related
3450
    cell.order = 42
3451
    cell.save()
3452
    # no error, but it does not work as expected: both cells has slug.
3453
    app.get(page.get_online_url(), status=200)
3454
    # remove slug of second cell
3455
    cell2.slug = ''
3456
    cell2.save()
3457
    urls = [
3458
        # get first cell data
3459
        '/api/cards/card_a/1/',
3460
        # get card_c schema
3461
        '/api/cards/card_c/@schema',
3462
        # follow cardc relation
3463
        '/api/cards/card_c/6/',
3464
        # and follow cardb relation
3465
        '/api/cards/card_b/7/',
3466
    ]
3467
    resp = app.get(page.get_online_url())
3468
    assert len(resp.context['cells']) == 2
3469
    assert resp.context['cells'][0].pk == cell2.pk
3470
    assert resp.context['cells'][0].repeat_index == 0
3471
    assert resp.context['cells'][1].pk == cell.pk
3472
    extra_ctx = re.findall(r'data-extra-context="(.*)"', resp.text)
3473
    mock_send.reset_mock()
3474
    cell_resp = app.get(cell2_url + '?ctx=' + extra_ctx[0])
3475
    assert cell_resp.context['repeat_index'] == 0
3476
    assert len(mock_send.call_args_list) == len(urls)
3477
    for j, url in enumerate(urls):
3478
        assert url in mock_send.call_args_list[j][0][0].url
3479

  
3480
    cell.order = 0  # reset
3481
    cell.save()
3449 3482
    # direct and multiple relation (items)
3483
    cell2.slug = 'slugb'  # reset
3450 3484
    cell2.related_card_path = 'sluga/cardsb'
3451 3485
    cell2.save()
3452 3486
    multiple(
3453
-