dataviz: display time_range fields only if relevant (#50836)
This commit is contained in:
parent
4a2dedf41a
commit
bc9f6b9fba
|
@ -56,35 +56,37 @@ class ChartNgForm(forms.ModelForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
stat_field = self.fields['statistic']
|
|
||||||
if not self.instance.statistic:
|
|
||||||
stat_field.queryset = stat_field.queryset.filter(available=True)
|
|
||||||
return
|
|
||||||
|
|
||||||
# display current statistic in choices even if unavailable
|
|
||||||
stat_field.queryset = stat_field.queryset.filter(Q(available=True) | Q(pk=self.instance.statistic.pk))
|
|
||||||
|
|
||||||
field_ids = list(self._meta.fields)
|
field_ids = list(self._meta.fields)
|
||||||
if self.instance.statistic.service_slug == 'bijoe':
|
if not self.instance.statistic or self.instance.statistic.service_slug == 'bijoe':
|
||||||
field_ids = [
|
field_ids = [
|
||||||
x for x in field_ids if x not in ('time_range', 'time_range_start', 'time_range_end')
|
x for x in field_ids if x not in ('time_range', 'time_range_start', 'time_range_end')
|
||||||
]
|
]
|
||||||
field_insert_index = field_ids.index('statistic') + 1
|
|
||||||
for filter_ in reversed(self.instance.statistic.filters):
|
|
||||||
filter_id = filter_['id']
|
|
||||||
choices = [(option['id'], option['label']) for option in filter_['options']]
|
|
||||||
initial = self.instance.filter_params.get(filter_id) or filter_.get('default')
|
|
||||||
|
|
||||||
required = filter_.get('required', False)
|
stat_field = self.fields['statistic']
|
||||||
if not required:
|
if not self.instance.statistic:
|
||||||
choices.insert(0, self.blank_choice)
|
stat_field.queryset = stat_field.queryset.filter(available=True)
|
||||||
|
else:
|
||||||
self.fields[filter_id] = forms.ChoiceField(
|
# display current statistic in choices even if unavailable
|
||||||
label=filter_['label'], choices=choices, required=required, initial=initial
|
stat_field.queryset = stat_field.queryset.filter(
|
||||||
|
Q(available=True) | Q(pk=self.instance.statistic.pk)
|
||||||
)
|
)
|
||||||
field_ids.insert(field_insert_index, filter_id)
|
|
||||||
|
|
||||||
# reorder so that filter fields appear after 'statistic' field
|
field_insert_index = field_ids.index('statistic') + 1
|
||||||
|
for filter_ in reversed(self.instance.statistic.filters):
|
||||||
|
filter_id = filter_['id']
|
||||||
|
choices = [(option['id'], option['label']) for option in filter_['options']]
|
||||||
|
initial = self.instance.filter_params.get(filter_id) or filter_.get('default')
|
||||||
|
|
||||||
|
required = filter_.get('required', False)
|
||||||
|
if not required:
|
||||||
|
choices.insert(0, self.blank_choice)
|
||||||
|
|
||||||
|
self.fields[filter_id] = forms.ChoiceField(
|
||||||
|
label=filter_['label'], choices=choices, required=required, initial=initial
|
||||||
|
)
|
||||||
|
field_ids.insert(field_insert_index, filter_id)
|
||||||
|
|
||||||
self.fields = OrderedDict((field_id, self.fields[field_id]) for field_id in field_ids)
|
self.fields = OrderedDict((field_id, self.fields[field_id]) for field_id in field_ids)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
|
@ -921,7 +921,12 @@ def test_chartng_cell_manager(app, admin_user, statistics):
|
||||||
|
|
||||||
app = login(app)
|
app = login(app)
|
||||||
|
|
||||||
cell = ChartNgCell(page=page, order=1, placeholder='content')
|
cell = ChartNgCell.objects.create(page=page, order=1, placeholder='content')
|
||||||
|
resp = app.get('/manage/pages/%s/' % page.id)
|
||||||
|
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
|
||||||
|
|
||||||
cell.statistic = Statistic.objects.get(slug='example')
|
cell.statistic = Statistic.objects.get(slug='example')
|
||||||
cell.save()
|
cell.save()
|
||||||
resp = app.get('/manage/pages/%s/' % page.id)
|
resp = app.get('/manage/pages/%s/' % page.id)
|
||||||
|
|
Loading…
Reference in New Issue