Browse Source

primeira versao da api de graficos

stable/1.0
Felipe Vieira 13 years ago
parent
commit
178f78b144
  1. 16
      sigi/apps/diagnosticos/models.py
  2. 62
      sigi/apps/diagnosticos/views.py
  3. 6
      sigi/urls.py

16
sigi/apps/diagnosticos/models.py

@ -155,6 +155,22 @@ class Pergunta(BaseSchema):
""" """
categoria = models.ForeignKey(Categoria, related_name='perguntas') categoria = models.ForeignKey(Categoria, related_name='perguntas')
def group_choices(self):
from django.db import connection, transaction
cursor = connection.cursor()
cursor.execute("""
SELECT choice_id, sum(1)
FROM diagnosticos_resposta
WHERE schema_id=%s
GROUP BY choice_id;
""", [self.id])
return [
(Escolha.objects.get(id=int(row[0])), row[1])
for row in cursor.fetchall()
]
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
verbose_name, verbose_name_plural = 'pergunta', 'perguntas' verbose_name, verbose_name_plural = 'pergunta', 'perguntas'

62
sigi/apps/diagnosticos/views.py

@ -1,9 +1,9 @@
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
import new import new
from django.http import HttpResponse from django.http import HttpResponse, QueryDict
from django.utils import simplejson from django.utils import simplejson
from django.shortcuts import render_to_response from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext from django.template import RequestContext
from django.views.decorators.cache import never_cache from django.views.decorators.cache import never_cache
from geraldo.generators import PDFGenerator from geraldo.generators import PDFGenerator
@ -11,7 +11,7 @@ from geraldo.generators import PDFGenerator
from sigi.apps.diagnosticos.urls import LOGIN_REDIRECT_URL from sigi.apps.diagnosticos.urls import LOGIN_REDIRECT_URL
from sigi.apps.utils.decorators import login_required from sigi.apps.utils.decorators import login_required
from sigi.apps.diagnosticos.decorators import validate_diagnostico from sigi.apps.diagnosticos.decorators import validate_diagnostico
from sigi.apps.diagnosticos.models import Diagnostico, Categoria from sigi.apps.diagnosticos.models import Diagnostico, Categoria, Pergunta
from sigi.apps.casas.models import Funcionario from sigi.apps.casas.models import Funcionario
from sigi.apps.diagnosticos.forms import (DiagnosticoMobileForm, from sigi.apps.diagnosticos.forms import (DiagnosticoMobileForm,
CasaLegislativaMobileForm, FuncionariosMobileForm) CasaLegislativaMobileForm, FuncionariosMobileForm)
@ -211,7 +211,6 @@ def diagnostico_pdf(request, id_diagnostico):
schema.value = data schema.value = data
schemas.append(schema) schemas.append(schema)
schemas = sorted(schemas, key=lambda schema: schema.title) # sort by title
schemas_by_categoria.append((categoria,schemas)) schemas_by_categoria.append((categoria,schemas))
context = RequestContext(request, { context = RequestContext(request, {
@ -224,3 +223,58 @@ def diagnostico_pdf(request, id_diagnostico):
return render_to_pdf('diagnosticos/diagnostico_pdf.html', context) return render_to_pdf('diagnosticos/diagnostico_pdf.html', context)
def graficos(request):
categorias = Categoria.objects.all()
sel_categoria = int(request.REQUEST.get("categoria","3"))
perguntas = Pergunta.objects.filter(categoria=sel_categoria).all()
context = RequestContext(request, {
'categorias': categorias,
'sel_categoria': sel_categoria,
'perguntas': perguntas,
})
return render_to_response('diagnosticos/graficos.html',
context)
def grafico_api(request):
graph_url = "http://chart.apis.google.com/chart"
graph_params = QueryDict("chxt=y&chbh=a&chco=A2C180,3D7930")
graph_params = graph_params.copy() # to make it mutable
width = request.REQUEST.get('width', '300')
height = request.REQUEST.get('height', '200')
graph_params.update({'chs': width + 'x' + height})
pergunta_slug = request.REQUEST.get('id', None)
pergunta = get_object_or_404(Pergunta, name=pergunta_slug)
if pergunta.datatype == 'many':
choices = [r[1] for r in pergunta.get_choices()]
graph_params.update({
'cht': 'bvg',
'chxt' : 'y',
'chd': 't:' + ",".join(choices)
})
elif pergunta.datatype == 'one':
choices = [str(r[1]) for r in pergunta.group_choices()]
graph_params.update({
'cht': 'p',
'chd': 't:' + ",".join(choices)
})
response = {
"version": "1.0",
"type": "photo",
"width": width,
"height": height,
"title": pergunta.title,
"url": graph_url + "?" + graph_params.urlencode(),
"provider_name": "SIGI",
"provider_url": "https://intranet.interlegis.gov.br/sigi/"
}
json = simplejson.dumps(response)
return HttpResponse(json, mimetype="application/json")
return redirect(response['url'])

6
sigi/urls.py

@ -76,7 +76,11 @@ urlpatterns = patterns(
# Menu com Birt reports # Menu com Birt reports
(r'^sigi/birt/', (r'^sigi/birt/',
'sigi.apps.birt.views.menu'), 'sigi.apps.birt.views.menu'),
# graficos de perguntas
(r'^sigi/diagnosticos/graficos/$',
'sigi.apps.diagnosticos.views.graficos'),
(r'^sigi/diagnosticos/grafico_api/$',
'sigi.apps.diagnosticos.views.grafico_api'),
# automatic interface based on admin # automatic interface based on admin
(r'^sigi/(.*)', sites.default.root), (r'^sigi/(.*)', sites.default.root),
) )

Loading…
Cancel
Save