From ca7d2345888c45ca4db5ab787f032ee92317527f Mon Sep 17 00:00:00 2001 From: "Starlone (Estagiario)" Date: Fri, 30 Jul 2010 19:48:59 +0000 Subject: [PATCH] Criando relatorio de casas --- sigi/apps/casas/admin.py | 12 ++- sigi/apps/casas/reports.py | 87 ++++++++++++++++++- .../casas/templates/casas/change_list.html | 13 +-- sigi/apps/casas/views.py | 22 ++++- 4 files changed, 124 insertions(+), 10 deletions(-) diff --git a/sigi/apps/casas/admin.py b/sigi/apps/casas/admin.py index ee32d14..bf13ef1 100644 --- a/sigi/apps/casas/admin.py +++ b/sigi/apps/casas/admin.py @@ -6,7 +6,7 @@ from sigi.apps.casas.models import CasaLegislativa from sigi.apps.contatos.models import Contato, Telefone from sigi.apps.convenios.models import Projeto, Convenio, EquipamentoPrevisto, Anexo from django.http import HttpResponse, HttpResponseRedirect -from sigi.apps.casas.reports import CasasLegislativasLabels +from sigi.apps.casas.reports import CasasLegislativasLabels, CasasLegislativasReport from geraldo.generators import PDFGenerator class ContatosInline(generic.GenericTabularInline): @@ -26,11 +26,12 @@ class CasaLegislativaAdmin(admin.ModelAdmin): form = CasaLegislativaForm change_form_template = 'casas/change_form.html' change_list_template = 'casas/change_list.html' - actions = ['delete_selected','etiqueta'] + actions = ['delete_selected','etiqueta','relatorio'] inlines = (TelefonesInline, ContatosInline, ConveniosInline) list_display = ('nome', 'email', 'pagina_web', 'municipio') list_display_links = ('nome',) list_filter = ('tipo', 'municipio') + ordering = ('municipio__uf','nome') fieldsets = ( (None, { 'fields': ('nome', 'sigla', 'tipo', 'cnpj', 'observacoes', @@ -62,4 +63,11 @@ class CasaLegislativaAdmin(admin.ModelAdmin): return response etiqueta.short_description = "Gerar etiqueta(s) da(s) casa(s) selecionada(s)" + def relatorio(modelAdmin,request,queryset): + response = HttpResponse(mimetype='application/pdf') + report = CasasLegislativasReport(queryset=queryset) + report.generate_by(PDFGenerator, filename=response) + return response + relatorio.short_description = u"Gerar relatório(s) da(s) casa(s) selecionada(s)" + admin.site.register(CasaLegislativa, CasaLegislativaAdmin) diff --git a/sigi/apps/casas/reports.py b/sigi/apps/casas/reports.py index 3b3b854..ac65f11 100644 --- a/sigi/apps/casas/reports.py +++ b/sigi/apps/casas/reports.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- from reportlab.lib.pagesizes import A4 from reportlab.lib.units import cm -from geraldo import Report, DetailBand, Label, ObjectValue, ManyElements +from geraldo import Report, DetailBand, Label, ObjectValue, ManyElements, \ + ReportGroup, ReportBand + +from sigi.apps.relatorios.reports import ReportDefault class CasasLegislativasLabels(Report): """ @@ -44,3 +47,85 @@ class CasasLegislativasLabels(Report): start_top=1.5*cm, height=0.5*cm, left=1*cm, width=9.4*cm, ), ] + +class CasasLegislativasReport(ReportDefault): + title = u'Relatório de Casas Legislativas' + + class band_page_header(ReportDefault.band_page_header): + + label_top = ReportDefault.band_page_header.label_top + label_left = [0.3,1,5.5,6.5,12] + elements = list(ReportDefault.band_page_header.elements) + + elements += [ + Label( + text="UF", + left=label_left[0]*cm, + top=label_top + ), + Label( + text="Municipio", + left=label_left[1]*cm, + top=label_top + ), + Label( + text="Tipo", + left=label_left[2]*cm, + top=label_top + ), + Label( + text="Presidente", + left=label_left[3]*cm, + top=label_top + ), + Label( + text="Logradouro", + left=label_left[4]*cm, + top=label_top + ), + + ] + + + + class band_page_footer(ReportDefault.band_page_footer): + pass + + class band_detail(ReportDefault.band_detail): + + label_left = [0.3,1,5.5,6.5,12] + + elements=[ + ObjectValue( + attribute_name='municipio.uf.sigla', + left=label_left[0]*cm + ), + ObjectValue( + attribute_name='municipio.nome', + left=label_left[1]*cm + ), + ObjectValue( + attribute_name='tipo', + left=label_left[2]*cm + ), + ObjectValue( + attribute_name='parlamentar', + left=label_left[3]*cm + ), + ObjectValue( + attribute_name='logradouro', + left=label_left[4]*cm, + ), + ] + + groups = [ + ReportGroup(attribute_name='municipio.uf', + band_header=ReportBand( + height=0.7*cm, + elements= [ + ObjectValue(attribute_name='municipio.uf') + ], + borders={'top': True}, + ) + ) + ] \ No newline at end of file diff --git a/sigi/apps/casas/templates/casas/change_list.html b/sigi/apps/casas/templates/casas/change_list.html index 2ae6d77..8ddb5ef 100644 --- a/sigi/apps/casas/templates/casas/change_list.html +++ b/sigi/apps/casas/templates/casas/change_list.html @@ -4,12 +4,13 @@ {% block object-tools %} {% if has_add_permission %} {% endif %} {% endblock %} diff --git a/sigi/apps/casas/views.py b/sigi/apps/casas/views.py index e798b5c..a8b9fd5 100644 --- a/sigi/apps/casas/views.py +++ b/sigi/apps/casas/views.py @@ -2,6 +2,8 @@ from django.http import HttpResponse, HttpResponseRedirect from geraldo.generators import PDFGenerator from sigi.apps.casas.models import CasaLegislativa from sigi.apps.casas.reports import CasasLegislativasLabels +from sigi.apps.casas.reports import CasasLegislativasReport + def labels_report(request, id=None): """ TODO: adicionar suporte para resultado de pesquisa do admin. @@ -15,8 +17,26 @@ def labels_report(request, id=None): kwargs[str(k)] = v qs = qs.filter(**kwargs) if not qs: - return HttpResponseRedirect('../') + return HttpResponseRedirect('../') response = HttpResponse(mimetype='application/pdf') report = CasasLegislativasLabels(queryset=qs) report.generate_by(PDFGenerator, filename=response) return response + +def report(request, id=None): + qs = CasaLegislativa.objects.all().order_by('municipio__uf','nome') + if id: + qs = qs.filter(pk=id) + elif request.GET: + kwargs = {} + for k, v in request.GET.iteritems(): + kwargs[str(k)] = v + qs = qs.filter(**kwargs) + if not qs: + return HttpResponseRedirect('../') + + #qs.order_by('municipio__uf','nome') + response = HttpResponse(mimetype='application/pdf') + report = CasasLegislativasReport(queryset=qs) + report.generate_by(PDFGenerator, filename=response) + return response