Projet

Général

Profil

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

Lauréline Guérin, 09 août 2022 15:33

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
1014 1014
        return super().is_visible(request, **kwargs)
1015 1015

  
1016 1016
    def check_validity(self):
1017
        if self.related_card_path:
1017
        if self.get_related_card_path():
1018 1018
            relations = [r[0] for r in self.get_related_card_paths()]
1019
            if self.related_card_path not in relations:
1019
            if self.get_related_card_path() not in relations:
1020 1020
                self.mark_as_invalid('wcs_card_relation_not_found')
1021 1021
                return
1022 1022

  
......
1034 1034
    def get_repeat_template(self, context):
1035 1035
        return len(context.get(self.global_context_key) or [])
1036 1036

  
1037
    def get_related_card_path(self):
1038
        if self.related_card_path == '__all__':
1039
            return ''
1040
        return self.related_card_path
1041

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

  
1037 1047
    def get_cards_from_ids(self, card_ids, context, synchronous=False):
1038 1048
        # if check_user, get all cards from ids in context, with correct filter-user-uuid and without_user value
1039 1049
        # use custom view if requested
......
1046 1056
        user = self.get_concerned_user(context)
1047 1057
        if self.only_for_user and user and not user.is_anonymous and user.get_name_id():
1048 1058
            api_url += '&filter-user-uuid=%s' % user.get_name_id()
1049
        api_url += '&%s' % '&'.join(['filter-internal-id=%s' % cid for cid in card_ids])
1059
        if not self.get_all():
1060
            api_url += '&%s' % '&'.join(['filter-internal-id=%s' % cid for cid in card_ids])
1050 1061
        if not synchronous:
1051 1062
            synchronous = bool(context.get('synchronous'))
1052 1063
        wcs_site = get_wcs_services().get(self.wcs_site)
......
1313 1324
                parts=parts[1:],
1314 1325
            )
1315 1326

  
1316
        first_cell_slug = self.related_card_path.split('/')[0]
1327
        first_cell_slug = self.get_related_card_path().split('/')[0]
1317 1328
        try:
1318 1329
            first_cell = WcsCardInfosCell.objects.get(page=self.page_id, slug=first_cell_slug)
1319 1330
        except (WcsCardInfosCell.DoesNotExist, WcsCardInfosCell.MultipleObjectsReturned):
......
1341 1352
        if not card_data:
1342 1353
            # card data not found
1343 1354
            return []
1344
        parts = self.related_card_path.split('/')[1:]
1355
        parts = self.get_related_card_path().split('/')[1:]
1345 1356
        return follow_data(
1346 1357
            card_data=card_data,
1347 1358
            relations=first_cell.cached_json['relations'],
......
1363 1374
            # not configured
1364 1375
            return []
1365 1376

  
1366
        if self.related_card_path:
1377
        if self.get_related_card_path():
1367 1378
            # look at other cells to get related ids
1368 1379
            card_ids = self.get_card_ids_from_related(original_context, request)
1369 1380
            if card_ids == []:
......
1381 1392
                    )
1382 1393
                return self.filter_card_ids(card_ids, original_context)
1383 1394

  
1395
        if self.get_all():
1396
            # get all cards
1397
            return [c['id'] for c in self.get_cards_from_ids([], original_context, synchronous=True)]
1398

  
1384 1399
        if self.card_ids:
1385 1400
            # card ids template is defined
1386 1401
            return self.get_card_ids_from_template(self.card_ids, original_context, request)
......
1425 1440
        card_id = self.get_card_id(context)
1426 1441
        if not card_id:
1427 1442
            if self.card_ids or self.related_card_path:
1428
                # template or related_card_path defined, but result is empty or None
1443
                # all cards, template or related_card_path defined, but result is empty or None
1429 1444
                extra_context['card_not_found'] = True
1430 1445
            return extra_context
1431 1446

  
tests/test_wcs.py
2335 2335
    # but only one cell on the page, no relations to follow
2336 2336
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2337 2337
        ('--', True, 'Identifier from page URL'),
2338
        ('__all__', False, 'All cards'),
2339
        ('', False, 'Other Card Identifiers'),
2340
    ]
2341

  
2342
    # all cards
2343
    cell.related_card_path = '__all__'
