Projet

Général

Profil

0005-wcs-option-to-list-all-cards-68037.patch

Lauréline Guérin, 29 août 2022 14:35

Télécharger (14 ko)

Voir les différences:

Subject: [PATCH 5/5] wcs: option to list all cards (#68037)

 combo/apps/wcs/forms.py  |  1 +
 combo/apps/wcs/models.py | 29 +++++++++++++-----
 tests/test_wcs.py        | 65 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 7 deletions(-)
combo/apps/wcs/forms.py
126 126
            if not self.instance.card_ids and not self.instance.related_card_path:
127 127
                self.initial['related_card_path'] = '--'
128 128
        self.fields['related_card_path'].choices += [
129
            ('__all__', _('All cards')),
129 130
            ('', _('Other Card Identifiers')),
130 131
        ] + self.instance.get_related_card_paths()
131 132

  
combo/apps/wcs/models.py
1019 1019
        return super().is_visible(request, **kwargs)
1020 1020

  
1021 1021
    def check_validity(self):
1022
        if self.related_card_path:
1022
        if self.get_related_card_path():
1023 1023
            relations = [r[0] for r in self.get_related_card_paths()]
1024
            if self.related_card_path not in relations:
1024
            if self.get_related_card_path() not in relations:
1025 1025
                self.mark_as_invalid('wcs_card_relation_not_found')
1026 1026
                return
1027 1027

  
......
1039 1039
    def get_repeat_template(self, context):
1040 1040
        return len(context.get(self.global_context_key) or [])
1041 1041

  
1042
    def get_related_card_path(self):
1043
        if self.related_card_path == '__all__':
1044
            return ''
1045
        return self.related_card_path
1046

  
1047
    def must_get_all(self):
1048
        if self.related_card_path == '__all__':
1049
            return True
1050
        return False
1051

  
1042 1052
    def get_cards_from_ids(self, card_ids, context, synchronous=False):
1043 1053
        # if check_user, get all cards from ids in context, with correct filter-user-uuid and without_user value
1044 1054
        # use custom view if requested
......
1051 1061
        user = self.get_concerned_user(context)
1052 1062
        if self.only_for_user and user and not user.is_anonymous and user.get_name_id():
1053 1063
            api_url += '&filter-user-uuid=%s' % user.get_name_id()
1054
        api_url += '&%s' % '&'.join(['filter-internal-id=%s' % cid for cid in card_ids])
1064
        if not self.must_get_all():
1065
            api_url += '&%s' % '&'.join(['filter-internal-id=%s' % cid for cid in card_ids])
1055 1066
        if not synchronous:
1056 1067
            synchronous = bool(context.get('synchronous'))
1057 1068
        wcs_site = get_wcs_services().get(self.wcs_site)
......
1318 1329
                parts=parts[1:],
1319 1330
            )
1320 1331

  
1321
        first_cell_slug = self.related_card_path.split('/')[0]
1332
        first_cell_slug = self.get_related_card_path().split('/', maxsplit=1)[0]
1322 1333
        try:
1323 1334
            first_cell = WcsCardInfosCell.objects.get(page=self.page_id, slug=first_cell_slug)
1324 1335
        except (WcsCardInfosCell.DoesNotExist, WcsCardInfosCell.MultipleObjectsReturned):
......
1346 1357
        if not card_data:
1347 1358
            # card data not found
1348 1359
            return []
1349
        parts = self.related_card_path.split('/')[1:]
1360
        parts = self.get_related_card_path().split('/')[1:]
1350 1361
        return follow_data(
1351 1362
            card_data=card_data,
1352 1363
            relations=first_cell.cached_json['relations'],
......
1368 1379
            # not configured
1369 1380
            return []
1370 1381

  
1371
        if self.related_card_path:
1382
        if self.get_related_card_path():
1372 1383
            # look at other cells to get related ids
1373 1384
            card_ids = self.get_card_ids_from_related(original_context, request)
1374 1385
            if card_ids == []:
......
1386 1397
                    )
1387 1398
                return self.filter_card_ids(card_ids, original_context)
1388 1399

  
1400
        if self.must_get_all():
1401
            # get all cards
1402
            return [c['id'] for c in self.get_cards_from_ids([], original_context, synchronous=True)]
1403

  
1389 1404
        if self.card_ids:
1390 1405
            # card ids template is defined
1391 1406
            return self.get_card_ids_from_template(self.card_ids, original_context, request)
......
1430 1445
        card_id = self.get_card_id(context)
1431 1446
        if not card_id:
