diff --git a/sigi/apps/casas/reports.py b/sigi/apps/casas/reports.py index 9fc7231..5d46791 100644 --- a/sigi/apps/casas/reports.py +++ b/sigi/apps/casas/reports.py @@ -2,10 +2,42 @@ from reportlab.lib.pagesizes import A4 from reportlab.lib.units import cm from geraldo import Report, DetailBand, Label, ObjectValue, ManyElements, \ - ReportGroup, ReportBand + ReportGroup, ReportBand, landscape from sigi.apps.relatorios.reports import ReportDefault +def string_to_cm(texto): + tamanho = 0 + minEspeciais = { + 'f':0.1, + 'i':0.05, + 'j':0.05, + 'l':0.05, + 'm':0.2, + 'r':0.1, + 't':0.15, + } + maiuEspeciais = { + 'I':0.05, + 'J':0.15, + 'L':0.15, + 'P':0.15, + } + for c in texto: + if c > 'a' and c<'z': + if c in minEspeciais: + tamanho += minEspeciais[c] + else: + tamanho += 0.17 + else: + if c in maiuEspeciais: + tamanho += maiuEspeciais[c] + else: + tamanho += 0.2 + return tamanho + + + class CasasLegislativasLabels(Report): """ Usage example:: @@ -24,47 +56,167 @@ class CasasLegislativasLabels(Report): margin_right = 0.4*cm class band_detail(DetailBand): + + width = 9.9*cm height = 5.6*cm margin_bottom = 0.0*cm - margin_right = 0.3*cm + margin_right = 0.3*cm # With this attribute as True, the band will try to align in # the same line. - display_inline = True + display_inline = True + + default_style = {'fontName': 'Helvetica', 'fontSize': 11} + + elements = [ + Label( + text='A Sua Excelência o(a) Senhor(a)', + top=1*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='parlamentar', + top=1.5*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='nome', + top=2*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + "Presidente da " + instance.nome + ), + ObjectValue( + attribute_name='logradouro', + top=2.5*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + (instance.logradouro +" - "+ instance.bairro) + if len(instance.bairro) != 0 + else instance.logradouro + ), + ObjectValue( + attribute_name='municipio', + top=3*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='cep', + top=3.5*cm, left=0.5*cm, width=9.4*cm, + ), + ] + +class CasasLabelsNomeMaior(CasasLegislativasLabels): + class band_detail(CasasLegislativasLabels.band_detail): + elements = [ + Label( + text='A Sua Excelência o(a) Senhor(a)', + top=1*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='parlamentar', + top=1.5*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='nome', + top=2*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + "Presidente da " + instance.nome + ), + ObjectValue( + attribute_name='logradouro', + top=3*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + instance.logradouro +" - "+ instance.bairro + if len(instance.bairro) != 0 + else instance.logradouro + ), + ObjectValue( + attribute_name='municipio', + top=3.5*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='cep', + top=4*cm, left=0.5*cm, width=9.4*cm, + ), + ] - default_style = {'fontName': 'Helvetica', 'fontSize': 11} +class CasasLabelsEnderecoMaior(CasasLegislativasLabels): + class band_detail(CasasLegislativasLabels.band_detail): + elements = [ + Label( + text='A Sua Excelência o(a) Senhor(a)', + top=1*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='parlamentar', + top=1.5*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='nome', + top=2*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + "Presidente da " + instance.nome + ), + ObjectValue( + attribute_name='logradouro', + top=2.5*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + instance.logradouro +" - "+ instance.bairro + if len(instance.bairro) != 0 + else instance.logradouro + ), + ObjectValue( + attribute_name='municipio', + top=3.5*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='cep', + top=4*cm, left=0.5*cm, width=9.4*cm, + ), + ] +class CasasLabelsNomeMaiorEnderecoMaior(CasasLegislativasLabels): + class band_detail(CasasLegislativasLabels.band_detail): elements = [ Label( text='A Sua Excelência o(a) Senhor(a)', - top=1*cm, left=1*cm, width=9.4*cm, + top=1*cm, left=0.5*cm, width=9.4*cm, ), ObjectValue( attribute_name='parlamentar', - top=1.5*cm, left=1*cm, width=9.4*cm, + top=1.5*cm, left=0.5*cm, width=9.4*cm, ), ObjectValue( - attribute_name='tipo', - top=2*cm, left=1*cm, width=9.4*cm, + attribute_name='nome', + top=2*cm, left=0.5*cm, width=9.4*cm, get_value=lambda instance: - "Presidente da " + instance.tipo.nome + " de" + "Presidente da " + instance.nome ), - ManyElements( - ObjectValue, - count=5, - attribute_name=('municipio.nome','logradouro','bairro','municipio','cep'), - start_top=2.5*cm, height=0.5*cm, left=1*cm, width=9.4*cm, + ObjectValue( + attribute_name='logradouro', + top=3*cm, left=0.5*cm, width=9.4*cm, + get_value=lambda instance: + instance.logradouro +" - "+ instance.bairro + if len(instance.bairro) != 0 + else instance.logradouro + ), + ObjectValue( + attribute_name='municipio', + top=4*cm, left=0.5*cm, width=9.4*cm, + ), + ObjectValue( + attribute_name='cep', + top=4.5*cm, left=0.5*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,10] + label_left = [0.3,1,5.5,11] elements = list(ReportDefault.band_page_header.elements) elements += [ @@ -98,7 +250,7 @@ class CasasLegislativasReport(ReportDefault): class band_detail(ReportDefault.band_detail): - label_left = [0.3,1,5.5,10] + label_left = [0.3,1,5.5,11] elements=[ ObjectValue( @@ -116,8 +268,7 @@ class CasasLegislativasReport(ReportDefault): ObjectValue( attribute_name='logradouro', left=label_left[3]*cm, - get_value=lambda instance: instance.logradouro + ' - '+ instance.bairro, - width = 10*cm + get_value=lambda instance: instance.logradouro + ' - '+ instance.bairro, ), ] @@ -135,4 +286,116 @@ class CasasLegislativasReport(ReportDefault): class CasasSemConvenioReport(CasasLegislativasReport): - title = u'Relatório de Casas Legislativas sem Convênio' \ No newline at end of file + title = u'Relatório de Casas Legislativas sem Convênio' + +class InfoCasaLegislativa(ReportDefault): + class band_detail(ReportDefault.band_detail): + + posicao_left = [ + 0,1.8, #Regiao + 5.5,6.8, #U.F. + 12,13.3, #Tipo + 0,2.3, #Municipio + 0,2.8, #Logradouro + 0,1.6, #Bairro + 0,1.3, #CEP + ] + posicao_top = [ + 0.5, #Regiao + 0.5, #U.F. + 0.5, #Tipo + 1.3, #Municipio + 2.1, #Logradouro + 2.9, #Bairro + 3.7, #CEP + ] + + display_inline = True + REGIAO_CHOICES = {'SL': 'Sul','SD': 'Sudeste','CO': 'Centro-Oeste','NE': 'Nordeste','NO': 'Norte',} + default_style = {'fontName': 'Helvetica', 'fontSize':14} + + + elements = [ + + # Linha 1 + Label( + text="Região: ", + left=posicao_left[0]*cm, + top=posicao_top[0]*cm, + ), + ObjectValue( + attribute_name='municipio.uf.regiao', + left=posicao_left[1]*cm, + top=posicao_top[0]*cm, + get_value=lambda instance: + {'SL': 'Sul','SD': 'Sudeste','CO': 'Centro-Oeste','NE': 'Nordeste','NO': 'Norte',} + [instance.municipio.uf.regiao] + ), + Label( + text="U.F.: ", + left=posicao_left[2]*cm, + top=posicao_top[1]*cm, + ), + ObjectValue( + attribute_name='municipio.uf', + left=posicao_left[3]*cm, + top=posicao_top[1]*cm, + ), + Label( + text="Tipo: ", + left=posicao_left[4]*cm, + top=posicao_top[2]*cm, + ), + ObjectValue( + attribute_name='tipo.nome', + left=posicao_left[5]*cm, + top=posicao_top[2]*cm, + width=6*cm, + ), + # Linha 2 + Label( + text="Município: ", + left=posicao_left[6]*cm, + top=posicao_top[3]*cm, + ), + ObjectValue( + attribute_name='municipio.nome', + left=posicao_left[7]*cm, + top=posicao_top[3]*cm, + width=20*cm, + ), + # Linha 3 + Label( + text="Logradouro: ", + left=posicao_left[8]*cm, + top=posicao_top[4]*cm, + ), + ObjectValue( + attribute_name='logradouro', + left=posicao_left[9]*cm, + top=posicao_top[4]*cm, + width=20*cm, + ), + Label( + text="Bairro: ", + left=posicao_left[10]*cm, + top=posicao_top[5]*cm, + ), + ObjectValue( + attribute_name='bairro', + left=posicao_left[11]*cm, + top=posicao_top[5]*cm, + ), + Label( + text="CEP: ", + left=posicao_left[12]*cm, + top=posicao_top[6]*cm, + ), + ObjectValue( + attribute_name='cep', + left=posicao_left[13]*cm, + top=posicao_top[6]*cm, + ), + + + ] \ No newline at end of file diff --git a/sigi/apps/casas/templates/casas/change_form.html b/sigi/apps/casas/templates/casas/change_form.html index 0e9e0dd..0533ad2 100644 --- a/sigi/apps/casas/templates/casas/change_form.html +++ b/sigi/apps/casas/templates/casas/change_form.html @@ -4,8 +4,9 @@ {% block object-tools %} {% if change %}{% if not is_popup %} {% endif %}{% endif %} diff --git a/sigi/apps/casas/views.py b/sigi/apps/casas/views.py index d54cb84..29d4655 100644 --- a/sigi/apps/casas/views.py +++ b/sigi/apps/casas/views.py @@ -2,11 +2,21 @@ 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 CasasLabelsNomeMaior +from sigi.apps.casas.reports import CasasLabelsEnderecoMaior +from sigi.apps.casas.reports import CasasLabelsNomeMaiorEnderecoMaior + from sigi.apps.casas.reports import CasasLegislativasReport from sigi.apps.casas.reports import CasasSemConvenioReport +from sigi.apps.casas.reports import InfoCasaLegislativa import csv +from sigi.apps.casas.reports import string_to_cm +from reportlab.lib.units import cm + + def labels_report(request, id=None): """ TODO: adicionar suporte para resultado de pesquisa do admin. """ @@ -17,12 +27,110 @@ def labels_report(request, id=None): kwargs = {} for k, v in request.GET.iteritems(): kwargs[str(k)] = v - qs = qs.filter(**kwargs) - if not qs: - return HttpResponseRedirect('../') + qs = qs.filter(**kwargs) + + + casasNormais = [] + casasNomesGrandes = [] + casasEnderecoGrande = [] + casasNomeEnderecoGrande = [] + tamanho = 8.2 + for casa in qs: + tamNome = string_to_cm("Presidente da " + casa.nome) + tamEnder = string_to_cm((casa.logradouro +" - "+ casa.bairro) + if len(casa.bairro) != 0 + else casa.logradouro + ) + if tamNome tamanho: + casasEnderecoGrande.append(casa) + elif tamNome >tamanho and tamEnder < tamanho: + casasNomesGrandes.append(casa) + else: + casasNomeEnderecoGrande.append(casa) + response = HttpResponse(mimetype='application/pdf') - report = CasasLegislativasLabels(queryset=qs) - report.generate_by(PDFGenerator, filename=response) + + cN = len(casasNormais) + cNoG = len(casasNomesGrandes) + cEG = len(casasEnderecoGrande) + cNoEG = len(casasNomeEnderecoGrande) + + # Testando se as listas não estão vazias e criando os reports + report1=report2=report3=report4=None + canvas = None + if cN: + report1 = CasasLegislativasLabels(queryset=casasNormais) + if cNoG or cEG or cNoEG: + canvas = report1.generate_by( + PDFGenerator, + filename=response,return_canvas=True, + ) + else: + report1.generate_by(PDFGenerator, filename=response) + + if cNoG: + report2 = CasasLabelsNomeMaior(queryset=casasNomesGrandes) + if cEG or cNoEG: + if canvas: + canvas = report2.generate_by( + PDFGenerator, + canvas=canvas, + return_canvas=True, + ) + else: + canvas = report2.generate_by( + PDFGenerator, + canvas=canvas, + return_canvas=True, + filename=response, + ) + else: + if canvas: + report2.generate_by( + PDFGenerator, + canvas=canvas, + ) + else: + report2.generate_by(PDFGenerator, filename=response) + + if cEG: + report3 = CasasLabelsEnderecoMaior(queryset=casasEnderecoGrande) + if cNoEG: + if canvas: + canvas = report3.generate_by( + PDFGenerator, + canvas=canvas, + return_canvas=True, + ) + else: + canvas = report3.generate_by( + PDFGenerator, + canvas=canvas, + return_canvas=True, + filename=response, + ) + else: + if canvas: + report3.generate_by( + PDFGenerator, + canvas=canvas, + ) + else: + report3.generate_by(PDFGenerator, filename=response) + + + if cNoEG: + report4 = CasasLabelsNomeMaiorEnderecoMaior(queryset=casasNomeEnderecoGrande) + if canvas: + report4.generate_by( + PDFGenerator, + canvas=canvas, + ) + else: + report4.generate_by(PDFGenerator, filename=response) + return response def report(request, id=None): @@ -43,6 +151,18 @@ def report(request, id=None): report.generate_by(PDFGenerator, filename=response) return response +def casa_info(request,id=None): + qs = CasaLegislativa.objects.all() + if id: + qs = qs.filter(pk=id) + else: + return HttpResponseRedirect('../') + + response = HttpResponse(mimetype='application/pdf') + report = InfoCasaLegislativa(queryset=qs) + report.generate_by(PDFGenerator, filename=response) + return response + def casas_sem_convenio_report(request): qs = CasaLegislativa.objects.filter(convenio=None).order_by('municipio__uf','nome')