2344
    cell.save()
2345
    resp = app.get('/manage/pages/%s/' % page.pk)
2346
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2347
        ('--', False, 'Identifier from page URL'),
2348
        ('__all__', True, 'All cards'),
2338 2349
        ('', False, 'Other Card Identifiers'),
2339 2350
    ]
2340 2351

  
2341 2352
    # add a second cell, related to the first card model
2353
    cell.related_card_path = ''
2354
    cell.save()
2342 2355
    cell2 = WcsCardInfosCell.objects.create(
2343 2356
        page=page, placeholder='content', order=1, carddef_reference='default:card_b'
2344 2357
    )
......
2346 2359
    # still no relation to follow
2347 2360
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2348 2361
        ('--', True, 'Identifier from page URL'),
2362
        ('__all__', False, 'All cards'),
2349 2363
        ('', False, 'Other Card Identifiers'),
2350 2364
    ]
2351 2365
    # no cell with id and slug
2352 2366
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2353 2367
        ('--', True, 'Identifier from page URL'),
2368
        ('__all__', False, 'All cards'),
2354 2369
        ('', False, 'Other Card Identifiers'),
2355 2370
    ]
2356 2371

  
......
2361 2376
    # still no relation to follow
2362 2377
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2363 2378
        ('--', True, 'Identifier from page URL'),
2379
        ('__all__', False, 'All cards'),
2364 2380
        ('', False, 'Other Card Identifiers'),
2365 2381
    ]
2366 2382
    # multiple relations to follow
2367 2383
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2368 2384
        ('--', True, 'Identifier from page URL'),
2385
        ('__all__', False, 'All cards'),
2369 2386
        ('', False, 'Other Card Identifiers'),
2370 2387
        ('sluga/cardb', False, 'sluga/cardb'),
2371 2388
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2382 2399
    # still no relation to follow
2383 2400
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2384 2401
        ('--', False, 'Identifier from page URL'),
2402
        ('__all__', False, 'All cards'),
2385 2403
        ('', True, 'Other Card Identifiers'),
2386 2404
    ]
2387 2405
    # can not user cell with multiple ids as reference
2388 2406
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2389 2407
        ('--', True, 'Identifier from page URL'),
2408
        ('__all__', False, 'All cards'),
2390 2409
        ('', False, 'Other Card Identifiers'),
2391 2410
    ]
2392 2411

  
......
2399 2418
    # multiple relations to follow
2400 2419
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2401 2420
        ('--', True, 'Identifier from page URL'),
2421
        ('__all__', False, 'All cards'),
2402 2422
        ('', False, 'Other Card Identifiers'),
2403 2423
        ('slugb/reverse:cardb', False, 'slugb/cardb (reverse)'),
2404 2424
        ('slugb/reverse:cardsb', False, 'slugb/cardsb (reverse)'),
......
2407 2427
    # still multiple relations to follow
2408 2428
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2409 2429
        ('--', True, 'Identifier from page URL'),
2430
        ('__all__', False, 'All cards'),
2410 2431
        ('', False, 'Other Card Identifiers'),
2411 2432
        ('sluga/cardb', False, 'sluga/cardb'),
2412 2433
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2427 2448
    # no more relation to follow
2428 2449
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2429 2450
        ('--', True, 'Identifier from page URL'),
2451
        ('__all__', False, 'All cards'),
2430 2452
        ('', False, 'Other Card Identifiers'),
2431 2453
    ]
2432 2454
    # still multiple relations to follow
2433 2455
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2434 2456
        ('--', False, 'Identifier from page URL'),
2457
        ('__all__', False, 'All cards'),
2435 2458
        ('', False, 'Other Card Identifiers'),
2436 2459
        ('sluga/cardb', True, 'sluga/cardb'),
2437 2460
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2456 2479
    resp = app.get('/manage/pages/%s/' % page.pk)
2457 2480
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2458 2481
        ('--', True, 'Identifier from page URL'),
2482
        ('__all__', False, 'All cards'),
2459 2483
        ('', False, 'Other Card Identifiers'),
2460 2484
        ('slugd/cardd-foo/carde-foo', False, 'slugd/cardd-foo/carde-foo'),
