From bf2478f4f969f9392191638f098d5214523afbc0 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 17 May 2022 11:45:30 +0200 Subject: [PATCH 1/2] dataviz: add prefix to filters cell form (#65348) --- combo/apps/dataviz/forms.py | 8 +++-- tests/test_dataviz.py | 62 ++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/combo/apps/dataviz/forms.py b/combo/apps/dataviz/forms.py index 768f9316..ec6f25e6 100644 --- a/combo/apps/dataviz/forms.py +++ b/combo/apps/dataviz/forms.py @@ -236,6 +236,8 @@ class ChartNgForm(ChartFiltersMixin, forms.ModelForm): class ChartNgPartialForm(ChartFiltersMixin, forms.ModelForm): + prefix = 'filter' + class Meta: model = ChartNgCell fields = ( @@ -252,15 +254,17 @@ class ChartNgPartialForm(ChartFiltersMixin, forms.ModelForm): def clean(self): for field in self._meta.fields: - if field not in self.data: + if '%s-%s' % (self.prefix, field) not in self.data: self.cleaned_data[field] = self.initial[field] for filter_ in self.instance.available_filters: - if filter_['id'] in self.data: + if '%s-%s' % (self.prefix, filter_['id']) in self.data: self.instance.filter_params[filter_['id']] = self.cleaned_data.get(filter_['id']) class ChartFiltersForm(ChartFiltersMixin, forms.ModelForm): + prefix = 'filter' + class Meta: model = ChartNgCell fields = ( diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 435092ce..8b725b18 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -2279,10 +2279,10 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): first_cell.save() resp = app.get('/') assert len(resp.form.fields) == 7 - assert 'time_range_start' in resp.form.fields - assert 'time_range_end' in resp.form.fields + assert 'filter-time_range_start' in resp.form.fields + assert 'filter-time_range_end' in resp.form.fields - time_range_field = resp.form['time_range'] + time_range_field = resp.form['filter-time_range'] assert time_range_field.value == '' assert time_range_field.options == [ ('', True, '---------'), @@ -2295,7 +2295,7 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): ('range', False, 'Free range (date)'), ] - time_interval_field = resp.form['time_interval'] + time_interval_field = resp.form['filter-time_interval'] assert time_interval_field.value == 'month' assert time_interval_field.options == [ ('day', False, 'Day'), @@ -2305,7 +2305,7 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): ('weekday', False, 'Week day'), ] - service_field = resp.form['service'] + service_field = resp.form['filter-service'] assert service_field.value == 'chrono' assert service_field.options == [ ('', False, '---------'), @@ -2313,7 +2313,7 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): ('combo', False, 'Combo'), ] - ou_field = resp.form['ou'] + ou_field = resp.form['filter-ou'] assert ou_field.value == '' assert ou_field.options == [ ('', True, '---------'), @@ -2328,7 +2328,7 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): old_resp = resp resp = app.get('/') for field in ('time_range', 'time_interval', 'service', 'ou'): - assert resp.form[field].options == old_resp.form[field].options + assert resp.form['filter-%s' % field].options == old_resp.form['filter-%s' % field].options # changing one filter value makes it disappear cell.filter_params = {'ou': 'default'} @@ -2336,13 +2336,13 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): resp = app.get('/') assert 'ou' not in resp.form.fields for field in ('time_range', 'time_interval', 'service'): - assert resp.form[field].options == old_resp.form[field].options + assert resp.form['filter-%s' % field].options == old_resp.form['filter-%s' % field].options # setting the same value for the other cell makes it appear again first_cell.filter_params = {'ou': 'default'} first_cell.save() resp = app.get('/') - assert resp.form['ou'].value == 'default' + assert resp.form['filter-ou'].value == 'default' # changing statistics type of cell remove some fields cell.statistic = Statistic.objects.get(slug='daily') @@ -2351,30 +2351,30 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): assert 'ou' not in resp.form.fields assert 'service' not in resp.form.fields for field in ('time_range', 'time_interval'): - assert resp.form[field].options == old_resp.form[field].options + assert resp.form['filter-%s' % field].options == old_resp.form['filter-%s' % field].options # changing time_interval value makes interval fields disappear cell.time_range = 'previous-year' cell.save() old_resp = resp resp = app.get('/') - assert 'time_range' not in resp.form.fields - assert 'time_range_start' not in resp.form.fields - assert 'time_range_end' not in resp.form.fields - assert resp.form['time_interval'].options == old_resp.form['time_interval'].options + assert 'filter-time_range' not in resp.form.fields + assert 'filter-time_range_start' not in resp.form.fields + assert 'filter-time_range_end' not in resp.form.fields + assert resp.form['filter-time_interval'].options == old_resp.form['filter-time_interval'].options # setting the same value for the other cell makes it appear again first_cell.time_range = 'previous-year' first_cell.save() resp = app.get('/') - assert resp.form['time_range'].value == 'previous-year' - assert resp.form['time_interval'].options == old_resp.form['time_interval'].options + assert resp.form['filter-time_range'].value == 'previous-year' + assert resp.form['filter-time_interval'].options == old_resp.form['filter-time_interval'].options # only common choices are shown first_cell.statistic.filters[0]['options'].remove({'id': 'day', 'label': 'Day'}) first_cell.statistic.save() resp = app.get('/') - assert resp.form['time_interval'].options == [ + assert resp.form['filter-time_interval'].options == [ ('month', True, 'Month'), ('year', False, 'Year'), ] @@ -2383,8 +2383,8 @@ def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache): first_cell.statistic.filters[0]['options'] = [{'id': 'random', 'label': 'Random'}] first_cell.statistic.save() resp = app.get('/') - assert 'time_interval' not in resp.form.fields - assert resp.form['time_range'].value == 'previous-year' + assert 'filter-time_interval' not in resp.form.fields + assert resp.form['filter-time_range'].value == 'previous-year' # form is not shown if no common filters exist first_cell.time_range = 'current-year' @@ -2403,7 +2403,7 @@ def test_chart_filters_cell_future_data(app, admin_user, new_api_statistics): app = login(app) resp = app.get('/') - time_range_field = resp.form['time_range'] + time_range_field = resp.form['filter-time_range'] assert time_range_field.value == '' assert time_range_field.options == [ ('', True, '---------'), @@ -2425,7 +2425,7 @@ def test_chart_filters_cell_future_data(app, admin_user, new_api_statistics): cell.save() resp = app.get('/') - time_range_field = resp.form['time_range'] + time_range_field = resp.form['filter-time_range'] assert time_range_field.value == '' assert time_range_field.options == [ ('', True, '---------'), @@ -2449,8 +2449,8 @@ def test_chart_filters_cell_with_subfilters(new_api_statistics, app, admin_user, app = login(app) resp = app.get('/') - assert 'form' in resp.form.fields - assert 'menu' not in resp.form.fields + assert 'filter-form' in resp.form.fields + assert 'filter-menu' not in resp.form.fields # select a choice with subfilters in manager resp = app.get('/manage/pages/%s/' % page.id) @@ -2458,8 +2458,8 @@ def test_chart_filters_cell_with_subfilters(new_api_statistics, app, admin_user, manager_submit_cell(resp.forms[1]) resp = app.get('/') - assert 'form' in resp.form.fields - assert 'menu' in resp.form.fields + assert 'filter-form' in resp.form.fields + assert 'filter-menu' in resp.form.fields @with_httmock(new_api_mock) @@ -2483,7 +2483,7 @@ def test_chartng_cell_api_view_get_parameters(app, normal_user, new_api_statisti assert 'time_interval=month' in request.url assert 'ou=default' in request.url - app.get(location + '?time_interval=year') + app.get(location + '?filter-time_interval=year') request = new_api_mock.call['requests'][2] assert 'time_interval=year' in request.url assert 'ou=default' in request.url @@ -2497,7 +2497,7 @@ def test_chartng_cell_api_view_get_parameters(app, normal_user, new_api_statisti assert 'start=2022-01-01' in request.url assert 'ou=default' in request.url - app.get(location + '?time_range=current-month') + app.get(location + '?filter-time_range=current-month') request = new_api_mock.call['requests'][4] assert 'start=2021-10-01' in request.url assert 'end=2021-11-01' in request.url @@ -2506,7 +2506,7 @@ def test_chartng_cell_api_view_get_parameters(app, normal_user, new_api_statisti cell.filter_params.clear() cell.statistic = Statistic.objects.get(slug='filter-multiple') cell.save() - app.get(location + '?color=green&color=blue') + app.get(location + '?filter-color=green&filter-color=blue') request = new_api_mock.call['requests'][5] assert 'color=green&color=blue' in request.url @@ -2515,18 +2515,18 @@ def test_chartng_cell_api_view_get_parameters(app, normal_user, new_api_statisti cell.filter_params = {'form': 'food-request'} cell.save() cell.update_subfilters() - app.get(location + '?menu=vegan') + app.get(location + '?filter-menu=vegan') request = new_api_mock.call['requests'][7] assert 'menu=vegan' in request.url # unknown params - app.get(location + '?time_interval=month&ou=default') + app.get(location + '?filter-time_interval=month&filter-ou=default') request = new_api_mock.call['requests'][8] assert 'time_interval=' not in request.url assert 'ou=' not in request.url # wrong params - resp = app.get(location + '?time_range_start=xxx') + resp = app.get(location + '?filter-time_range_start=xxx') assert 'Wrong parameters' in resp.text assert len(new_api_mock.call['requests']) == 9 -- 2.30.2