dataviz: add select multiple options support (#57818)
This commit is contained in:
parent
9f6d68477a
commit
d13e801e24
|
@ -114,10 +114,12 @@ class ChartNgForm(forms.ModelForm):
|
||||||
initial = self.instance.filter_params.get(filter_id) or filter_.get('default')
|
initial = self.instance.filter_params.get(filter_id) or filter_.get('default')
|
||||||
|
|
||||||
required = filter_.get('required', False)
|
required = filter_.get('required', False)
|
||||||
if not required:
|
multiple = filter_.get('multiple')
|
||||||
|
if not required and not multiple:
|
||||||
choices.insert(0, self.blank_choice)
|
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
|
label=filter_['label'], choices=choices, required=required, initial=initial
|
||||||
)
|
)
|
||||||
field_ids.insert(field_insert_index, filter_id)
|
field_ids.insert(field_insert_index, filter_id)
|
||||||
|
|
|
@ -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}
|
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
|
@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']
|
statistics_field = resp.form[field_prefix + 'statistic']
|
||||||
assert len(statistics_field.options) == len(STATISTICS_LIST['data']) + 1
|
assert len(statistics_field.options) == len(STATISTICS_LIST['data']) + 1
|
||||||
assert statistics_field.value == str(cell.statistic.pk)
|
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']
|
time_interval_field = resp.form[field_prefix + 'time_interval']
|
||||||
assert time_interval_field.pos == statistics_field.pos + 1
|
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.filter_params == {}
|
||||||
assert cell.time_range == ''
|
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 is None
|
||||||
|
cell.refresh_from_db()
|
||||||
|
assert cell.filter_params == {}
|
||||||
|
|
||||||
|
|
||||||
@with_httmock(new_api_mock)
|
@with_httmock(new_api_mock)
|
||||||
@pytest.mark.freeze_time('2021-10-06')
|
@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 'time_interval=' not in request.url
|
||||||
assert 'ou=' 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.save()
|
||||||
cell.get_chart()
|
cell.get_chart()
|
||||||
request = new_api_mock.call['requests'][1]
|
request = new_api_mock.call['requests'][1]
|
||||||
assert 'time_interval=month' in request.url
|
assert 'time_interval=month' in request.url
|
||||||
assert 'ou=default' in request.url
|
assert 'ou=default' in request.url
|
||||||
|
assert 'color=green&color=blue' in request.url
|
||||||
|
|
||||||
freezer.move_to(date)
|
freezer.move_to(date)
|
||||||
cell.time_range = 'previous-year'
|
cell.time_range = 'previous-year'
|
||||||
|
|
Loading…
Reference in New Issue