56 |
56 |
|
57 |
57 |
def __init__(self, *args, **kwargs):
|
58 |
58 |
super().__init__(*args, **kwargs)
|
59 |
|
stat_field = self.fields['statistic']
|
60 |
|
if not self.instance.statistic:
|
61 |
|
stat_field.queryset = stat_field.queryset.filter(available=True)
|
62 |
|
return
|
63 |
|
|
64 |
|
# display current statistic in choices even if unavailable
|
65 |
|
stat_field.queryset = stat_field.queryset.filter(Q(available=True) | Q(pk=self.instance.statistic.pk))
|
66 |
59 |
|
67 |
60 |
field_ids = list(self._meta.fields)
|
68 |
|
if self.instance.statistic.service_slug == 'bijoe':
|
|
61 |
if not self.instance.statistic or self.instance.statistic.service_slug == 'bijoe':
|
69 |
62 |
field_ids = [
|
70 |
63 |
x for x in field_ids if x not in ('time_range', 'time_range_start', 'time_range_end')
|
71 |
64 |
]
|
72 |
|
field_insert_index = field_ids.index('statistic') + 1
|
73 |
|
for filter_ in reversed(self.instance.statistic.filters):
|
74 |
|
filter_id = filter_['id']
|
75 |
|
choices = [(option['id'], option['label']) for option in filter_['options']]
|
76 |
|
initial = self.instance.filter_params.get(filter_id) or filter_.get('default')
|
77 |
|
|
78 |
|
required = filter_.get('required', False)
|
79 |
|
if not required:
|
80 |
|
choices.insert(0, self.blank_choice)
|
81 |
|
|
82 |
|
self.fields[filter_id] = forms.ChoiceField(
|
83 |
|
label=filter_['label'], choices=choices, required=required, initial=initial
|
|
65 |
|
|
66 |
stat_field = self.fields['statistic']
|
|
67 |
if not self.instance.statistic:
|
|
68 |
stat_field.queryset = stat_field.queryset.filter(available=True)
|
|
69 |
else:
|
|
70 |
# display current statistic in choices even if unavailable
|
|
71 |
stat_field.queryset = stat_field.queryset.filter(
|
|
72 |
Q(available=True) | Q(pk=self.instance.statistic.pk)
|
84 |
73 |
)
|
85 |
|
field_ids.insert(field_insert_index, filter_id)
|
86 |
74 |
|
87 |
|
# reorder so that filter fields appear after 'statistic' field
|
|
75 |
field_insert_index = field_ids.index('statistic') + 1
|
|
76 |
for filter_ in reversed(self.instance.statistic.filters):
|
|
77 |
filter_id = filter_['id']
|
|
78 |
choices = [(option['id'], option['label']) for option in filter_['options']]
|
|
79 |
initial = self.instance.filter_params.get(filter_id) or filter_.get('default')
|
|
80 |
|
|
81 |
required = filter_.get('required', False)
|
|
82 |
if not required:
|
|
83 |
choices.insert(0, self.blank_choice)
|
|
84 |
|
|
85 |
self.fields[filter_id] = forms.ChoiceField(
|
|
86 |
label=filter_['label'], choices=choices, required=required, initial=initial
|
|
87 |
)
|
|
88 |
field_ids.insert(field_insert_index, filter_id)
|
|
89 |
|
88 |
90 |
self.fields = OrderedDict((field_id, self.fields[field_id]) for field_id in field_ids)
|
89 |
91 |
|
90 |
92 |
def save(self, *args, **kwargs):
|