From 47ce4fa5ec43ff0b50dac4c1fe28366dc8ba4af8 Mon Sep 17 00:00:00 2001 From: "Starlone (Estagiario)" Date: Thu, 28 Oct 2010 20:38:52 +0000 Subject: [PATCH] =?UTF-8?q?Alterando=20gera=C3=A7=C3=A3o=20de=20relatorios?= =?UTF-8?q?=20para=20que=20tratem=20ordena=C3=A7=C3=A3o=20do=20admin.=20Ti?= =?UTF-8?q?rando=20codigos=20desnecessarios=20das=20=20actions=20do=20admi?= =?UTF-8?q?n=20de=20casa.=20Acrescentando=20urls=20que=20faltaram=20para?= =?UTF-8?q?=20tratar=20etiqueta=20sem=20presidente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/casas/admin.py | 22 ++++------- sigi/apps/casas/views.py | 72 ++++++++++++++++++++---------------- sigi/apps/convenios/admin.py | 2 +- sigi/apps/convenios/views.py | 57 ++++++++++++++++------------ sigi/urls.py | 5 +++ 5 files changed, 88 insertions(+), 70 deletions(-) diff --git a/sigi/apps/casas/admin.py b/sigi/apps/casas/admin.py index f8a717b..97f7897 100644 --- a/sigi/apps/casas/admin.py +++ b/sigi/apps/casas/admin.py @@ -8,7 +8,8 @@ from sigi.apps.convenios.models import Projeto, Convenio, EquipamentoPrevisto, A from django.http import HttpResponse, HttpResponseRedirect from sigi.apps.casas.reports import CasasLegislativasLabels, CasasLegislativasReport from geraldo.generators import PDFGenerator -from sigi.apps.casas.views import casa_info, labels_report, export_csv, labels_report_sem_presidente +from sigi.apps.casas.views import casa_info, labels_report, export_csv, \ + labels_report_sem_presidente, report from sigi.apps.utils import queryset_ascii class ContatosInline(generic.GenericTabularInline): @@ -60,30 +61,23 @@ class CasaLegislativaAdmin(admin.ModelAdmin): extra_context={'query_str': '?' + request.META['QUERY_STRING']} ) - def etiqueta(modelAdmin,request,queryset): - response = HttpResponse(mimetype='application/pdf') + def etiqueta(modelAdmin,request,queryset): return labels_report(request,queryset=queryset) etiqueta.short_description = "Gerar etiqueta(s) da(s) casa(s) selecionada(s)" - def etiqueta_sem_presidente(modelAdmin,request,queryset): - response = HttpResponse(mimetype='application/pdf') + def etiqueta_sem_presidente(modelAdmin,request,queryset): return labels_report_sem_presidente(request,queryset=queryset) etiqueta_sem_presidente.short_description = "Gerar etiqueta(s) sem presidente 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 + def relatorio(modelAdmin,request,queryset): + return report(request,queryset=queryset) relatorio.short_description = u"Exportar a(s) casa(s) selecionada(s) para PDF" - def relatorio_completo(modelAdmin,request,queryset): - response = HttpResponse(mimetype='application/pdf') + def relatorio_completo(modelAdmin,request,queryset): return casa_info(request,queryset=queryset) relatorio_completo.short_description = u"Gerar relatório completo da(s) casa(s) selecionada(s)" - def relatorio_csv(modelAdmin,request,queryset): - response = HttpResponse(mimetype='text/csv') + def relatorio_csv(modelAdmin,request,queryset): return export_csv(request) relatorio_csv.short_description = u"Exportar casa(s) selecionada(s) para CSV" diff --git a/sigi/apps/casas/views.py b/sigi/apps/casas/views.py index 502ffcd..6f69817 100644 --- a/sigi/apps/casas/views.py +++ b/sigi/apps/casas/views.py @@ -11,9 +11,31 @@ from sigi.apps.casas.reports import InfoCasaLegislativa import csv +def query_ordena(qs,o,ot): + list_display = ('nome','municipio','presidente','logradouro') + + aux = list_display[(int(o)-1)] + if ot =='asc': + qs = qs.order_by(aux) + else: + qs = qs.order_by("-"+aux) + return qs + +def get_for_qs(get,qs): + kwargs = {} + for k,v in get.iteritems(): + if not k == 'o': + if k == "ot": + qs = query_ordena(qs,get["o"],get["ot"]) + else: + kwargs[str(k)] = v + qs = qs.filter(**kwargs) + return qs + + def labels_report(request, id=None,queryset=None): """ TODO: adicionar suporte para resultado de pesquisa do admin. - """ + """ if queryset: qs = queryset else: @@ -21,10 +43,7 @@ def labels_report(request, id=None,queryset=None): 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) + qs = get_for_qs(request.GET,qs) response = HttpResponse(mimetype='application/pdf') report = CasasLegislativasLabels(queryset=qs) @@ -42,10 +61,7 @@ def labels_report_sem_presidente(request, id=None,queryset=None): 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) + qs = get_for_qs(request.GET,qs) response = HttpResponse(mimetype='application/pdf') report = CasasLegislativasLabelsSemPresidente(queryset=qs) @@ -54,17 +70,18 @@ def labels_report_sem_presidente(request, id=None,queryset=None): 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('../') +def report(request, id=None,queryset=None): + if queryset: + qs = queryset + else: + qs = CasaLegislativa.objects.all() + if id: + qs = qs.filter(pk=id) + elif request.GET: + qs = get_for_qs(request.GET,qs) + + if not qs: + return HttpResponseRedirect('../') #qs.order_by('municipio__uf','nome') response = HttpResponse(mimetype='application/pdf') @@ -80,10 +97,7 @@ def casa_info(request,id=None,queryset=None): 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) + qs = get_for_qs(request.GET,qs) if not qs: return HttpResponseRedirect('../') @@ -116,15 +130,11 @@ def casa_info(request,id=None,queryset=None): def casas_sem_convenio_report(request): qs = CasaLegislativa.objects.filter(convenio=None).order_by('municipio__uf','nome') - if request.GET: - kwargs = {} - for k, v in request.GET.iteritems(): - kwargs[str(k)] = v - qs = qs.filter(**kwargs) + if request.GET: + qs = get_for_qs(request.GET,qs) if not qs: return HttpResponseRedirect('../') - - #qs.order_by('municipio__uf','nome') + response = HttpResponse(mimetype='application/pdf') report = CasasSemConvenioReport(queryset=qs) report.generate_by(PDFGenerator, filename=response) diff --git a/sigi/apps/convenios/admin.py b/sigi/apps/convenios/admin.py index 36198f4..b3909e4 100644 --- a/sigi/apps/convenios/admin.py +++ b/sigi/apps/convenios/admin.py @@ -47,7 +47,7 @@ class ConvenioAdmin(admin.ModelAdmin): list_display_links = ('num_convenio','casa_legislativa',) list_filter = ('projeto','casa_legislativa','conveniada', 'equipada') #date_hierarchy = 'data_adesao' - ordering = ('casa_legislativa__municipio__uf','-id',) + ordering = ('casa_legislativa__municipio__uf','casa_legislativa','-id',) raw_id_fields = ('casa_legislativa',) queryset = queryset_ascii search_fields = ('id', 'search_text',#'casa_legislativa__nome', diff --git a/sigi/apps/convenios/views.py b/sigi/apps/convenios/views.py index 710beb3..2d52e82 100644 --- a/sigi/apps/convenios/views.py +++ b/sigi/apps/convenios/views.py @@ -14,25 +14,44 @@ from django.template import Context, loader import datetime - - +def query_ordena(qs,o,ot): + list_display = ('num_convenio', 'casa_legislativa', + 'data_adesao','data_retorno_assinatura','data_termo_aceite', + 'projeto', + ) + + aux = list_display[(int(o)-1)] + if ot =='asc': + qs = qs.order_by(aux) + else: + qs = qs.order_by("-"+aux) + return qs + +def get_for_qs(get,qs): + kwargs = {} + ids = 0 + for k,v in get.iteritems(): + if not k == 'o': + if k == "ot": + qs = query_ordena(qs,get["o"],get["ot"]) + else: + kwargs[str(k)] = v + if(str(k)=='ids'): + ids = 1 + break + qs = qs.filter(**kwargs) + + if ids: + query = 'id IN ('+ kwargs['ids'].__str__()+')' + qs = Convenio.objects.extra(where=[query]) + return qs def report_por_cm(request, id=None): qs = Convenio.objects.filter(casa_legislativa__tipo__sigla='CM').order_by('casa_legislativa__municipio__uf','casa_legislativa') if id: qs = qs.filter(pk=id) elif request.GET: #Se tiver algum parametro de pesquisa - kwargs = {} - ids = 0 - for k, v in request.GET.iteritems(): - kwargs[str(k)] = v - if(str(k)=='ids'): - ids = 1 - break - qs = qs.filter(**kwargs) - if ids: - query = 'id IN ('+ kwargs['ids'].__str__()+')' - qs = Convenio.objects.extra(where=[query]) + qs = get_for_qs(request.GET,qs) if not qs: return HttpResponseRedirect('../') response = HttpResponse(mimetype='application/pdf') @@ -45,17 +64,7 @@ def report_por_al(request, id=None): if id: qs = qs.filter(pk=id) elif request.GET: #Se tiver algum parametro de pesquisa - kwargs = {} - ids = 0 - for k, v in request.GET.iteritems(): - kwargs[str(k)] = v - if(str(k)=='ids'): - ids = 1 - break - qs = qs.filter(**kwargs) - if ids: - query = 'id IN ('+ kwargs['ids'].__str__()+')' - qs = Convenio.objects.extra(where=[query]) + qs = get_for_qs(request.GET,qs) if not qs: return HttpResponseRedirect('../') response = HttpResponse(mimetype='application/pdf') diff --git a/sigi/urls.py b/sigi/urls.py index 94d59f0..e555644 100644 --- a/sigi/urls.py +++ b/sigi/urls.py @@ -16,6 +16,11 @@ urlpatterns = patterns( 'sigi.apps.casas.views.labels_report'), (r'^casas/casalegislativa/(?P\w+)/labels/', 'sigi.apps.casas.views.labels_report'), + # reports labels sem presidente + (r'^casas/casalegislativa/labels_sem_presidente/', + 'sigi.apps.casas.views.labels_report_sem_presidente'), + (r'^casas/casalegislativa/(?P\w+)/labels_sem_presidente/', + 'sigi.apps.casas.views.labels_report_sem_presidente'), # reports (r'^casas/casalegislativa/reports/', 'sigi.apps.casas.views.report'),