From a97c740daca8137fdac3ec543d1bcbae5b3928d9 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 11 May 2022 16:06:13 +0200 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(-) diff --git a/combo/apps/dataviz/forms.py b/combo/apps/dataviz/forms.py index ebc4207d..768f9316 100644 --- a/combo/apps/dataviz/forms.py +++ b/combo/apps/dataviz/forms.py @@ -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) diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 37a845d7..c96f42da 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -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):