dataviz: use select2 without ajax in filters cell (#71885)
gitea/combo/pipeline/head This commit looks good Details

This commit is contained in:
Valentin Deniaud 2023-04-24 15:56:38 +02:00
parent 5314bceda0
commit e5d3357951
4 changed files with 46 additions and 8 deletions

View File

@ -75,6 +75,7 @@ def trigger_statistics_list_refresh():
class ChartFiltersMixin:
ajax_choices = True
time_intervals = (
Choice('week', _('Week')),
Choice('month', _('Month')),
@ -158,7 +159,7 @@ class ChartFiltersMixin:
choices = self.get_filter_options(cell, filter_, initial)
widget_class = MultipleSelect2Widget if multiple else Select2Widget
widget = widget_class(cell, filter_['id'], choices, initial)
widget = widget_class(cell, filter_['id'], choices, initial, self.ajax_choices)
field_class = forms.MultipleChoiceField if multiple else forms.ChoiceField
field = field_class(
@ -350,6 +351,7 @@ class ChartNgPartialForm(ChartFiltersMixin, forms.ModelForm):
class ChartFiltersForm(ChartFiltersMixin, forms.ModelForm):
ajax_choices = False
overridden_filters = StaticField()
prefix = 'filter'

View File

@ -781,8 +781,8 @@ class ChartFiltersCell(CellBase):
verbose_name = _('Filters')
class Media:
js = ('js/gadjo.multiselectwidget.js',)
css = {'all': ('css/gadjo.multiselectwidget.css',)}
js = ('xstatic/select2.min.js', 'xstatic/i18n/fr.js')
css = {'all': ('xstatic/select2.min.css',)}
@classmethod
def is_enabled(cls):

View File

@ -24,17 +24,18 @@ class Select2WidgetMixin:
js = 'xstatic/select2.min.js'
css = {'all': ('xstatic/select2.min.css',)}
def __init__(self, cell, filter_id, choices, initial):
def __init__(self, cell, filter_id, choices, initial, ajax_choices):
from .forms import Choice
attrs = {}
if self.enable_select2(choices):
attrs['data-autocomplete'] = 'true'
attrs['lang'] = settings.LANGUAGE_CODE
attrs['data-select2-url'] = reverse(
'combo-dataviz-choices', kwargs={'cell_id': cell.pk, 'filter_id': filter_id}
)
choices = self.filter_choices(choices, initial)
if ajax_choices:
attrs['data-select2-url'] = reverse(
'combo-dataviz-choices', kwargs={'cell_id': cell.pk, 'filter_id': filter_id}
)
choices = self.filter_choices(choices, initial)
super().__init__(choices=Choice.get_field_choices(choices), attrs=attrs)

View File

@ -3112,6 +3112,41 @@ def test_chart_filters_cell_select_filters(new_api_statistics, app, admin_user,
assert resp.forms[0].get(field_prefix + 'filters', index=0).value == 'ou'
@with_httmock(new_api_mock)
def test_chart_filters_cell_select2_choices(app, admin_user, new_api_statistics):
page = Page.objects.create(title='One', slug='index')
cell = ChartNgCell(page=page, order=1, placeholder='content')
cell.statistic = Statistic.objects.get(slug='filter-multiple')
cell.save()
ChartFiltersCell.objects.create(page=page, order=2, placeholder='content')
# multiple select2 is enabled without ajax
resp = app.get('/')
assert 'data-autocomplete' in resp.text
assert 'data-select2-url' not in resp.text
assert resp.form['filter-color'].options == [
('red', False, 'Red'),
('green', False, 'Green'),
('blue', False, 'Blue'),
]
cell.statistic = Statistic.objects.get(slug='option-groups')
cell.save()
# add choices to enable select2
form_filter = next(x for x in cell.statistic.filters if x['id'] == 'form')
form_filter['options'].append(
['Category C', [{'id': 'test-%s' % i, 'label': 'test %s' % i} for i in range(20)]]
)
cell.statistic.save()
# single select2 is enabled without ajax
resp = app.get('/')
assert 'data-autocomplete' in resp.text
assert 'data-select2-url' not in resp.text
assert len(resp.form['filter-form'].options) == 24
@with_httmock(new_api_mock)
@pytest.mark.freeze_time('2021-10-06')
def test_chartng_cell_api_view_get_parameters(app, normal_user, new_api_statistics, nocache):