dataviz: add support for filter deprecation (#65140)

This commit is contained in:
Valentin Deniaud 2022-05-11 16:06:13 +02:00
parent c909f70ad6
commit a97c740dac
2 changed files with 52 additions and 1 deletions

View File

@ -67,6 +67,11 @@ class ChartFiltersMixin:
fields = OrderedDict()
for filter_ in cell.available_filters:
filter_id = filter_['id']
if filter_.get('deprecated') and (
filter_id not in cell.filter_params
or cell.filter_params.get(filter_id) == filter_.get('default')
):
continue
has_option_groups = isinstance(filter_['options'][0], list)
if filter_['options'] and has_option_groups:
@ -111,6 +116,9 @@ class ChartFiltersMixin:
fields[filter_id] = field_class(
label=filter_['label'], choices=choices, required=required, initial=initial
)
if filter_.get('deprecated'):
fields[filter_id].label += ' (%s)' % _('deprecated')
fields[filter_id].help_text = filter_.get('deprecation_hint')
fields[filter_id].is_filter_field = True
return fields
@ -198,7 +206,8 @@ class ChartNgForm(ChartFiltersMixin, forms.ModelForm):
self.instance.filter_params[filter_['id']] = filter_['default']
else:
for filter_ in self.instance.available_filters:
self.instance.filter_params[filter_['id']] = self.cleaned_data.get(filter_['id'])
if filter_['id'] in self.cleaned_data:
self.instance.filter_params[filter_['id']] = self.cleaned_data[filter_['id']]
cell = super().save(*args, **kwargs)

View File

@ -473,6 +473,33 @@ STATISTICS_LIST = {
}
],
},
{
'url': 'https://authentic.example.com/api/statistics/deprecated-filter/',
'name': 'Deprecated filter',
'id': 'deprecated-filter',
"filters": [
{
"id": "form",
"label": "Form",
'deprecated': True,
'deprecation_hint': 'This field should not be used',
'options': [
{'id': 'one', 'label': 'One'},
{'id': 'two', 'label': 'two'},
],
},
{
"id": "card",
"label": "Card",
'deprecated': True,
'options': [
{'id': 'one', 'label': 'One'},
{'id': 'two', 'label': 'two'},
],
'default': 'one',
},
],
},
]
}
@ -1423,6 +1450,21 @@ def test_chartng_cell_manager_new_api(app, admin_user, new_api_statistics):
assert resp.pyquery('optgroup[label="Category A"] option').val() == 'test'
assert resp.pyquery('optgroup[label="Category B"] option').val() == 'test-2'
deprecated_stat = Statistic.objects.get(slug='deprecated-filter')
resp.form[field_prefix + 'statistic'] = deprecated_stat.pk
manager_submit_cell(resp.form)
assert field_prefix + 'form' not in resp.form.fields
assert field_prefix + 'card' not in resp.form.fields
cell.refresh_from_db()
cell.filter_params = {'form': 'one', 'card': 'one'}
cell.save()
resp = app.get('/manage/pages/%s/' % page.id)
assert field_prefix + 'form' in resp.form.fields
assert field_prefix + 'card' not in resp.form.fields
assert 'Form (deprecated)' in resp.text
assert 'This field should not be used' in resp.text
@with_httmock(new_api_mock)
def test_chartng_cell_manager_future_data(app, admin_user, new_api_statistics):