Projet

Général

Profil

0001-dataviz-add-support-for-filter-deprecation-65140.patch

Valentin Deniaud, 12 mai 2022 17:34

Télécharger (4,28 ko)

Voir les différences:

Subject: [PATCH] dataviz: add support for filter deprecation (#65140)

 combo/apps/dataviz/forms.py | 11 +++++++++-
 tests/test_dataviz.py       | 42 +++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)
combo/apps/dataviz/forms.py
67 67
        fields = OrderedDict()
68 68
        for filter_ in cell.available_filters:
69 69
            filter_id = filter_['id']
70
            if filter_.get('deprecated') and (
71
                filter_id not in cell.filter_params
72
                or cell.filter_params.get(filter_id) == filter_.get('default')
73
            ):
74
                continue
70 75

  
71 76
            has_option_groups = isinstance(filter_['options'][0], list)
72 77
            if filter_['options'] and has_option_groups:
......
111 116
            fields[filter_id] = field_class(
112 117
                label=filter_['label'], choices=choices, required=required, initial=initial
113 118
            )
119
            if filter_.get('deprecated'):
120
                fields[filter_id].label += ' (%s)' % _('deprecated')
121
                fields[filter_id].help_text = filter_.get('deprecation_hint')
114 122
            fields[filter_id].is_filter_field = True
115 123

  
116 124
        return fields
......
198 206
                    self.instance.filter_params[filter_['id']] = filter_['default']
199 207
        else:
200 208
            for filter_ in self.instance.available_filters:
201
                self.instance.filter_params[filter_['id']] = self.cleaned_data.get(filter_['id'])
209
                if filter_['id'] in self.cleaned_data:
210
                    self.instance.filter_params[filter_['id']] = self.cleaned_data[filter_['id']]
202 211

  
203 212
        cell = super().save(*args, **kwargs)
204 213

  
tests/test_dataviz.py
473 473
                }
474 474
            ],
475 475
        },
476
        {
477
            'url': 'https://authentic.example.com/api/statistics/deprecated-filter/',
478
            'name': 'Deprecated filter',
479
            'id': 'deprecated-filter',
480
            "filters": [
481
                {
482
                    "id": "form",
483
                    "label": "Form",
484
                    'deprecated': True,
485
                    'deprecation_hint': 'This field should not be used',
486
                    'options': [
487
                        {'id': 'one', 'label': 'One'},
488
                        {'id': 'two', 'label': 'two'},
489
                    ],
490
                },
491
                {
492
                    "id": "card",
493
                    "label": "Card",
494
                    'deprecated': True,
495
                    'options': [
496
                        {'id': 'one', 'label': 'One'},
497
                        {'id': 'two', 'label': 'two'},
498
                    ],
499
                    'default': 'one',
500
                },
501
            ],
502
        },
476 503
    ]
477 504
}
478 505

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

  
1453
    deprecated_stat = Statistic.objects.get(slug='deprecated-filter')
1454
    resp.form[field_prefix + 'statistic'] = deprecated_stat.pk
1455
    manager_submit_cell(resp.form)
1456
    assert field_prefix + 'form' not in resp.form.fields
1457
    assert field_prefix + 'card' not in resp.form.fields
1458

  
1459
    cell.refresh_from_db()
1460
    cell.filter_params = {'form': 'one', 'card': 'one'}
1461
    cell.save()
1462
    resp = app.get('/manage/pages/%s/' % page.id)
1463
    assert field_prefix + 'form' in resp.form.fields
1464
    assert field_prefix + 'card' not in resp.form.fields
1465
    assert 'Form (deprecated)' in resp.text
1466
    assert 'This field should not be used' in resp.text
1467

  
1426 1468

  
1427 1469
@with_httmock(new_api_mock)
1428 1470
def test_chartng_cell_manager_future_data(app, admin_user, new_api_statistics):
1429
-