From 740ec5b0c046c9b40cb8a062724e071db3f292a4 Mon Sep 17 00:00:00 2001 From: Breno Teixeira Date: Tue, 12 Nov 2013 17:14:28 -0200 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o/Adi=C3=A7=C3=A3o=20do=20f?= =?UTF-8?q?iltro=20por=20ordem=20alfabetica?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/parlamentares/admin.py | 37 +++------------------------- sigi/apps/servidores/admin.py | 20 ++++++++++++++- sigi/apps/utils/alphabetic_filter.py | 30 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 sigi/apps/utils/alphabetic_filter.py diff --git a/sigi/apps/parlamentares/admin.py b/sigi/apps/parlamentares/admin.py index 598f08b..15c171a 100644 --- a/sigi/apps/parlamentares/admin.py +++ b/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,41 +25,9 @@ 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' + title = 'Nome Completo do Parlamentar' + parameter_name = 'nome_completo' class ParlamentarAdmin(admin.ModelAdmin): diff --git a/sigi/apps/servidores/admin.py b/sigi/apps/servidores/admin.py index 911a0bc..670ea8f 100644 --- a/sigi/apps/servidores/admin.py +++ b/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 diff --git a/sigi/apps/utils/alphabetic_filter.py b/sigi/apps/utils/alphabetic_filter.py new file mode 100644 index 0000000..22fef35 --- /dev/null +++ b/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() ) ) \ No newline at end of file