Browse Source

Refatoração/Adição do filtro por ordem alfabetica

stable/2.0
Breno Teixeira 11 years ago
parent
commit
740ec5b0c0
  1. 33
      sigi/apps/parlamentares/admin.py
  2. 20
      sigi/apps/servidores/admin.py
  3. 30
      sigi/apps/utils/alphabetic_filter.py

33
sigi/apps/parlamentares/admin.py

@ -8,6 +8,7 @@ from django.http import HttpResponse, HttpResponseRedirect
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.alphabetic_filter import AlphabeticFilter
class MandatosInline(admin.TabularInline):
model = Mandato
@ -24,38 +25,6 @@ class PartidoAdmin(admin.ModelAdmin):
search_fields = ('nome', 'sigla')
class AlphabeticFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = ''
# Parameter for the filter that will be used in the URL query.
parameter_name = ''
alphabetic = string.ascii_uppercase
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
return ((letter, letter,) for letter in self.alphabetic)
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
qs = self.parameter_name + '__istartswith', self.value()
return queryset.filter(qs)
class ParlamentarNomeCompletoFilter(AlphabeticFilter):
title = 'Nome Completo do Parlamentar'
parameter_name = 'nome_completo'

20
sigi/apps/servidores/admin.py

@ -6,6 +6,8 @@ from sigi.apps.utils.admin_widgets import AdminImageWidget
from sigi.apps.servidores.models import Servidor, Funcao, Licenca, Ferias, Servico, Subsecretaria
from sigi.apps.contatos.models import Endereco, Telefone
from sigi.apps.servidores.forms import FeriasForm, LicencaForm, FuncaoForm
from sigi.apps.utils.alphabetic_filter import AlphabeticFilter
class FuncaoAdmin(admin.ModelAdmin):
form = FuncaoForm
@ -24,23 +26,39 @@ class FeriasAdmin(admin.ModelAdmin):
'servidor__nome_completo', 'servidor__email_pessoal',
'servidor__user__email', 'servidor__user__username')
from sigi.apps.utils.alphabetic_filter import AlphabeticFilter
class ServidorFilter(AlphabeticFilter):
title = 'Nome do Servidor'
parameter_name = 'servidor__nome_completo'
class LicencaAdmin(admin.ModelAdmin):
form = LicencaForm
list_display = ('servidor', 'inicio_licenca', 'fim_licenca')
list_filter = ('servidor', 'inicio_licenca', 'fim_licenca')
list_filter = (ServidorFilter, 'servidor', 'inicio_licenca', 'fim_licenca')
search_fields = ('obs',
'servidor__nome_completo', 'servidor__email_pessoal',
'servidor__user__email', 'servidor__user__username')
def lookup_allowed(self, lookup, value):
return super(LicencaAdmin, self).lookup_allowed(lookup, value) or \
lookup in ['servidor__nome_completo']
class EnderecoInline(generic.GenericStackedInline):
model = Endereco
extra = 0
raw_id_fields = ('municipio',)
class TelefonesInline(generic.GenericTabularInline):
extra = 1
model = Telefone
class ServidorAdmin(admin.ModelAdmin):
def is_active(self, servidor):
return servidor.user.is_active

30
sigi/apps/utils/alphabetic_filter.py

@ -0,0 +1,30 @@
# coding: utf-8
import string
from django.contrib import admin
class AlphabeticFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = ''
# Parameter for the filter that will be used in the URL query.
parameter_name = ''
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
return ((letter, letter,) for letter in string.ascii_uppercase)
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
if self.value():
return queryset.filter( ( self.parameter_name + '__istartswith', self.value() ) )
Loading…
Cancel
Save