combo/combo/apps/dataviz/forms.py

320 lines
12 KiB
Python

# combo - content management system
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from collections import OrderedDict
from django import forms
from django.conf import settings
from django.db import transaction
from django.db.models import Q
from django.db.models.fields import BLANK_CHOICE_DASH
from django.template import Context, Template, TemplateSyntaxError, VariableDoesNotExist
from django.utils.translation import ugettext_lazy as _
from combo.utils import cache_during_request, requests, spooler
from .models import ChartCell, ChartNgCell
class ChartForm(forms.ModelForm):
class Meta:
model = ChartCell
fields = ('title', 'url')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
available_charts = []
for site_dict in settings.KNOWN_SERVICES.get('bijoe').values():
result = requests.get(
'/visualization/json/',
remote_service=site_dict,
without_user=True,
headers={'accept': 'application/json'},
).json()
available_charts.extend([(x['path'], x['name']) for x in result])
available_charts.sort(key=lambda x: x[1])
self.fields['url'].widget = forms.Select(choices=available_charts)
@cache_during_request
def trigger_statistics_list_refresh():
transaction.on_commit(spooler.refresh_statistics_list)
class ChartFiltersMixin:
time_intervals = (
('week', _('Week')),
('month', _('Month')),
('year', _('Year')),
('weekday', _('Week day')),
)
def get_filter_fields(self, cell):
fields = OrderedDict()
for filter_ in cell.available_filters:
filter_id = filter_['id']
if filter_.get('deprecated') and (
filter_id not in cell.filter_params
or cell.filter_params.get(filter_id) == filter_.get('default')
):
continue
has_option_groups = isinstance(filter_['options'][0], list)
if filter_['options'] and has_option_groups:
choices = [
(group, [(opt['id'], opt['label']) for opt in options])
for group, options in filter_['options']
]
else:
choices = [(option['id'], option['label']) for option in filter_['options']]
initial = cell.filter_params.get(filter_id, filter_.get('default'))
if filter_id == 'time_interval':
self.extend_time_interval_choices(choices)
required = filter_.get('required', False)
multiple = filter_.get('multiple')
if not required and not multiple:
choices = BLANK_CHOICE_DASH + choices
extra_variables = cell.page.get_extra_variables_keys()
variable_choices = [('variable:' + key, key) for key in extra_variables]
if has_option_groups:
possible_choices = {choice[0] for _, group_choices in choices for choice in group_choices}
else:
possible_choices = {choice[0] for choice in choices}
for choice in initial if isinstance(initial, list) else [initial]:
if not choice:
continue
if choice.startswith('variable:'):
variable = choice.replace('variable:', '')
if not variable in extra_variables:
variable_choices.append((choice, _('%s (unavailable)') % variable))
elif choice not in possible_choices:
choices.append((choice, _('%s (unavailable)') % choice))
if variable_choices and not multiple and filter_id != 'time_interval':
choices.append((_('Page variables'), variable_choices))
field_class = forms.MultipleChoiceField if multiple else forms.ChoiceField
fields[filter_id] = field_class(
label=filter_['label'], choices=choices, required=required, initial=initial
)
if filter_.get('deprecated'):
fields[filter_id].label += ' (%s)' % _('deprecated')
fields[filter_id].help_text = filter_.get('deprecation_hint')
fields[filter_id].is_filter_field = True
return fields
def extend_time_interval_choices(self, choices):
choice_ids = {choice_id for choice_id, _ in choices}
if 'day' in choice_ids:
for choice in self.time_intervals:
if choice[0] not in choice_ids:
choices.append(choice)
def update_time_range_choices(self, statistic, exclude_template_choice=False):
choices = self.fields['time_range'].choices
if not statistic.has_future_data:
choices = [choice for choice in choices if not choice[0].startswith('next')]
if exclude_template_choice:
choices = [choice for choice in choices if choice[0] != 'range-template']
self.fields['time_range'].choices = choices
class ChartNgForm(ChartFiltersMixin, forms.ModelForm):
class Meta:
model = ChartNgCell
fields = (
'title',
'statistic',
'time_range',
'time_range_start',
'time_range_end',
'time_range_start_template',
'time_range_end_template',
'chart_type',
'height',
'sort_order',
'hide_null_values',
)
widgets = {
'time_range_start': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
'time_range_end': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
}
def __init__(self, *args, **kwargs):
trigger_statistics_list_refresh()
super().__init__(*args, **kwargs)
stat_field = self.fields['statistic']
if not self.instance.statistic:
stat_field.queryset = stat_field.queryset.filter(available=True)
else:
# display current statistic in choices even if unavailable
stat_field.queryset = stat_field.queryset.filter(
Q(available=True) | Q(pk=self.instance.statistic.pk)
)
self.add_filter_fields()
self.update_time_range_choices(self.instance.statistic)
if not self.instance.statistic or self.instance.statistic.service_slug == 'bijoe':
for field in (
'time_range',
'time_range_start',
'time_range_end',
'time_range_start_template',
'time_range_end_template',
):
del self.fields[field]
def add_filter_fields(self):
new_fields = OrderedDict()
for field_name, field in self.fields.items():
new_fields[field_name] = field
if field_name == 'statistic':
# insert filter fields after statistic field
new_fields.update(self.get_filter_fields(self.instance))
self.fields = new_fields
def save(self, *args, **kwargs):
if 'statistic' in self.changed_data:
self.instance.filter_params.clear()
self.instance.time_range = ''
for filter_ in self.instance.available_filters:
if 'default' in filter_:
self.instance.filter_params[filter_['id']] = filter_['default']
else:
for filter_ in self.instance.available_filters:
if filter_['id'] in self.cleaned_data:
self.instance.filter_params[filter_['id']] = self.cleaned_data[filter_['id']]
cell = super().save(*args, **kwargs)
for filter_ in cell.available_filters:
if filter_.get('has_subfilters') and filter_['id'] in self.changed_data:
cell.update_subfilters()
self.fields = OrderedDict(
(name, field)
for name, field in self.fields.items()
if not hasattr(field, 'is_filter_field')
)
self.add_filter_fields()
break
return cell
def clean(self):
for template_field in ('time_range_start_template', 'time_range_end_template'):
if not self.cleaned_data.get(template_field):
continue
context = {'now': datetime.datetime.now, 'today': datetime.datetime.now}
try:
Template('{{ %s|date:"Y-m-d" }}' % self.cleaned_data[template_field]).render(Context(context))
except (VariableDoesNotExist, TemplateSyntaxError) as e:
self.add_error(template_field, e)
class ChartNgPartialForm(ChartFiltersMixin, forms.ModelForm):
class Meta:
model = ChartNgCell
fields = (
'time_range',
'time_range_start',
'time_range_end',
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields.update(self.get_filter_fields(self.instance))
for field in self.fields.values():
field.required = False
def clean(self):
for field in self._meta.fields:
if 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:
self.instance.filter_params[filter_['id']] = self.cleaned_data.get(filter_['id'])
class ChartFiltersForm(ChartFiltersMixin, forms.ModelForm):
class Meta:
model = ChartNgCell
fields = (
'time_range',
'time_range_start',
'time_range_end',
)
widgets = {
'time_range_start': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
'time_range_end': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
}
def __init__(self, *args, **kwargs):
page = kwargs.pop('page')
super().__init__(*args, **kwargs)
chart_cells = list(ChartNgCell.objects.filter(page=page, statistic__isnull=False).order_by('order'))
if not chart_cells:
self.fields.clear()
return
first_cell = chart_cells[0]
for field in self._meta.fields:
self.fields[field].initial = getattr(first_cell, field)
dynamic_fields = self.get_filter_fields(first_cell)
dynamic_fields_values = {k: v for k, v in first_cell.filter_params.items()}
self.update_time_range_choices(first_cell.statistic, exclude_template_choice=True)
for cell in chart_cells[1:]:
cell_filter_fields = self.get_filter_fields(cell)
# keep only common fields
dynamic_fields = {k: v for k, v in dynamic_fields.items() if k in cell_filter_fields}
# keep only same value fields
for field, value in cell.filter_params.items():
if field in dynamic_fields and value != dynamic_fields_values.get(field):
del dynamic_fields[field]
if cell.time_range != first_cell.time_range:
for field in self._meta.fields:
self.fields.pop(field, None)
# ensure compatible choices lists
for field_name, field in cell_filter_fields.items():
if field_name in dynamic_fields:
dynamic_fields[field_name].choices = [
x for x in dynamic_fields[field_name].choices if x in field.choices
]
if dynamic_fields[field_name].choices == []:
del dynamic_fields[field_name]
if 'time_range' in self.fields:
self.update_time_range_choices(cell.statistic)
self.fields.update(dynamic_fields)