From adfcceb2ad95d914110cd3a52af16936be4b9002 Mon Sep 17 00:00:00 2001 From: Felipe Vieira Date: Tue, 14 Feb 2012 14:22:59 +0000 Subject: [PATCH] primeiro relatorio de diagnostico --- sigi/apps/diagnosticos/views.py | 28 ++++++++++ sigi/shortcuts.py | 18 ++++++ .../diagnosticos/diagnostico_pdf.html | 55 +++++++++++++++++++ sigi/urls.py | 5 +- 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 sigi/shortcuts.py create mode 100644 sigi/templates/diagnosticos/diagnostico_pdf.html diff --git a/sigi/apps/diagnosticos/views.py b/sigi/apps/diagnosticos/views.py index 164d491..23db7a6 100644 --- a/sigi/apps/diagnosticos/views.py +++ b/sigi/apps/diagnosticos/views.py @@ -1,10 +1,12 @@ # -*- coding: utf8 -*- +import new from django.http import HttpResponse from django.utils import simplejson from django.shortcuts import render_to_response from django.template import RequestContext from django.views.decorators.cache import never_cache +from geraldo.generators import PDFGenerator from sigi.apps.diagnosticos.urls import LOGIN_REDIRECT_URL from sigi.apps.utils.decorators import login_required @@ -13,6 +15,7 @@ from sigi.apps.diagnosticos.models import Diagnostico, Categoria from sigi.apps.casas.models import Funcionario from sigi.apps.diagnosticos.forms import (DiagnosticoMobileForm, CasaLegislativaMobileForm, FuncionariosMobileForm) +from sigi.shortcuts import render_to_pdf @never_cache @@ -185,3 +188,28 @@ def categoria_contatos(request, id_diagnostico): 'diagnostico': diagnostico, 'casa_legislativa': casa_legislativa}) return render_to_response('diagnosticos/diagnosticos_categoria_contatos_form.html', context) + +def diagnostico_pdf(request, id_diagnostico): + diagnostico = Diagnostico.objects.get(pk=id_diagnostico) + categorias = Categoria.objects.all() + + forms = [] + for categoria in categorias: + form = DiagnosticoMobileForm( + instance=diagnostico, + category=categoria.id + ) + fields = [] + for field in form: + #field.value = field.data[field.name] + fields.append(field) + forms.append((categoria,fields)) + + context = RequestContext(request, { + 'pagesize':'A4', + 'diagnostico': diagnostico, + 'forms': forms, + }) + + return render_to_response('diagnosticos/diagnostico_pdf.html', context) + diff --git a/sigi/shortcuts.py b/sigi/shortcuts.py new file mode 100644 index 0000000..64a5ff5 --- /dev/null +++ b/sigi/shortcuts.py @@ -0,0 +1,18 @@ +import cStringIO as StringIO +import ho.pisa as pisa +from django.template.loader import get_template +from django.template import Context +from django.http import HttpResponse +from cgi import escape + + +def render_to_pdf(template_src, context_dict): + template = get_template(template_src) + context = Context(context_dict) + html = template.render(context) + result = StringIO.StringIO() + + pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8')), result) + if not pdf.err: + return HttpResponse(result.getvalue(), mimetype='application/pdf') + return HttpResponse('We had some errors
%s
' % escape(html)) diff --git a/sigi/templates/diagnosticos/diagnostico_pdf.html b/sigi/templates/diagnosticos/diagnostico_pdf.html new file mode 100644 index 0000000..0b39c43 --- /dev/null +++ b/sigi/templates/diagnosticos/diagnostico_pdf.html @@ -0,0 +1,55 @@ + + + + + My Title + + + + +
+ {% for categoria, fields in forms %} +

Bloco {{categoria.nome}}

+ {% for field in fields %} +

+ {% if field.help_text %} + {{ field.label }} ({{ field.help_text }}) + {% else %} + {{ field.label }} + {% endif %} +

+

{{ field }}

+ {% endfor %} + + {% endfor %} +

+ + + diff --git a/sigi/urls.py b/sigi/urls.py index 8ac039a..b6ee01b 100644 --- a/sigi/urls.py +++ b/sigi/urls.py @@ -37,7 +37,10 @@ urlpatterns = patterns( 'sigi.apps.casas.views.casas_sem_convenio_report'), # reports convenios (r'^sigi/convenios/convenio/reports/', - 'sigi.apps.convenios.views.report'), + 'sigi.apps.convenios.views.report'), + # reports diagnosticos + (r'^sigi/diagnosticos/diagnostico/(?P\w+).pdf', + 'sigi.apps.diagnosticos.views.diagnostico_pdf'), #Carrinho Casa (r'^sigi/casas/casalegislativa/carrinho/deleta_itens_carrinho', 'sigi.apps.casas.views.deleta_itens_carrinho'),