From b00c249a4a7771e32570ea84c74b0dd7bb402b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Mon, 6 May 2013 12:16:16 -0300 Subject: [PATCH] =?UTF-8?q?Inclus=C3=A3o=20de=20lista=20de=20servi=C3=A7os?= =?UTF-8?q?=20filtr=C3=A1vel=20por=20tipo=20e=20UF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/admin/filterspecs.py | 27 +++++++++++++++++++ sigi/apps/servicos/admin.py | 18 +++++++++++-- sigi/apps/servicos/models.py | 2 ++ .../admin/servicos/servico/change_list.html | 4 +++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 sigi/templates/admin/servicos/servico/change_list.html diff --git a/sigi/admin/filterspecs.py b/sigi/admin/filterspecs.py index 3c472a6..25167c9 100644 --- a/sigi/admin/filterspecs.py +++ b/sigi/admin/filterspecs.py @@ -107,6 +107,33 @@ class MunicipioUFFilterSpec(AbstractFilterSpec): FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'uf_filter', False), MunicipioUFFilterSpec)) +class CasaUFFilterSpec(AbstractFilterSpec): + """ + Usage: + + my_casa_legislativa_field.casa_uf_filter = True + + On Django 1.3 you will can specify a lookup on admin filters. Example: + + list_filter = ('casa_legislativa__municipio__uf',) + + """ + + def __init__(self, f, request, params, model, model_admin): + super(CasaUFFilterSpec, self).__init__(f, request, params, model, + model_admin) + self.lookup_kwarg = '%s__municipio__uf__codigo_ibge__exact' % f.name + self.lookup_val = request.GET.get(self.lookup_kwarg, None) + self.lookup_choices = UnidadeFederativa.objects.all().order_by('nome') + def title(self): + return _('UF') % \ + {'field_name': self.field.verbose_name} + + +# registering the filter +FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'casa_uf_filter', False), + CasaUFFilterSpec)) + class ConvenioUFFilterSpec(AbstractFilterSpec): """ Usage: diff --git a/sigi/apps/servicos/admin.py b/sigi/apps/servicos/admin.py index dec482f..ccb56e7 100644 --- a/sigi/apps/servicos/admin.py +++ b/sigi/apps/servicos/admin.py @@ -48,7 +48,7 @@ class TipoServicoAdmin(admin.ModelAdmin): class ServicoAdmin(admin.ModelAdmin): form = ServicoFormAdmin - list_display = ('casa_legislativa', 'tipo_servico', 'hospedagem_interlegis', 'data_ativacao', 'data_desativacao',) + list_display = ('casa_legislativa','getUf', 'tipo_servico', 'hospedagem_interlegis', 'data_ativacao', 'data_desativacao',) fieldsets = (( None, { 'fields': ('casa_legislativa', 'data_ativacao',) }), @@ -62,9 +62,23 @@ class ServicoAdmin(admin.ModelAdmin): 'fields': ('data_alteracao', 'data_desativacao', 'motivo_desativacao',) })) readonly_fields = ('casa_legislativa', 'data_ativacao', 'data_alteracao') - + list_filter = ('tipo_servico', 'hospedagem_interlegis', 'casa_legislativa') + list_display_links = [] + ordering = ('casa_legislativa__municipio__uf', 'casa_legislativa', 'tipo_servico',) + actions = None inlines = (LogServicoInline,) + def getUf(self, obj): + return '%s' % (obj.casa_legislativa.municipio.uf) + + getUf.short_description = 'UF' + getUf.admin_order_field = 'casa_legislativa__municipio__uf' + + def lookup_allowed(self, lookup, value): + return super(ServicoAdmin, self).lookup_allowed(lookup, value) or \ + lookup in ['casa_legislativa__municipio__uf__codigo_ibge__exact'] + + def add_view(self, request, form_url='', extra_context=None): id_casa = request.GET.get('id_casa', None) diff --git a/sigi/apps/servicos/models.py b/sigi/apps/servicos/models.py index b6bb81a..a4d7ba0 100644 --- a/sigi/apps/servicos/models.py +++ b/sigi/apps/servicos/models.py @@ -42,6 +42,8 @@ class Servico(models.Model): data_desativacao = models.DateField('Data de desativação', blank=True, null=True) motivo_desativacao = models.TextField('Motivo da desativação', blank=True) + casa_legislativa.casa_uf_filter = True + def __unicode__(self): return "%s (%s)" % (self.tipo_servico.nome, 'ativo' if self.data_desativacao is None else 'Desativado') diff --git a/sigi/templates/admin/servicos/servico/change_list.html b/sigi/templates/admin/servicos/servico/change_list.html new file mode 100644 index 0000000..5d90f39 --- /dev/null +++ b/sigi/templates/admin/servicos/servico/change_list.html @@ -0,0 +1,4 @@ +{% extends "admin/change_list.html" %} + +{% block object-tools %} +{% endblock %} \ No newline at end of file