From 85c76d0709b236da43fb2b19266c65e243e616e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Wed, 13 Mar 2013 11:57:58 -0300 Subject: [PATCH] =?UTF-8?q?Acrescentado=20filtro=20por=20tipo=20de=20servi?= =?UTF-8?q?=C3=A7o=20na=20changelist=20das=20casas=20atendidas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/admin/filterspecs.py | 37 +++++++++++++++++++++++++++++++++++++ sigi/apps/casas/models.py | 1 + sigi/apps/servicos/admin.py | 4 ++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/sigi/admin/filterspecs.py b/sigi/admin/filterspecs.py index 5454a8a..3c472a6 100644 --- a/sigi/admin/filterspecs.py +++ b/sigi/admin/filterspecs.py @@ -4,6 +4,7 @@ from django.utils.safestring import mark_safe from django.utils.translation import ugettext as _ from sigi.apps.contatos.models import UnidadeFederativa from abc import ABCMeta +from apps.servicos.models import TipoServico class IsActiveFilterSpec(BooleanFieldFilterSpec): """ @@ -210,3 +211,39 @@ class RangeValuesFilterSpec(FilterSpec): FilterSpec.filter_specs.insert(-1, (lambda f: hasattr(f, 'list_filter_range'), RangeValuesFilterSpec)) + +class TipoServicoFilterSpec(ChoicesFilterSpec): + """ + Usage: + + tipo_servico_field.ts_filter = True + + On Django 1.3 you will can specify a lookup on admin filters. Example: + + list_filter = ('municipio__uf',) + + """ + + def __init__(self, f, request, params, model, model_admin): + super(TipoServicoFilterSpec, self).__init__(f, request, params, model, + model_admin) + self.lookup_kwarg = 'servico__tipo_servico__id__exact' + self.lookup_val = request.GET.get(self.lookup_kwarg, None) + self.lookup_choices = TipoServico.objects.all().order_by('nome') + + def choices(self, cl): + yield {'selected': self.lookup_val is None, + 'query_string': cl.get_query_string({}, [self.lookup_kwarg]), + 'display': _('All')} + for val in self.lookup_choices: + yield {'selected': smart_unicode(val.id) == self.lookup_val, + 'query_string': cl.get_query_string({self.lookup_kwarg: val.id}), + 'display': val.nome} + + + def title(self): + return _('Tipo de servico') + +# registering the filter +FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'ts_filter', False), + TipoServicoFilterSpec)) diff --git a/sigi/apps/casas/models.py b/sigi/apps/casas/models.py index 15008da..f95b182 100644 --- a/sigi/apps/casas/models.py +++ b/sigi/apps/casas/models.py @@ -40,6 +40,7 @@ class CasaLegislativa(models.Model): observacoes = models.TextField(u'observações', blank=True) num_parlamentares = models.PositiveIntegerField('Número de parlamentares') codigo_interlegis = models.CharField('Código Interlegis', max_length=3, blank=True) + codigo_interlegis.ts_filter = True # Informações de contato logradouro = models.CharField( diff --git a/sigi/apps/servicos/admin.py b/sigi/apps/servicos/admin.py index 4061729..dec482f 100644 --- a/sigi/apps/servicos/admin.py +++ b/sigi/apps/servicos/admin.py @@ -126,14 +126,14 @@ class CasaAtendidaAdmin(admin.ModelAdmin): ,) readonly_fields = ('nome', 'logradouro', 'bairro', 'municipio', 'cep') inlines = (ContatosInline,) - list_filter = ('tipo', 'municipio', ) + list_filter = ('tipo', 'codigo_interlegis', 'municipio', ) search_fields = ('search_text','cnpj', 'bairro', 'logradouro', 'cep', 'municipio__nome', 'municipio__uf__nome', 'municipio__codigo_ibge', 'pagina_web', 'observacoes') def lookup_allowed(self, lookup, value): return super(CasaAtendidaAdmin, self).lookup_allowed(lookup, value) or \ - lookup in ['municipio__uf__codigo_ibge__exact'] + lookup in ['municipio__uf__codigo_ibge__exact', 'servico__tipo_servico__id__exact', ] def change_view(self, request, object_id, extra_context=None): # Se a Casa ainda não é atendida, gerar o código interlegis para ela