1432 1447
            if self.card_ids or self.related_card_path:
1433
                # template or related_card_path defined, but result is empty or None
1448
                # all cards, template or related_card_path defined, but result is empty or None
1434 1449
                extra_context['card_not_found'] = True
1435 1450
            return extra_context
1436 1451

  
tests/test_wcs.py
2372 2372
    # but only one cell on the page, no relations to follow
2373 2373
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2374 2374
        ('--', True, 'Identifier from page URL'),
2375
        ('__all__', False, 'All cards'),
2376
        ('', False, 'Other Card Identifiers'),
2377
    ]
2378

  
2379
    # all cards
2380
    cell.related_card_path = '__all__'
2381
    cell.save()
2382
    resp = app.get('/manage/pages/%s/' % page.pk)
2383
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2384
        ('--', False, 'Identifier from page URL'),
2385
        ('__all__', True, 'All cards'),
2375 2386
        ('', False, 'Other Card Identifiers'),
2376 2387
    ]
2377 2388

  
2378 2389
    # add a second cell, related to the first card model
2390
    cell.related_card_path = ''
2391
    cell.save()
2379 2392
    cell2 = WcsCardInfosCell.objects.create(
2380 2393
        page=page, placeholder='content', order=1, carddef_reference='default:card_b'
2381 2394
    )
......
2383 2396
    # still no relation to follow
2384 2397
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2385 2398
        ('--', True, 'Identifier from page URL'),
2399
        ('__all__', False, 'All cards'),
2386 2400
        ('', False, 'Other Card Identifiers'),
2387 2401
    ]
2388 2402
    # no cell with id and slug
2389 2403
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2390 2404
        ('--', True, 'Identifier from page URL'),
2405
        ('__all__', False, 'All cards'),
2391 2406
        ('', False, 'Other Card Identifiers'),
2392 2407
    ]
2393 2408

  
......
2398 2413
    # still no relation to follow
2399 2414
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2400 2415
        ('--', True, 'Identifier from page URL'),
2416
        ('__all__', False, 'All cards'),
2401 2417
        ('', False, 'Other Card Identifiers'),
2402 2418
    ]
2403 2419
    # multiple relations to follow
2404 2420
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2405 2421
        ('--', True, 'Identifier from page URL'),
2422
        ('__all__', False, 'All cards'),
2406 2423
        ('', False, 'Other Card Identifiers'),
2407 2424
        ('sluga/cardb', False, 'sluga/cardb'),
2408 2425
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2419 2436
    # still no relation to follow
2420 2437
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2421 2438
        ('--', False, 'Identifier from page URL'),
2439
        ('__all__', False, 'All cards'),
2422 2440
        ('', True, 'Other Card Identifiers'),
2423 2441
    ]
2424 2442
    # can not user cell with multiple ids as reference
2425 2443
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2426 2444
        ('--', True, 'Identifier from page URL'),
2445
        ('__all__', False, 'All cards'),
2427 2446
        ('', False, 'Other Card Identifiers'),
2428 2447
    ]
2429 2448

  
......
2436 2455
    # multiple relations to follow
2437 2456
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2438 2457
        ('--', True, 'Identifier from page URL'),
2458
        ('__all__', False, 'All cards'),
2439 2459
        ('', False, 'Other Card Identifiers'),
2440 2460
        ('slugb/reverse:cardb', False, 'slugb/cardb (reverse)'),
2441 2461
        ('slugb/reverse:cardsb', False, 'slugb/cardsb (reverse)'),
......
2444 2464
    # still multiple relations to follow
2445 2465
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2446 2466
        ('--', True, 'Identifier from page URL'),
2467
        ('__all__', False, 'All cards'),
2447 2468
        ('', False, 'Other Card Identifiers'),
2448 2469
        ('sluga/cardb', False, 'sluga/cardb'),
2449 2470
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2464 2485
    # no more relation to follow
2465 2486
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2466 2487
        ('--', True, 'Identifier from page URL'),
2488
        ('__all__', False, 'All cards'),
2467 2489
        ('', False, 'Other Card Identifiers'),
2468 2490
    ]
2469 2491
    # still multiple relations to follow
2470 2492
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2471 2493
        ('--', False, 'Identifier from page URL'),
2494
        ('__all__', False, 'All cards'),
2472 2495
        ('', False, 'Other Card Identifiers'),
2473 2496
        ('sluga/cardb', True, 'sluga/cardb'),
2474 2497
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2493 2516
    resp = app.get('/manage/pages/%s/' % page.pk)