2461 2485
        ('slugd/carde-foo', False, 'slugd/carde-foo'),
2462 2486
    ]
2463 2487
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2464 2488
        ('--', True, 'Identifier from page URL'),
2489
        ('__all__', False, 'All cards'),
2465 2490
        ('', False, 'Other Card Identifiers'),
2466 2491
        ('sluge/cardd-bar', False, 'sluge/cardd-bar'),
2467 2492
        ('sluge/reverse:carde-foo', False, 'sluge/carde-foo (reverse)'),
......
2477 2502
    resp = app.get('/manage/pages/%s/' % page.pk)
2478 2503
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2479 2504
        ('--', True, 'Identifier from page URL'),
2505
        ('__all__', False, 'All cards'),
2480 2506
        ('', False, 'Other Card Identifiers'),
2481 2507
        ('slugd-bis/cardd-foo', False, 'slugd-bis/cardd-foo'),
2482 2508
        ('slugd-bis/reverse:cardd-foo', False, 'slugd-bis/cardd-foo (reverse)'),
......
2485 2511
    ]
2486 2512
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2487 2513
        ('--', True, 'Identifier from page URL'),
2514
        ('__all__', False, 'All cards'),
2488 2515
        ('', False, 'Other Card Identifiers'),
2489 2516
        ('slugd/cardd-foo', False, 'slugd/cardd-foo'),
2490 2517
        ('slugd/reverse:cardd-foo', False, 'slugd/cardd-foo (reverse)'),
......
2502 2529
    resp = app.get('/manage/pages/%s/' % page.pk)
2503 2530
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2504 2531
        ('--', True, 'Identifier from page URL'),
2532
        ('__all__', False, 'All cards'),
2505 2533
        ('', False, 'Other Card Identifiers'),
2506 2534
        ('sluge-bis/cardd-bar/carde-foo', False, 'sluge-bis/cardd-bar/carde-foo'),
2507 2535
    ]
2508 2536
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2509 2537
        ('--', True, 'Identifier from page URL'),
2538
        ('__all__', False, 'All cards'),
2510 2539
        ('', False, 'Other Card Identifiers'),
2511 2540
        ('sluge/cardd-bar/carde-foo', False, 'sluge/cardd-bar/carde-foo'),
2512 2541
    ]
......
3189 3218
    )
3190 3219

  
3191 3220

  
3221
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
3222
def test_card_cell_render_all_cards(mock_send, nocache, app):
3223
    page = Page.objects.create(title='xxx', slug='foo', template_name='standard')
3224
    cell = WcsCardInfosCell.objects.create(
3225
        page=page,
3226
        placeholder='content',
3227
        order=0,
3228
        carddef_reference='default:card_model_1',
3229
        related_card_path='__all__',
3230
    )
3231

  
3232
    cell_url = reverse(
3233
        'combo-public-ajax-page-cell',
3234
        kwargs={'page_pk': page.pk, 'cell_reference': cell.get_reference()},
3235
    )
3236

  
3237
    # check url called
3238
    mock_send.reset_mock()
3239
    resp = app.get(page.get_online_url())
3240
    assert len(resp.context['cells']) == 3
3241
    extra_ctx = re.findall(r'data-extra-context="(.*)"', resp.text)
3242
    for i in range(0, 3):
3243
        assert resp.context['cells'][i].pk == cell.pk
3244
        assert resp.context['cells'][i].repeat_index == i
3245
        cell_resp = app.get(cell_url + '?ctx=' + extra_ctx[i])
3246
        assert cell_resp.context['repeat_index'] == i
3247
    assert len(mock_send.call_args_list) == 7
3248
    # page rendering
3249
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3250
    # cell rendering
3251
    for i in range(0, 3):
3252
        assert '/api/cards/card_model_1/list' in mock_send.call_args_list[i * 2 + 1][0][0].url
3253
        assert '/api/cards/card_model_1/list' in mock_send.call_args_list[i * 2 + 2][0][0].url
3254
        assert 'filter-internal-id' not in mock_send.call_args_list[i * 2 + 2][0][0].url
3255

  
3256

  
3192 3257
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
3193 3258
def test_card_cell_render_identifier(mock_send, nocache, app):
3194 3259
    page = Page.objects.create(
3195
-