diff --git a/sigi/apps/casas/admin.py b/sigi/apps/casas/admin.py index 856c52f..6ca42b6 100644 --- a/sigi/apps/casas/admin.py +++ b/sigi/apps/casas/admin.py @@ -16,6 +16,8 @@ class TelefonesInline(generic.GenericTabularInline): class CasaLegislativaAdmin(admin.ModelAdmin): form = CasaLegislativaForm + change_form_template = 'casas/change_form.html' + change_list_template = 'casas/change_list.html' inlines = (TelefonesInline, ContatosInline) list_display = ('nome', 'email', 'pagina_web', 'municipio') list_display_links = ('nome',) @@ -37,4 +39,10 @@ class CasaLegislativaAdmin(admin.ModelAdmin): 'cep', 'municipio__nome', 'municipio__uf__nome', 'municipio__codigo_ibge', 'pagina_web', 'observacoes') + def changelist_view(self, request, extra_context=None): + return super(CasaLegislativaAdmin, self).changelist_view( + request, + extra_context={'query_str': '?' + request.META['QUERY_STRING']} + ) + admin.site.register(CasaLegislativa, CasaLegislativaAdmin) diff --git a/sigi/apps/casas/models.py b/sigi/apps/casas/models.py index aec461e..c32ee4d 100644 --- a/sigi/apps/casas/models.py +++ b/sigi/apps/casas/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from django.db import models from django.contrib.contenttypes import generic -from sigi.apps.mesas.models import MesaDiretora +from sigi.apps.mesas.models import MesaDiretora, MembroMesaDiretora class CasaLegislativa(models.Model): CASA_CHOICES = ( @@ -64,7 +64,12 @@ class CasaLegislativa(models.Model): def __unicode__(self): return self.nome - def get_presidente(self): - mesa = MesaDiretora.objects.get(casa_legislativa=self) - membro = mesa.membromesadiretora_set.get(cargo__descricao__iexact='presidente') - return membro.parlamentar + def get_presidente_nome(self): + try: + mesa = MesaDiretora.objects.get(casa_legislativa=self) + membro = mesa.membromesadiretora_set.get( + cargo__descricao__iexact='presidente' + ) + except (MesaDiretora.DoesNotExist, MembroMesaDiretora.DoesNotExist): + return '' + return membro.parlamentar.nome_completo diff --git a/sigi/apps/casas/reports.py b/sigi/apps/casas/reports.py new file mode 100644 index 0000000..c016f0e --- /dev/null +++ b/sigi/apps/casas/reports.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +import reporting +from reportlab.lib.units import cm +from geraldo import Report, ReportBand, Label, ObjectValue +from sigi.apps.casas.models import CasaLegislativa + +class CasasLegislativasLabels(Report): + """ + Usage example:: + + >>> from geraldo.generators import PDFGenerator + >>> queryset = CasaLegislativa.objects.filter(municipio__uf__sigla='MG') + >>> report = LabelsReport(queryset) + >>> report.generate_by(PDFGenerator, filename='./inline-detail-report.pdf') + + """ + + class band_detail(ReportBand): + width = 9.40*cm + height = 4.60*cm + + # With this attribute as True, the band will try to align in + # the same line. + display_inline = True + + elements = [ + Label(text='A Sua ExcelĂȘncia o(a) Senhor(a)', top=0, left=0), + ObjectValue( + attribute_name='get_presidente_nome', + top=0.5*cm, left=0, width=9.00*cm, + get_value=lambda obj: obj.get_presidente_nome(), + ), + ObjectValue(attribute_name='nome', top=1.0*cm, left=0, width=9.00*cm), + ObjectValue(attribute_name='logradouro', top=1.5*cm, left=0, width=9.00*cm), + ObjectValue(attribute_name='bairro', top=2*cm, left=0, width=9.00*cm), + ObjectValue(attribute_name='municipio', top=2.5*cm, left=0, width=9.00*cm), + ObjectValue(attribute_name='cep', top=3*cm, left=0, width=9.00*cm), + ] + +reporting.site.register(CasasLegislativasLabels, CasaLegislativa, 'etiquetas') diff --git a/sigi/apps/casas/templates/casas/change_form.html b/sigi/apps/casas/templates/casas/change_form.html new file mode 100644 index 0000000..0e9e0dd --- /dev/null +++ b/sigi/apps/casas/templates/casas/change_form.html @@ -0,0 +1,12 @@ +{% extends "admin/change_form.html" %} +{% load i18n reporting_tags %} + +{% block object-tools %} +{% if change %}{% if not is_popup %} + +{% endif %}{% endif %} +{% endblock %} diff --git a/sigi/apps/casas/templates/casas/change_list.html b/sigi/apps/casas/templates/casas/change_list.html new file mode 100644 index 0000000..642bd05 --- /dev/null +++ b/sigi/apps/casas/templates/casas/change_list.html @@ -0,0 +1,15 @@ +{% extends "admin/change_list.html" %} +{% load i18n reporting_tags %} + +{% block object-tools %} + {% if has_add_permission %} + + {% endif %} +{% endblock %} diff --git a/sigi/apps/casas/views.py b/sigi/apps/casas/views.py new file mode 100644 index 0000000..e798b5c --- /dev/null +++ b/sigi/apps/casas/views.py @@ -0,0 +1,22 @@ +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 + +def labels_report(request, id=None): + """ TODO: adicionar suporte para resultado de pesquisa do admin. + """ + qs = CasaLegislativa.objects.all() + 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('../') + response = HttpResponse(mimetype='application/pdf') + report = CasasLegislativasLabels(queryset=qs) + report.generate_by(PDFGenerator, filename=response) + return response diff --git a/sigi/settings.py b/sigi/settings.py index ad5e3c0..5fe0766 100644 --- a/sigi/settings.py +++ b/sigi/settings.py @@ -88,6 +88,7 @@ INSTALLED_APPS = ( 'django_extensions', # http://django-command-extensions.googlecode.com 'googlecharts', # http://github.com/jacobian/django-googlecharts 'treemenus', # http://django-treemenus.googlecode.com + 'reporting', # http://github.com/marinho/geraldo 'sigi.apps.casas', 'sigi.apps.contatos', 'sigi.apps.convenios', diff --git a/sigi/urls.py b/sigi/urls.py index da6cd83..6eb1965 100644 --- a/sigi/urls.py +++ b/sigi/urls.py @@ -1,17 +1,19 @@ from django.conf import settings from django.conf.urls.defaults import * -from django.contrib import databrowse -from django.db.models import get_models from sigi import sites # register admin filters import sigi.admin.filterspecs -map(databrowse.site.register, get_models()) - urlpatterns = patterns( '', + # reports + (r'^casas/casalegislativa/labels/', + 'sigi.apps.casas.views.labels_report'), + (r'^casas/casalegislativa/(?P\w+)/labels/', + 'sigi.apps.casas.views.labels_report'), + # automatic interface based on admin (r'^(.*)', sites.default.root), )