diff --git a/sigi/admin/filterspecs.py b/sigi/admin/filterspecs.py index 25167c9..49645fe 100644 --- a/sigi/admin/filterspecs.py +++ b/sigi/admin/filterspecs.py @@ -5,6 +5,7 @@ from django.utils.translation import ugettext as _ from sigi.apps.contatos.models import UnidadeFederativa from abc import ABCMeta from apps.servicos.models import TipoServico +from apps.convenios.models import Projeto class IsActiveFilterSpec(BooleanFieldFilterSpec): """ @@ -274,3 +275,38 @@ class TipoServicoFilterSpec(ChoicesFilterSpec): # registering the filter FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'ts_filter', False), TipoServicoFilterSpec)) + +class CasaProjetoFilterSpec(ChoicesFilterSpec): + """ + For usage with CasaLegislativa model: + + any_field.projeto_filter = True + + On Django 1.3 you will can specify a lookup on admin filters. Example: + + list_filter = ('convenio_set',) + + """ + + def __init__(self, f, request, params, model, model_admin): + super(CasaProjetoFilterSpec, self).__init__(f, request, params, model, + model_admin) + self.lookup_kwarg = 'convenio__projeto__id__exact' + self.lookup_val = request.GET.get(self.lookup_kwarg, None) + self.lookup_choices = Projeto.objects.all().order_by('sigla') + + 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.sigla} + + def title(self): + return _('Projeto') + +# registering the filter +FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'projeto_filter', False), + CasaProjetoFilterSpec)) diff --git a/sigi/apps/casas/admin.py b/sigi/apps/casas/admin.py index 28d7ad8..f966d91 100644 --- a/sigi/apps/casas/admin.py +++ b/sigi/apps/casas/admin.py @@ -144,9 +144,9 @@ class CasaLegislativaAdmin(admin.ModelAdmin): actions = ['adicionar_casas',] inlines = (TelefonesInline, PresidenteInline, FuncionariosInline, ConveniosInline, LegislaturaInline, DiagnosticoInline, BemInline, ServicoInline, PlanoDiretorInline) - list_display = ('nome','municipio','logradouro', 'ult_alt_endereco') + list_display = ('nome','municipio','logradouro', 'ult_alt_endereco', 'get_convenios') list_display_links = ('nome',) - list_filter = ('tipo', 'municipio') + list_filter = ('tipo', 'municipio', 'search_text') ordering = ('nome','municipio__uf') queyrset = queryset_ascii fieldsets = ( @@ -168,6 +168,11 @@ class CasaLegislativaAdmin(admin.ModelAdmin): 'cep', 'municipio__nome', 'municipio__uf__nome', 'municipio__codigo_ibge', 'pagina_web', 'observacoes') + def get_convenios(self, obj): + return '' + get_convenios.short_description = u'Convênios' + get_convenios.allow_tags= True + def changelist_view(self, request, extra_context=None): return super(CasaLegislativaAdmin, self).changelist_view( request, @@ -176,7 +181,7 @@ class CasaLegislativaAdmin(admin.ModelAdmin): def lookup_allowed(self, lookup, value): return super(CasaLegislativaAdmin, self).lookup_allowed(lookup, value) or \ - lookup in ['municipio__uf__codigo_ibge__exact'] + lookup in ['municipio__uf__codigo_ibge__exact', 'convenio__projeto__id__exact'] def etiqueta(self,request,queryset): diff --git a/sigi/apps/casas/models.py b/sigi/apps/casas/models.py index c208e4f..13f658b 100644 --- a/sigi/apps/casas/models.py +++ b/sigi/apps/casas/models.py @@ -35,6 +35,7 @@ class CasaLegislativa(models.Model): # Guarda um campo para ser usado em buscas em caixa baixa e sem acento search_text = SearchField(field_names=['nome']) + search_text.projeto_filter = True tipo = models.ForeignKey(TipoCasaLegislativa, verbose_name="Tipo") cnpj = models.CharField('CNPJ', max_length=32, blank=True) observacoes = models.TextField(u'observações', blank=True) diff --git a/sigi/apps/convenios/models.py b/sigi/apps/convenios/models.py index 4b4b0c9..1d38ad6 100644 --- a/sigi/apps/convenios/models.py +++ b/sigi/apps/convenios/models.py @@ -111,7 +111,10 @@ class Convenio(models.Model): verbose_name = u'convênio' def __unicode__(self): - return str(self.id) + if self.data_retorno_assinatura != None: + return u"Convênio nº %s - projeto %s, em %s" % (self.num_convenio, self.projeto.sigla, self.data_retorno_assinatura) + else: + return u"Adesão ao projeto %s, em %s" % (self.projeto.sigla, self.data_adesao) class EquipamentoPrevisto(models.Model): """ Modelo utilizado para registrar os equipamentos