diff --git a/sigi/apps/parlamentares/admin.py b/sigi/apps/parlamentares/admin.py index 5fabe05..0de99dd 100644 --- a/sigi/apps/parlamentares/admin.py +++ b/sigi/apps/parlamentares/admin.py @@ -9,6 +9,7 @@ from sigi.apps.contatos.models import Telefone from sigi.apps.parlamentares.models import Partido, Parlamentar, Mandato from sigi.apps.parlamentares.views import adicionar_parlamentar_carrinho from sigi.apps.utils.filters import AlphabeticFilter +from sigi.apps.utils.base_admin import BaseModelAdmin class MandatosInline(admin.TabularInline): @@ -33,7 +34,7 @@ class ParlamentarNomeCompletoFilter(AlphabeticFilter): parameter_name = 'nome_completo' -class ParlamentarAdmin(admin.ModelAdmin): +class ParlamentarAdmin(BaseModelAdmin): inlines = (TelefonesInline, MandatosInline) list_display = ('nome_completo', 'nome_parlamentar', 'sexo') list_display_links = ('nome_completo', 'nome_parlamentar') diff --git a/sigi/apps/utils/base_admin.py b/sigi/apps/utils/base_admin.py new file mode 100644 index 0000000..b76370a --- /dev/null +++ b/sigi/apps/utils/base_admin.py @@ -0,0 +1,34 @@ +from django.contrib import admin +from django.contrib.admin.views.main import ChangeList + + +class ClearAllFilter(object): + title = None + template = 'clear_all_filter.html' + + def __init__(self, disabled): + self.disabled = disabled + + def choices(self, cl): + return [self.disabled] + + def queryset(self, request, queryset): + return queryset + + +class BaseChangeList(ChangeList): + + def get_filters(self, request): + (filter_specs, has_filters, lookup_params, + use_distinct) = super(BaseChangeList, self).get_filters(request) + if filter_specs: + clear_all_disabled = not self.get_filters_params() + filter_specs = [ClearAllFilter(clear_all_disabled)] + filter_specs + + return (filter_specs, has_filters, lookup_params, use_distinct) + + +class BaseModelAdmin(admin.ModelAdmin): + + def get_changelist(self, request, **kwargs): + return BaseChangeList diff --git a/sigi/apps/utils/test_base_admin.py b/sigi/apps/utils/test_base_admin.py new file mode 100644 index 0000000..ff3268d --- /dev/null +++ b/sigi/apps/utils/test_base_admin.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + + +def test_clear_all_filters_is_disabled_if_no_filter_was_used(admin_client): + response = admin_client.get('/parlamentares/parlamentar', follow=True) + assert response.status_code == 200 + assert '