Projet

Général

Profil

0004-wcs-opti-use-detail-url-if-only-one-id-68037.patch

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

Télécharger (9,34 ko)

Voir les différences:

Subject: [PATCH 4/5] wcs: opti, use detail url if only one id (#68037)

 combo/apps/wcs/models.py | 41 +++++++++++++++++++++++++++++++++++-----
 tests/test_wcs.py        | 33 +++++++++++++++-----------------
 2 files changed, 51 insertions(+), 23 deletions(-)
combo/apps/wcs/models.py
1087 1087
        card_ids = context.get(self.global_context_key)
1088 1088
        if not card_ids:
1089 1089
            return None
1090
        if len(card_ids) == 1:
1091
            # if only one id, do not use the list endpoint:
1092
            # may be url can be cached and reused by cells with related
1093
            return self.get_card_data(card_id=card_id, context=context)
1090 1094
        cards = self.get_cards_from_ids(card_ids, context)
1091 1095
        for card_data in cards:
1092 1096
            if str(card_data.get('id')) == str(card_id):
......
1094 1098
        return None
1095 1099

  
1096 1100
    def get_card_data(
1097
        self, card_slug, card_custom_view, card_id, only_for_user, without_user, context, synchronous=False
1101
        self,
1102
        card_id,
1103
        context,
1104
        card_slug=Ellipsis,
1105
        card_custom_view=Ellipsis,
1106
        only_for_user=Ellipsis,
1107
        without_user=Ellipsis,
1108
        synchronous=False,
1098 1109
    ):
1110
        if card_slug is Ellipsis:
1111
            card_slug = self.card_slug
1112
        if card_custom_view is Ellipsis:
1113
            card_custom_view = self.card_custom_view
1114
        if only_for_user is Ellipsis:
1115
            only_for_user = self.only_for_user
1116
        if without_user is Ellipsis:
1117
            without_user = self.without_user
1099 1118
        api_url = '/api/cards/%s/%s/?include-files-content=off' % (card_slug, card_id)
1100 1119
        if card_custom_view:
1101 1120
            api_url = '/api/cards/%s/%s/%s/?include-files-content=off' % (
......
1121 1140
            )
1122 1141
            response.raise_for_status()
1123 1142
        except RequestException:
1143
            if card_custom_view:
1144
                # if there's a custom view consider the error is a 404 because
1145
                # the card was not found in that view, and mark it so.
1146
                context['card_not_found'] = True
1124 1147
            return {}
1125 1148

  
1126 1149
        if response.status_code == 200:
......
1276 1299
                return []
1277 1300
            # and data
1278 1301
            next_card_data = self.get_card_data(
1302
                card_id=card_data['fields']['%s_raw' % varname],
1303
                context=context,
1279 1304
                card_slug=card_slug,
1280 1305
                card_custom_view=None,
1281
                card_id=card_data['fields']['%s_raw' % varname],
1282 1306
                only_for_user=False,
1283 1307
                without_user=False,
1284
                context=context,
1285 1308
                synchronous=True,
1286 1309
            )
1287 1310
            if not next_card_data:
......
1312 1335
            # no card id found
1313 1336
            return []
1314 1337
        card_data = self.get_card_data(
1338
            card_id=card_id,
1339
            context=context,
1315 1340
            card_slug=first_cell.card_slug,
1316 1341
            card_custom_view=first_cell.card_custom_view,
1317
            card_id=card_id,
1318 1342
            only_for_user=first_cell.only_for_user,
1319 1343
            without_user=first_cell.without_user,
1320
            context=context,
1321 1344
            synchronous=True,
1322 1345
        )
1323 1346
        if not card_data:
......
1353 1376
            elif card_ids:
1354 1377
                # check that ids from related are available for user
1355 1378
                # (use only_for_user and without_user flags)
1379
                if len(card_ids) == 1:
1380
                    # if only one id, do not use the list endpoint:
1381
                    # may be url can be cached and reused by cells with related
1382
                    return (
1383
                        card_ids
1384
                        if self.get_card_data(card_id=card_ids[0], context=original_context, synchronous=True)
1385
                        else []
1386
                    )
1356 1387
                return self.filter_card_ids(card_ids, original_context)
1357 1388

  
1358 1389
        if self.card_ids:
tests/test_wcs.py
3252 3252
    assert 'Card Model 1' in cell_resp
3253 3253
    assert '<p>Unknown Card</p>' not in cell_resp
3254 3254
    assert len(mock_send.call_args_list) == 1
3255
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3256
    assert '&filter-internal-id=11&' in mock_send.call_args_list[0][0][0].url
3255
    assert '/api/cards/card_model_1/11/' in mock_send.call_args_list[0][0][0].url
3257 3256

  
3258 3257
    # with identifiers
3259 3258
    page.sub_slug = ''
......
3271 3270
    assert 'Card Model 1' in cell_resp
3272 3271
    assert '<p>Unknown Card</p>' in cell_resp
3273 3272
    assert len(mock_send.call_args_list) == 1
3274
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3275
    assert '&filter-internal-id=42&' in mock_send.call_args_list[0][0][0].url
3273
    assert '/api/cards/card_model_1/42/' in mock_send.call_args_list[0][0][0].url
3276 3274

  
3277 3275
    cell.card_ids = '42, , 35'
3278 3276
    cell.save()
......
3316 3314
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3317 3315
    # cell rendering
3318 3316
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[1][0][0].url
3319
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[2][0][0].url
3320
    assert '&filter-internal-id=13&' in mock_send.call_args_list[2][0][0].url
3317
    assert '/api/cards/card_model_1/13' in mock_send.call_args_list[2][0][0].url
3321 3318

  
3322 3319
    def test_card_ids():
3323 3320
        mock_send.reset_mock()
......
3483 3480
            # get first cell data
3484 3481
            '/api/cards/card_a/1/',
3485 3482
            # and follow cardb relation
3486
            ('/api/cards/card_b/list', '&filter-internal-id=1&'),  # check user access
3487
            ('/api/cards/card_b/list', '&filter-internal-id=1&'),  # get card
3483
            '/api/cards/card_b/1/',  # check user access
3484
            '/api/cards/card_b/1/',  # get card
3488 3485
        ]
