Browse Source

Add "Clear all filters" option to filters (for Parlamentarian)

producao
Marcio Mazza 10 years ago
parent
commit
0c8b2c199b
  1. 3
      sigi/apps/parlamentares/admin.py
  2. 34
      sigi/apps/utils/base_admin.py
  3. 15
      sigi/apps/utils/test_base_admin.py
  4. 4
      sigiStatic/css/base_site.css
  5. 5
      templates/clear_all_filter.html

3
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')

34
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

15
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 '<li class="clear-all-filter disabled"><a href="?">Clear All Filters</a></li>' in response.content
def test_clear_all_filters_is_enabled_if_some_filter_was_used(admin_client):
# now we filter by capital letter
response = admin_client.get('/parlamentares/parlamentar/?nome_completo=B', follow=True)
assert response.status_code == 200
# and there is no "disabled" css class
assert '<li class="clear-all-filter"><a href="?">Clear All Filters</a></li>' in response.content

4
sigiStatic/css/base_site.css

@ -230,3 +230,7 @@ h1 {
#changelist-search span.small {
margin-left: 10px;
}
.dropdown-menu .clear-all-filter a {
color: #357ebd;
}

5
templates/clear_all_filter.html

@ -0,0 +1,5 @@
{% load i18n %}
{% with choices|first as disabled %}
<li class="clear-all-filter{% if disabled %} disabled{% endif %}"><a href="?">{% trans 'Clear All Filters' %}</a></li>
{% endwith %}
Loading…
Cancel
Save