89 lines
3.4 KiB
Python
89 lines
3.4 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 json
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django import forms
|
|
from django.conf import settings
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from .models import CubesBarChart
|
|
from .utils import get_cubes, get_cube, get_drilldown
|
|
|
|
|
|
class CubesBarChartForm(forms.ModelForm):
|
|
EMPTY = [(u'', _('None'))]
|
|
|
|
class Meta:
|
|
model = CubesBarChart
|
|
fields = ('title', 'url', 'cube', 'aggregate1', 'drilldown1', 'drilldown2',
|
|
'other_parameters')
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(CubesBarChartForm, self).__init__(*args, **kwargs)
|
|
for field in ('cube', 'aggregate1', 'drilldown1', 'drilldown2'):
|
|
self.fields[field] = forms.ChoiceField(
|
|
label=self.fields[field].label,
|
|
initial=self.fields[field].initial,
|
|
required=False,
|
|
choices=self.EMPTY)
|
|
if getattr(settings, 'CUBES_URL', None):
|
|
cube_choices = self.get_cubes_choices()
|
|
if cube_choices:
|
|
self.fields['cube'].choices = cube_choices
|
|
aggregate1_choices = self.get_aggregate_choices()
|
|
if aggregate1_choices:
|
|
self.fields['aggregate1'].choices = aggregate1_choices
|
|
drilldown_choices = self.get_drilldown_choices()
|
|
if drilldown_choices:
|
|
self.fields['drilldown1'].choices = drilldown_choices
|
|
self.fields['drilldown2'].choices = drilldown_choices
|
|
|
|
def clean_other_parameters(self):
|
|
other_parameters = self.cleaned_data['other_parameters']
|
|
if other_parameters:
|
|
try:
|
|
decoded = json.loads(other_parameters)
|
|
assert isinstance(decoded, dict)
|
|
for key, value in decoded.iteritems():
|
|
assert isinstance(key, unicode)
|
|
assert isinstance(value, unicode)
|
|
except (ValueError, AssertionError):
|
|
raise ValidationError(_('Other parameter must be a JSON object containing only '
|
|
'strings'))
|
|
return other_parameters
|
|
|
|
def get_cubes_choices(self):
|
|
cubes = get_cubes()
|
|
return self.EMPTY + [(cube['name'], cube.get('label')) for cube in cubes]
|
|
|
|
def get_aggregate_choices(self):
|
|
cube = self.instance.cube
|
|
if cube:
|
|
cube = get_cube(cube)
|
|
if cube:
|
|
return self.EMPTY + [(ag['name'], ag['label']) for ag in cube.get('aggregates', [])]
|
|
return []
|
|
|
|
def get_drilldown_choices(self):
|
|
cube = self.instance.cube
|
|
if cube:
|
|
choices = get_drilldown(cube)
|
|
if choices:
|
|
return self.EMPTY + choices
|
|
return []
|