From 627194fa8d0bb54921dff307916c96ffd091b18e Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 13 Oct 2021 11:54:31 +0200 Subject: [PATCH] dataviz: add select multiple options support (#57818) --- combo/apps/dataviz/forms.py | 6 +++-- tests/test_dataviz.py | 50 +++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/combo/apps/dataviz/forms.py b/combo/apps/dataviz/forms.py index 7a35c645..0a59f7e5 100644 --- a/combo/apps/dataviz/forms.py +++ b/combo/apps/dataviz/forms.py @@ -114,10 +114,12 @@ class ChartNgForm(forms.ModelForm): initial = self.instance.filter_params.get(filter_id) or filter_.get('default') required = filter_.get('required', False) - if not required: + multiple = filter_.get('multiple') + if not required and not multiple: choices.insert(0, self.blank_choice) - self.fields[filter_id] = forms.ChoiceField( + field_class = forms.MultipleChoiceField if multiple else forms.ChoiceField + self.fields[filter_id] = field_class( label=filter_['label'], choices=choices, required=required, initial=initial ) field_ids.insert(field_insert_index, filter_id) diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index dbb507cb..096a9588 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -364,6 +364,23 @@ STATISTICS_LIST = { } ], }, + { + 'url': 'https://authentic.example.com/api/statistics/filter-multiple/', + 'name': 'Filter on multiple values', + 'id': 'filter-multiple', + "filters": [ + { + "id": "color", + "label": "Color", + "options": [ + {"id": "red", "label": "Red"}, + {"id": "green", "label": "Green"}, + {"id": "blue", "label": "Blue"}, + ], + "multiple": True, + } + ], + }, ] } @@ -419,6 +436,17 @@ def new_api_mock(url, request): }, } return {'content': json.dumps(response), 'request': request, 'status_code': 200} + if url.path == '/api/statistics/filter-multiple/': + response = { + 'data': { + 'series': [ + {'data': [None, 1], 'label': 'Red / Green'}, + {'data': [1, 4], 'label': 'Red / Blue'}, + ], + 'x_labels': ['2020-12-30', '2020-12-31'], + }, + } + return {'content': json.dumps(response), 'request': request, 'status_code': 200} @pytest.fixture @@ -1138,7 +1166,9 @@ def test_chartng_cell_manager_new_api(app, admin_user, new_api_statistics): statistics_field = resp.form[field_prefix + 'statistic'] assert len(statistics_field.options) == len(STATISTICS_LIST['data']) + 1 assert statistics_field.value == str(cell.statistic.pk) - assert statistics_field.options[4][2] == 'Connection: One serie stat' + selected_options = [option for option in statistics_field.options if option[1] is True] + assert len(selected_options) == 1 + assert selected_options[0][2] == 'Connection: One serie stat' time_interval_field = resp.form[field_prefix + 'time_interval'] assert time_interval_field.pos == statistics_field.pos + 1 @@ -1203,6 +1233,21 @@ def test_chartng_cell_manager_new_api(app, admin_user, new_api_statistics): assert cell.filter_params == {} assert cell.time_range == '' + filter_multiple_stat = Statistic.objects.get(slug='filter-multiple') + resp.form[field_prefix + 'statistic'] = filter_multiple_stat.pk + resp = resp.form.submit().follow() + resp.form[field_prefix + 'color'].select_multiple(texts=['Blue', 'Green']) + resp = resp.form.submit().follow() + assert resp.form[field_prefix + 'color'].value == ['green', 'blue'] + cell.refresh_from_db() + assert cell.filter_params == {'color': ['green', 'blue']} + + resp.form[field_prefix + 'color'].select_multiple(texts=[]) + resp = resp.form.submit().follow() + assert resp.form[field_prefix + 'color'].value == None + cell.refresh_from_db() + assert cell.filter_params == {} + @with_httmock(new_api_mock) @pytest.mark.freeze_time('2021-10-06') @@ -1429,12 +1474,13 @@ def test_chartng_cell_new_api_filter_params(new_api_statistics, nocache, freezer assert 'time_interval=' not in request.url assert 'ou=' not in request.url - cell.filter_params = {'time_interval': 'month', 'ou': 'default'} + cell.filter_params = {'time_interval': 'month', 'ou': 'default', 'color': ['green', 'blue']} cell.save() cell.get_chart() request = new_api_mock.call['requests'][1] assert 'time_interval=month' in request.url assert 'ou=default' in request.url + assert 'color=green&color=blue' in request.url freezer.move_to(date) cell.time_range = 'previous-year' -- 2.30.2