Projet

Général

Profil

0001-dataviz-add-option-groups-support-65134.patch

Valentin Deniaud, 11 mai 2022 17:17

Télécharger (3,66 ko)

Voir les différences:

Subject: [PATCH] dataviz: add option groups support (#65134)

 combo/apps/dataviz/forms.py | 16 ++++++++++++++--
 tests/test_dataviz.py       | 28 ++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 2 deletions(-)
combo/apps/dataviz/forms.py
67 67
        fields = OrderedDict()
68 68
        for filter_ in cell.available_filters:
69 69
            filter_id = filter_['id']
70
            choices = [(option['id'], option['label']) for option in filter_['options']]
70

  
71
            has_option_groups = isinstance(filter_['options'][0], list)
72
            if filter_['options'] and has_option_groups:
73
                choices = [
74
                    (group, [(opt['id'], opt['label']) for opt in options])
75
                    for group, options in filter_['options']
76
                ]
77
            else:
78
                choices = [(option['id'], option['label']) for option in filter_['options']]
79

  
71 80
            initial = cell.filter_params.get(filter_id, filter_.get('default'))
72 81

  
73 82
            if filter_id == 'time_interval':
......
81 90
            extra_variables = cell.page.get_extra_variables_keys()
82 91
            variable_choices = [('variable:' + key, key) for key in extra_variables]
83 92

  
84
            possible_choices = {choice[0] for choice in choices}
93
            if has_option_groups:
94
                possible_choices = {choice[0] for _, group_choices in choices for choice in group_choices}
95
            else:
96
                possible_choices = {choice[0] for choice in choices}
85 97
            for choice in initial if isinstance(initial, list) else [initial]:
86 98
                if not choice:
87 99
                    continue
tests/test_dataviz.py
457 457
            'id': 'with-future-data',
458 458
            'future_data': True,
459 459
        },
460
        {
461
            'url': 'https://authentic.example.com/api/statistics/option-groups/',
462
            'name': 'Option groups',
463
            'id': 'option-groups',
464
            "filters": [
465
                {
466
                    "id": "form",
467
                    "label": "Form",
468
                    "options": [
469
                        [None, [{'id': 'all', 'label': 'All'}]],
470
                        ['Category A', [{'id': 'test', 'label': 'Test'}]],
471
                        ['Category B', [{'id': 'test-2', 'label': 'test 2'}]],
472
                    ],
473
                }
474
            ],
475
        },
460 476
    ]
461 477
}
462 478

  
......
1395 1411
    cell.refresh_from_db()
1396 1412
    assert cell.get_filter_params() == {}
1397 1413

  
1414
    option_groups_stat = Statistic.objects.get(slug='option-groups')
1415
    resp.form[field_prefix + 'statistic'] = option_groups_stat.pk
1416
    manager_submit_cell(resp.form)
1417
    assert resp.form[field_prefix + 'form'].options == [
1418
        ('', True, '---------'),
1419
        ('all', False, 'All'),
1420
        ('test', False, 'Test'),
1421
        ('test-2', False, 'test 2'),
1422
    ]
1423
    assert resp.pyquery('optgroup[label="Category A"] option').val() == 'test'
1424
    assert resp.pyquery('optgroup[label="Category B"] option').val() == 'test-2'
1425

  
1398 1426

  
1399 1427
@with_httmock(new_api_mock)
1400 1428
def test_chartng_cell_manager_future_data(app, admin_user, new_api_statistics):
1401
-