From 3580747fe6180fb989aa72e56849a5d522dc57e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 29 Aug 2016 18:14:27 +0200 Subject: [PATCH] dataviz: add support for including bijoe graphs (#12744) --- combo/apps/dataviz/forms.py | 21 ++++++++++++- combo/apps/dataviz/migrations/0006_chartcell.py | 35 ++++++++++++++++++++++ combo/apps/dataviz/models.py | 29 ++++++++++++++++++ .../dataviz/templates/combo/dataviz-chart.html | 3 ++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 combo/apps/dataviz/migrations/0006_chartcell.py create mode 100644 combo/apps/dataviz/templates/combo/dataviz-chart.html diff --git a/combo/apps/dataviz/forms.py b/combo/apps/dataviz/forms.py index 502b878..0063afc 100644 --- a/combo/apps/dataviz/forms.py +++ b/combo/apps/dataviz/forms.py @@ -21,10 +21,29 @@ from django import forms from django.conf import settings from django.core.exceptions import ValidationError -from .models import BaseCubesChart +from combo.utils import requests + +from .models import BaseCubesChart, ChartCell from .utils import get_cubes, get_cube, get_drilldown +class ChartForm(forms.ModelForm): + class Meta: + model = ChartCell + fields = ('title', 'url') + + def __init__(self, *args, **kwargs): + super(ChartForm, self).__init__(*args, **kwargs) + available_charts = [] + for site_key, site_dict in settings.KNOWN_SERVICES.get('bijoe').items(): + 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) + + class CubesBarChartForm(forms.ModelForm): EMPTY = [(u'', _('None'))] diff --git a/combo/apps/dataviz/migrations/0006_chartcell.py b/combo/apps/dataviz/migrations/0006_chartcell.py new file mode 100644 index 0000000..70f00b4 --- /dev/null +++ b/combo/apps/dataviz/migrations/0006_chartcell.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('data', '0020_auto_20160928_1152'), + ('auth', '0006_require_contenttypes_0002'), + ('dataviz', '0005_auto_20160928_1152'), + ] + + operations = [ + migrations.CreateModel( + name='ChartCell', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('placeholder', models.CharField(max_length=20)), + ('order', models.PositiveIntegerField()), + ('slug', models.SlugField(verbose_name='Slug', blank=True)), + ('extra_css_class', models.CharField(max_length=100, verbose_name='Extra classes for CSS styling', blank=True)), + ('public', models.BooleanField(default=True, verbose_name='Public')), + ('restricted_to_unlogged', models.BooleanField(default=False, verbose_name='Restrict to unlogged users')), + ('title', models.CharField(max_length=150, null=True, verbose_name='Title', blank=True)), + ('url', models.URLField(max_length=150, null=True, verbose_name='URL', blank=True)), + ('groups', models.ManyToManyField(to='auth.Group', verbose_name='Groups', blank=True)), + ('page', models.ForeignKey(to='data.Page')), + ], + options={ + 'verbose_name': 'Chart', + }, + ), + ] diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 2641417..b75d6bb 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -61,6 +61,35 @@ class Gauge(CellBase): } +@register_cell_class +class ChartCell(CellBase): + template_name = 'combo/dataviz-chart.html' + + title = models.CharField(_('Title'), max_length=150, blank=True, null=True) + url = models.URLField(_('URL'), max_length=150, blank=True, null=True) + + class Meta: + verbose_name = _('Chart') + + @classmethod + def is_enabled(self): + return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('bijoe') + + def get_default_form_class(self): + from .forms import ChartForm + return ChartForm + + def get_additional_label(self): + if self.title: + return self.title + return '' + + def get_cell_extra_context(self, context): + context = super(ChartCell, self).get_cell_extra_context(context) + context['title'] = self.title + context['url'] = self.url + return context + class BaseCubesChart(CellBase): title = models.CharField(_('Title'), max_length=150, blank=True, null=True) url = models.URLField(_('URL'), max_length=150, blank=True, null=True) diff --git a/combo/apps/dataviz/templates/combo/dataviz-chart.html b/combo/apps/dataviz/templates/combo/dataviz-chart.html new file mode 100644 index 0000000..7a45c80 --- /dev/null +++ b/combo/apps/dataviz/templates/combo/dataviz-chart.html @@ -0,0 +1,3 @@ +{% if title %}

{{title}}

{% endif %} + -- 2.9.3