Projet

Général

Profil

0001-dataviz-support-required-filter-without-default-valu.patch

Valentin Deniaud, 06 juillet 2022 11:02

Télécharger (3,81 ko)

Voir les différences:

Subject: [PATCH] dataviz: support required filter without default value
 (#66299)

 combo/apps/dataviz/forms.py |  7 ++++++
 tests/test_dataviz.py       | 44 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
combo/apps/dataviz/forms.py
204 204
            for filter_ in self.instance.available_filters:
205 205
                if 'default' in filter_:
206 206
                    self.instance.filter_params[filter_['id']] = filter_['default']
207
                elif filter_.get('required'):
208
                    options = (
209
                        filter_['options'][0][1]
210
                        if isinstance(filter_['options'][0], list)
211
                        else filter_['options']
212
                    )
213
                    self.instance.filter_params[filter_['id']] = options[0]['id']
207 214
        else:
208 215
            for filter_ in self.instance.available_filters:
209 216
                if filter_['id'] in self.cleaned_data:
tests/test_dataviz.py
470 470
                        ['Category A', [{'id': 'test', 'label': 'Test'}]],
471 471
                        ['Category B', [{'id': 'test-2', 'label': 'test 2'}]],
472 472
                    ],
473
                }
473
                },
474
                {
475
                    "id": "cards_count",
476
                    "label": "Cards",
477
                    "options": [
478
                        ['Category A', [{'id': 'test', 'label': 'Test'}]],
479
                        ['Category B', [{'id': 'test-2', 'label': 'test 2'}]],
480
                    ],
481
                    "required": True,
482
                },
474 483
            ],
475 484
        },
476 485
        {
......
500 509
                },
501 510
            ],
502 511
        },
512
        {
513
            'url': 'https://authentic.example.com/api/statistics/required-without-default/',
514
            'name': 'Required without default',
515
            'id': 'required-without-default',
516
            "filters": [
517
                {
518
                    "id": "test",
519
                    "label": "Test",
520
                    "options": [
521
                        {"id": "b", "label": "B"},
522
                        {"id": "a", "label": "A"},
523
                    ],
524
                    "required": True,
525
                },
526
            ],
527
        },
503 528
    ]
504 529
}
505 530

  
......
1450 1475
    assert resp.pyquery('optgroup[label="Category A"] option').val() == 'test'
1451 1476
    assert resp.pyquery('optgroup[label="Category B"] option').val() == 'test-2'
1452 1477

  
1478
    assert resp.form[field_prefix + 'cards_count'].options == [
1479
        ('test', True, 'Test'),
1480
        ('test-2', False, 'test 2'),
1481
    ]
1482
    cell.refresh_from_db()
1483
    assert cell.filter_params == {'cards_count': 'test'}
1484

  
1485
    required_without_default_stat = Statistic.objects.get(slug='required-without-default')
1486
    resp.form[field_prefix + 'statistic'] = required_without_default_stat.pk
1487
    manager_submit_cell(resp.form)
1488
    assert resp.form[field_prefix + 'test'].options == [
1489
        ('b', True, 'B'),
1490
        ('a', False, 'A'),
1491
    ]
1492
    cell.refresh_from_db()
1493
    assert cell.filter_params == {'test': 'b'}
1494

  
1453 1495
    deprecated_stat = Statistic.objects.get(slug='deprecated-filter')
1454 1496
    resp.form[field_prefix + 'statistic'] = deprecated_stat.pk
1455 1497
    manager_submit_cell(resp.form)
1456
-