From 375b443e5ab157c283ca89ff5e42a9463c1b0bf2 Mon Sep 17 00:00:00 2001 From: Felipe Vieira Date: Mon, 12 Dec 2011 14:35:30 +0000 Subject: [PATCH] adicionando filtro por usuario ativo no admin --- sigi/admin/filterspecs.py | 23 +++++++++++++- sigi/apps/servidores/admin.py | 57 ++++++++++++++++------------------ sigi/apps/servidores/models.py | 3 +- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/sigi/admin/filterspecs.py b/sigi/admin/filterspecs.py index 51b14f8..5454a8a 100644 --- a/sigi/admin/filterspecs.py +++ b/sigi/admin/filterspecs.py @@ -1,10 +1,31 @@ -from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec +from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec, BooleanFieldFilterSpec from django.utils.encoding import smart_unicode from django.utils.safestring import mark_safe from django.utils.translation import ugettext as _ from sigi.apps.contatos.models import UnidadeFederativa from abc import ABCMeta +class IsActiveFilterSpec(BooleanFieldFilterSpec): + """ + Adds filtering by user is_active attr in the admin filter sidebar + my_model_user_field.is_active__filter = True + """ + + def __init__(self, f, request, params, model, model_admin): + super(IsActiveFilterSpec, self).__init__(f, request, params, model, + model_admin) + self.lookup_kwarg = '%s__is_active__exact' % f.name + self.lookup_kwarg2 = '%s__is_active__isnull' % f.name + self.lookup_val = request.GET.get(self.lookup_kwarg, None) + self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None) + + def title(self): + return _('active') + +# registering the filter +FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'is_active__filter', False), + IsActiveFilterSpec)) + class AlphabeticFilterSpec(ChoicesFilterSpec): """ Adds filtering by first char (alphabetic style) of values in the admin diff --git a/sigi/apps/servidores/admin.py b/sigi/apps/servidores/admin.py index bd1201e..1462b94 100644 --- a/sigi/apps/servidores/admin.py +++ b/sigi/apps/servidores/admin.py @@ -7,10 +7,6 @@ from sigi.apps.servidores.models import Servidor, Funcao, Licenca, Ferias from sigi.apps.contatos.models import Endereco, Telefone from sigi.apps.servidores.forms import FeriasForm, LicencaForm, FuncaoForm -class FuncaoInline(admin.TabularInline): - model = Funcao - extra = 1 - class FuncaoAdmin(admin.ModelAdmin): form = FuncaoForm list_display = ('servidor', 'funcao', 'cargo','inicio_funcao', 'fim_funcao') @@ -20,36 +16,25 @@ class FuncaoAdmin(admin.ModelAdmin): 'servidor__user__email', 'servidor__user__first_name', 'servidor__user__last_name', 'servidor__user__username') -class FeriasInline(admin.TabularInline): - model = Ferias - extra = 1 - class FeriasAdmin(admin.ModelAdmin): form = FeriasForm list_display = ('servidor', 'inicio_ferias', 'fim_ferias') - list_filter = ('servidor', 'inicio_ferias', 'fim_ferias') + list_filter = ('inicio_ferias', 'fim_ferias') search_fields = ('obs', - 'servidor__nome_completo', 'servidor__obs', 'servidor__apontamentos', - 'servidor__user__email', 'servidor__user__first_name', - 'servidor__user__last_name', 'servidor__user__username') - - -class LicencaInline(admin.TabularInline): - model = Licenca - extra = 1 + 'servidor__nome_completo', 'servidor__email_pesoal', + 'servidor__user__email', 'servidor__user__username') class LicencaAdmin(admin.ModelAdmin): form = LicencaForm list_display = ('servidor', 'inicio_licenca', 'fim_licenca') list_filter = ('servidor', 'inicio_licenca', 'fim_licenca') search_fields = ('obs', - 'servidor__nome_completo', 'servidor__obs', 'servidor__apontamentos', - 'servidor__user__email', 'servidor__user__first_name', - 'servidor__user__last_name', 'servidor__user__username') + 'servidor__nome_completo', 'servidor__email_pesoal', + 'servidor__user__email', 'servidor__user__username') -class EnderecoInline(generic.GenericTabularInline): +class EnderecoInline(generic.GenericStackedInline): model = Endereco - extra = 1 + extra = 0 raw_id_fields = ('municipio',) class TelefonesInline(generic.GenericTabularInline): @@ -57,8 +42,24 @@ class TelefonesInline(generic.GenericTabularInline): model = Telefone class ServidorAdmin(admin.ModelAdmin): - list_display = ('nome_completo', 'servico') - list_filter = ('sexo', 'servico') + + def is_active(self, servidor): + return servidor.user.is_active + is_active.admin_order_field = 'is_active' + is_active.boolean = True + is_active.short_description = 'ativo' + + def queryset(self, request): + qs = super(ServidorAdmin, self).queryset(request) + qs = qs.extra(select={'is_active': """ + SELECT auth_user.is_active + FROM auth_user + WHERE auth_user.id = servidores_servidor.user_id + """}) + return qs + + list_display = ('nome_completo', 'is_active', 'foto', 'servico') + list_filter = ('user', 'sexo', 'servico',) search_fields = ('nome_completo', 'obs', 'apontamentos', 'user__email', 'user__first_name', 'user__last_name', 'user__username') @@ -69,18 +70,14 @@ class ServidorAdmin(admin.ModelAdmin): 'fields': ('user',), }), ('Cadastro', { - 'fields': ('nome_completo', 'foto', 'email_pessoal', 'rg', 'cpf', 'sexo', 'data_nascimento', 'matricula', 'ramal') + 'fields': ('nome_completo', 'foto', 'email_pessoal', 'rg', 'cpf', 'sexo', 'data_nascimento', 'matricula', 'ramal', 'data_nomeacao', 'ato_numero', 'ato_exoneracao') }), ('Origem', { - 'fields': ('turno',), + 'fields': ('turno', 'de_fora'), }), (u'Observações', { 'fields': ('apontamentos', 'obs'), }), - #('Advanced options', { - # 'classes': ('collapse',), - # 'fields': ('enable_comments', 'registration_required', 'template_name') - #}), ) def formfield_for_dbfield(self, db_field, **kwargs): diff --git a/sigi/apps/servidores/models.py b/sigi/apps/servidores/models.py index fe5572b..a54d5cf 100644 --- a/sigi/apps/servidores/models.py +++ b/sigi/apps/servidores/models.py @@ -57,6 +57,7 @@ class Servidor(models.Model): # usuario responsavel pela autenticação do servidor no sistema user = models.ForeignKey(User, unique=True) + user.is_active__filter = True nome_completo = models.CharField(max_length=128) nome_completo.alphabetic_filter = True apelido = models.CharField(max_length=50, blank=True) @@ -98,7 +99,7 @@ class Servidor(models.Model): apontamentos = models.TextField(u'apontamentos', blank=True, null=True) # Informações de contato - email_pessoal = models.EmailField('e-mail pessoal', blank=True, null=True) + email_pessoal = models.EmailField('email pessoal', blank=True, null=True) endereco = generic.GenericRelation('contatos.Endereco') telefones = generic.GenericRelation('contatos.Telefone') ramal = models.CharField(max_length=25, blank=True, null=True)