3489 3486
    )
3490 3487
    cell3.delete()  # reset
......
3500 3497
            # follow cardc relation
3501 3498
            '/api/cards/card_c/6/',
3502 3499
            # and follow cardb relation
3503
            ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # check user access
3504
            ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # get card
3500
            '/api/cards/card_b/7/',  # check user access
3501
            '/api/cards/card_b/7/',  # get card
3505 3502
        ]
3506 3503
    )
3507 3504

  
......
3521 3518
        # follow cardc relation
3522 3519
        '/api/cards/card_c/6/',
3523 3520
        # and follow cardb relation
3524
        ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # check user access
3525
        ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # get card
3521
        '/api/cards/card_b/7/',  # check user access
3522
        '/api/cards/card_b/7/',  # get card
3526 3523
    ]
3527 3524
    resp = app.get(page.get_online_url())
3528 3525
    assert len(resp.context['cells']) == 2
......
3558 3555
            # follow cardc relation
3559 3556
            '/api/cards/card_c/6/',
3560 3557
            # and follow cardb relation
3561
            ('/api/cards/card_b/list/a-custom-view', '&filter-internal-id=7&'),  # check user access
3562
            ('/api/cards/card_b/list/a-custom-view', '&filter-internal-id=7&'),  # get card
3558
            '/api/cards/card_b/a-custom-view/7/',  # check user access
3559
            '/api/cards/card_b/a-custom-view/7/',  # get card
3563 3560
        ]
3564 3561
    )
3565 3562

  
......
3877 3874
            # get list of card_f with cardf=42
3878 3875
            '/api/cards/card_f/list?orig=combo&filter-cardh=42',
3879 3876
            # and follow cardf reverse relation
3880
            ('/api/cards/card_f/list', '&filter-internal-id=41&'),  # check user access
3881
            ('/api/cards/card_f/list', '&filter-internal-id=41&'),  # get card
3877
            '/api/cards/card_f/41/',  # check user access
3878
            '/api/cards/card_f/41/',  # get card
3882 3879
        ]
3883 3880
    )
3884 3881

  
......
3897 3894
            # get list of card_g with cardf=44
3898 3895
            '/api/cards/card_g/list?orig=combo&filter-cardh=44',
3899 3896
            # and follow cardf reverse relation
3900
            ('/api/cards/card_g/list', '&filter-internal-id=43&'),  # check user access
3901
            ('/api/cards/card_g/list', '&filter-internal-id=43&'),  # get card
3897
            '/api/cards/card_g/43/',  # check user access
3898
            '/api/cards/card_g/43/',  # get card
3902 3899
        ]
3903 3900
    )
3904 3901

  
3905
-