Projet

Général

Profil

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

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

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
1082 1082
        card_ids = context.get(self.global_context_key)
1083 1083
        if not card_ids:
1084 1084
            return None
1085
        if len(card_ids) == 1:
1086
            # if only one id, do not use the list endpoint:
1087
            # may be url can be cached and reused by cells with related
1088
            return self.get_card_data(card_id=card_id, context=context)
1085 1089
        cards = self.get_cards_from_ids(card_ids, context)
1086 1090
        for card_data in cards:
1087 1091
            if str(card_data.get('id')) == str(card_id):
......
1089 1093
        return None
1090 1094

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

  
1121 1144
        if response.status_code == 200:
......
1271 1294
                return []
1272 1295
            # and data
1273 1296
            next_card_data = self.get_card_data(
1297
                card_id=card_data['fields']['%s_raw' % varname],
1298
                context=context,
1274 1299
                card_slug=card_slug,
1275 1300
                card_custom_view=None,
1276
                card_id=card_data['fields']['%s_raw' % varname],
1277 1301
                only_for_user=False,
1278 1302
                without_user=False,
1279
                context=context,
1280 1303
                synchronous=True,
1281 1304
            )
1282 1305
            if not next_card_data:
......
1307 1330
            # no card id found
1308 1331
            return []
1309 1332
        card_data = self.get_card_data(
1333
            card_id=card_id,
1334
            context=context,
1310 1335
            card_slug=first_cell.card_slug,
1311 1336
            card_custom_view=first_cell.card_custom_view,
1312
            card_id=card_id,
1313 1337
            only_for_user=first_cell.only_for_user,
1314 1338
            without_user=first_cell.without_user,
1315
            context=context,
1316 1339
            synchronous=True,
1317 1340
        )
1318 1341
        if not card_data:
......
1348 1371
            elif card_ids:
1349 1372
                # check that ids from related are available for user
1350 1373
                # (use only_for_user and without_user flags)
1374
                if len(card_ids) == 1:
1375
                    # if only one id, do not use the list endpoint:
1376
                    # may be url can be cached and reused by cells with related
1377
                    return (
1378
                        card_ids
1379
                        if self.get_card_data(card_id=card_ids[0], context=original_context, synchronous=True)
1380
                        else []
1381
                    )
1351 1382
                return self.filter_card_ids(card_ids, original_context)
1352 1383

  
1353 1384
        if self.card_ids:
tests/test_wcs.py
3215 3215
    assert 'Card Model 1' in cell_resp
3216 3216
    assert '<p>Unknown Card</p>' not in cell_resp
3217 3217
    assert len(mock_send.call_args_list) == 1
3218
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3219
    assert '&filter-internal-id=11&' in mock_send.call_args_list[0][0][0].url
3218
    assert '/api/cards/card_model_1/11/' in mock_send.call_args_list[0][0][0].url
3220 3219

  
3221 3220
    # with identifiers
3222 3221
    page.sub_slug = ''
......
3234 3233
    assert 'Card Model 1' in cell_resp
3235 3234
    assert '<p>Unknown Card</p>' in cell_resp
3236 3235
    assert len(mock_send.call_args_list) == 1
3237
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3238
    assert '&filter-internal-id=42&' in mock_send.call_args_list[0][0][0].url
3236
    assert '/api/cards/card_model_1/42/' in mock_send.call_args_list[0][0][0].url
3239 3237

  
3240 3238
    cell.card_ids = '42, , 35'
3241 3239
    cell.save()
......
3279 3277
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
3280 3278
    # cell rendering
3281 3279
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[1][0][0].url
3282
    assert '/api/cards/card_model_1/list' in mock_send.call_args_list[2][0][0].url
3283
    assert '&filter-internal-id=13&' in mock_send.call_args_list[2][0][0].url
3280
    assert '/api/cards/card_model_1/13' in mock_send.call_args_list[2][0][0].url
3284 3281

  
3285 3282
    def test_card_ids():
3286 3283
        mock_send.reset_mock()
......
3446 3443
            # get first cell data
3447 3444
            '/api/cards/card_a/1/',
3448 3445
            # and follow cardb relation
3449
            ('/api/cards/card_b/list', '&filter-internal-id=1&'),  # check user access
3450
            ('/api/cards/card_b/list', '&filter-internal-id=1&'),  # get card
3446
            '/api/cards/card_b/1/',  # check user access
3447
            '/api/cards/card_b/1/',  # get card
3451 3448
        ]
3452 3449
    )
3453 3450
    cell3.delete()  # reset
......
3463 3460
            # follow cardc relation
3464 3461
            '/api/cards/card_c/6/',
3465 3462
            # and follow cardb relation
3466
            ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # check user access
3467
            ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # get card
3463
            '/api/cards/card_b/7/',  # check user access
3464
            '/api/cards/card_b/7/',  # get card
3468 3465
        ]
3469 3466
    )
3470 3467

  
......
3484 3481
        # follow cardc relation
3485 3482
        '/api/cards/card_c/6/',
3486 3483
        # and follow cardb relation
3487
        ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # check user access
3488
        ('/api/cards/card_b/list', '&filter-internal-id=7&'),  # get card
3484
        '/api/cards/card_b/7/',  # check user access
3485
        '/api/cards/card_b/7/',  # get card
3489 3486
    ]
3490 3487
    resp = app.get(page.get_online_url())
3491 3488
    assert len(resp.context['cells']) == 2
......
3521 3518
            # follow cardc relation
3522 3519
            '/api/cards/card_c/6/',
3523 3520
            # and follow cardb relation
3524
            ('/api/cards/card_b/list/a-custom-view', '&filter-internal-id=7&'),  # check user access
3525
            ('/api/cards/card_b/list/a-custom-view', '&filter-internal-id=7&'),  # get card
3521
            '/api/cards/card_b/a-custom-view/7/',  # check user access
3522
            '/api/cards/card_b/a-custom-view/7/',  # get card
3526 3523
        ]
3527 3524
    )
3528 3525

  
......
3840 3837
            # get list of card_f with cardf=42
3841 3838
            '/api/cards/card_f/list?orig=combo&filter-cardh=42',
3842 3839
            # and follow cardf reverse relation
3843
            ('/api/cards/card_f/list', '&filter-internal-id=41&'),  # check user access
3844
            ('/api/cards/card_f/list', '&filter-internal-id=41&'),  # get card
3840
            '/api/cards/card_f/41/',  # check user access
3841
            '/api/cards/card_f/41/',  # get card
3845 3842
        ]
3846 3843
    )
3847 3844

  
......
3860 3857
            # get list of card_g with cardf=44
3861 3858
            '/api/cards/card_g/list?orig=combo&filter-cardh=44',
3862 3859
            # and follow cardf reverse relation
3863
            ('/api/cards/card_g/list', '&filter-internal-id=43&'),  # check user access
3864
            ('/api/cards/card_g/list', '&filter-internal-id=43&'),  # get card
3860
            '/api/cards/card_g/43/',  # check user access
3861
            '/api/cards/card_g/43/',  # get card
3865 3862
        ]
3866 3863
    )
3867 3864

  
3868
-