2494 2517
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2495 2518
        ('--', True, 'Identifier from page URL'),
2519
        ('__all__', False, 'All cards'),
2496 2520
        ('', False, 'Other Card Identifiers'),
2497 2521
        ('slugd/cardd-foo/carde-foo', False, 'slugd/cardd-foo/carde-foo'),
2498 2522
        ('slugd/carde-foo', False, 'slugd/carde-foo'),
2499 2523
    ]
2500 2524
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2501 2525
        ('--', True, 'Identifier from page URL'),
2526
        ('__all__', False, 'All cards'),
2502 2527
        ('', False, 'Other Card Identifiers'),
2503 2528
        ('sluge/cardd-bar', False, 'sluge/cardd-bar'),
2504 2529
        ('sluge/reverse:carde-foo', False, 'sluge/carde-foo (reverse)'),
......
2514 2539
    resp = app.get('/manage/pages/%s/' % page.pk)
2515 2540
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2516 2541
        ('--', True, 'Identifier from page URL'),
2542
        ('__all__', False, 'All cards'),
2517 2543
        ('', False, 'Other Card Identifiers'),
2518 2544
        ('slugd-bis/cardd-foo', False, 'slugd-bis/cardd-foo'),
2519 2545
        ('slugd-bis/reverse:cardd-foo', False, 'slugd-bis/cardd-foo (reverse)'),
......
2522 2548
    ]
2523 2549
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2524 2550
        ('--', True, 'Identifier from page URL'),
2551
        ('__all__', False, 'All cards'),
2525 2552
        ('', False, 'Other Card Identifiers'),
2526 2553
        ('slugd/cardd-foo', False, 'slugd/cardd-foo'),
2527 2554
        ('slugd/reverse:cardd-foo', False, 'slugd/cardd-foo (reverse)'),
......
2539 2566
    resp = app.get('/manage/pages/%s/' % page.pk)
2540 2567
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2541 2568
        ('--', True, 'Identifier from page URL'),
2569
        ('__all__', False, 'All cards'),
2542 2570
        ('', False, 'Other Card Identifiers'),
2543 2571
        ('sluge-bis/cardd-bar/carde-foo', False, 'sluge-bis/cardd-bar/carde-foo'),
2544 2572
    ]
2545 2573
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2546 2574
        ('--', True, 'Identifier from page URL'),
2575
        ('__all__', False, 'All cards'),
2547 2576
        ('', False, 'Other Card Identifiers'),
2548 2577
        ('sluge/cardd-bar/carde-foo', False, 'sluge/cardd-bar/carde-foo'),
2549 2578
    ]
......
3226 3255
    )
3227 3256

  
3228 3257

  
3258
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
3259
def test_card_cell_render_all_cards(mock_send, nocache, app):
3260
    page = Page.objects.create(title='xxx', slug='foo', template_name='standard')
3261
    cell = WcsCardInfosCell.objects.create(
3262
        page=page,
3263
        placeholder='content',
3264
        order=0,
3265
        carddef_reference='default:card_model_1',
3266
        related_card_path='__all__',
3267
    )
3268

  
3269
    cell_url = reverse(
3270
        'combo-public-ajax-page-cell',
3271
        kwargs={'page_pk': page.pk, 'cell_reference': cell.get_reference()},
3272
    )
3273

  
3274
    # check url called
3275
    mock_send.reset_mock()
3276
    resp = app.get(page.get_online_url())
3277
    assert len(resp.context['cells']) == 3
3278
    extra_ctx = re.findall(r'data-extra-context="(.*)"', resp.text)
3279
    for i in range(0, 3):
3280
        assert resp.context['cells'][i].pk == cell.pk
3281
        assert resp.context['cells'][i].repeat_index == i
3282
        cell_resp = app.get(cell_url + '?ctx=' + extra_ctx[i])
3283
        assert cell_resp.context['repeat_index'] == i
3284
    assert len(mock_send.call_args_list) == 7
3285
    # page rendering
3286
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3287
    # cell rendering
3288
    for i in range(0, 3):
3289
        assert '/api/cards/card_model_1/list' in mock_send.call_args_list[i * 2 + 1][0][0].url
3290
        assert '/api/cards/card_model_1/list' in mock_send.call_args_list[i * 2 + 2][0][0].url
3291
        assert 'filter-internal-id' not in mock_send.call_args_list[i * 2 + 2][0][0].url
3292

  
3293

  
3229 3294
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
3230 3295
def test_card_cell_render_identifier(mock_send, nocache, app):
3231 3296
    page = Page.objects.create(
3232
-