From 4473e9d86574fa00066fd5af817a1c5b3d9269c5 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Tue, 5 May 2015 12:22:57 -0300 Subject: [PATCH] Use pentaho for Saberes dashboards --- requirements/requirements.txt | 1 + sigi/apps/home/templatetags/menu_conf.yaml | 9 ++- .../saberes/templates/saberes/dashboard.html | 16 +---- sigi/apps/saberes/urls.py | 8 ++- sigi/apps/saberes/views.py | 61 +++++++++++++------ sigi/urls.py | 10 ++- sigiStatic/css/base_site.css | 7 +-- 7 files changed, 68 insertions(+), 44 deletions(-) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 3ed49bd..6059784 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -16,4 +16,5 @@ psycopg2==2.5.4 python-memcached==1.53 PyYAML==3.11 reportlab==2.7 +requests==2.7.0 six==1.9.0 diff --git a/sigi/apps/home/templatetags/menu_conf.yaml b/sigi/apps/home/templatetags/menu_conf.yaml index 57d648d..b778bec 100644 --- a/sigi/apps/home/templatetags/menu_conf.yaml +++ b/sigi/apps/home/templatetags/menu_conf.yaml @@ -1,6 +1,4 @@ main_menu: - - title: Gráficos - url: '' - title: Municípios url: contatos/municipio/ children: @@ -95,3 +93,10 @@ main_menu: children: - title: Desembolsos url: financeiro/desembolso/ + - title: Saberes + url: '/' + children: + - title: Dashboard + url: saberes/dashboard/ + - title: Cursos sem Tutoria + url: saberes/dashboard/cursos-sem-turoria/ diff --git a/sigi/apps/saberes/templates/saberes/dashboard.html b/sigi/apps/saberes/templates/saberes/dashboard.html index 209b1c4..430c73f 100644 --- a/sigi/apps/saberes/templates/saberes/dashboard.html +++ b/sigi/apps/saberes/templates/saberes/dashboard.html @@ -1,20 +1,10 @@ {% extends "admin/base_site.html" %} -{% block extrastyle %} +{% block extrahead %} {{ block.super }} - + {{ headers|safe }} {% endblock %} {% block content %} -
- {% include "saberes/snippets.html" %} -
+ {{ content|safe }} {% endblock %} diff --git a/sigi/apps/saberes/urls.py b/sigi/apps/saberes/urls.py index 07fcc51..43927ab 100644 --- a/sigi/apps/saberes/urls.py +++ b/sigi/apps/saberes/urls.py @@ -1,9 +1,13 @@ # coding: utf-8 from django.conf.urls import patterns, url +from .views import cursos_sem_tutoria, dashboard, pentaho_proxy + + urlpatterns = patterns( 'sigi.apps.saberes.views', - url(r'^dashboard/$', 'dashboard', name="saberes-dashboard-view"), - url(r'^dashboard/(?P\w+)/$', 'detail', name="saberes-dashboard-detail"), + url(r'^dashboard/cursos-sem-turoria/?$', cursos_sem_tutoria, name="saberes-dashboard-detail"), + url(r'^dashboard/?$', dashboard, name="saberes-dashboard-view"), + url(r'^(?P(plugin|api)/.*)$', pentaho_proxy), ) diff --git a/sigi/apps/saberes/views.py b/sigi/apps/saberes/views.py index 14e8468..0007d24 100644 --- a/sigi/apps/saberes/views.py +++ b/sigi/apps/saberes/views.py @@ -1,31 +1,56 @@ # -*- coding: utf-8 -*- - from collections import OrderedDict -from django.utils.translation import ugettext as _ -from django.db.models import Sum, Avg + +import requests +from django.http import HttpResponse from django.shortcuts import render, render_to_response, get_object_or_404 from django.template import RequestContext -from sigi.apps.mdl.models import User, CourseStats -from sigi.apps.saberes.models import CategoriasInteresse, PainelItem +from django.utils.translation import ugettext as _ +from django.views.decorators.csrf import csrf_exempt +from requests.auth import HTTPBasicAuth + +from sigi.apps.mdl.models import CourseStats +from sigi.apps.saberes.models import CategoriasInteresse +from sigi.settings import PENTAHO_SERVER, PENTAHO_DASHBOARDS, PENTAHO_USERNAME_PASSWORD + + +PENTAHO_CDF_URL = 'http://%s/pentaho/plugin/pentaho-cdf-dd/api/renderer/' % PENTAHO_SERVER + + +def get_dashboard_parts(dashboard_id, this_host): + params = PENTAHO_DASHBOARDS[dashboard_id] + params['root'] = this_host + return [requests.get(PENTAHO_CDF_URL + method, + params=params, auth=HTTPBasicAuth(*PENTAHO_USERNAME_PASSWORD)).content + for method in ('getHeaders', 'getContent')] + + +def make_dashboard(dashboard_id, adjust_content=lambda x: x): + def view(request): + headers, content = get_dashboard_parts(dashboard_id, request.META['HTTP_HOST']) + content = adjust_content(content) + return render(request, 'saberes/dashboard.html', + dict(headers=headers, content=content)) + return view -def dashboard(request): - paineis = OrderedDict() +def use_to_container_fluid(content): + return content.replace("class='container'", "class='container-fluid'") - for p in PainelItem.objects.all(): - if p.painel not in paineis: - paineis[p.painel] = {'titulo': p.painel, 'dados': []} - paineis[p.painel]['dados'].append(p) - for p in paineis: - try: - paineis[p]['area'] = CategoriasInteresse.objects.get(descricao=paineis[p]['titulo']) - except: - pass +dashboard = make_dashboard('saberes-geral') +cursos_sem_tutoria = make_dashboard('saberes-cursos-sem-tutoria', use_to_container_fluid) - extra_context = {'paineis': paineis} - return render_to_response('saberes/dashboard.html', extra_context, context_instance=RequestContext(request)) +@csrf_exempt +def pentaho_proxy(request, path): + url = 'http://%s/pentaho/%s' % (PENTAHO_SERVER, path) + params = request.GET or request.POST + auth = HTTPBasicAuth(*PENTAHO_USERNAME_PASSWORD) + response = requests.get(url, params=params, auth=auth) + return HttpResponse(response.content, + status=response.status_code, + content_type=response.headers.get('Content-Type')) def detail(request, area): diff --git a/sigi/urls.py b/sigi/urls.py index 14018ab..1ccef90 100644 --- a/sigi/urls.py +++ b/sigi/urls.py @@ -1,10 +1,13 @@ #-*- coding:utf-8 -*- -from django.conf.urls import patterns, include, url -from django.views.generic.base import TemplateView from django.conf import settings +from django.conf.urls import patterns, include, url from django.conf.urls.static import static - from django.contrib import admin +from django.views.generic.base import TemplateView + +from sigi.apps.saberes.views import pentaho_proxy + + admin.site.index_template = 'index.html' admin.autodiscover() @@ -19,6 +22,7 @@ urlpatterns = patterns( url(r'^servicos/', include('sigi.apps.servicos.urls')), url(r'^saberes/', include('sigi.apps.saberes.urls')), url(r'^dashboard/', include('sigi.apps.metas.urls')), + url(r'^pentaho/(?P(plugin|api)/.*)$', pentaho_proxy), url(r'^', include(admin.site.urls)), diff --git a/sigiStatic/css/base_site.css b/sigiStatic/css/base_site.css index 96425a7..84efad4 100644 --- a/sigiStatic/css/base_site.css +++ b/sigiStatic/css/base_site.css @@ -6,11 +6,6 @@ a:hover { color: #11488d; } -/* HEADER */ -#header { - background: #003351 url(../img/default-bg2.jpg); -} - #site-name a:hover { text-decoration: none; } @@ -237,4 +232,4 @@ h1 { /*Fixing Google Maps API conflict with bootstrap. */ .container-flex > div[class*='col-'] #map div, .row-flex > div[class*='col-'] #map div { width: initial; -} \ No newline at end of file +}