|
@ -1,45 +1,22 @@ |
|
|
# -*- coding: utf-8 -*- |
|
|
from django.db import models |
|
|
from django.contrib import admin |
|
|
from django.contrib import admin |
|
|
from django.contrib.contenttypes import generic |
|
|
from django.utils.safestring import mark_safe |
|
|
from django.utils.translation import gettext as _ |
|
|
from django.utils.translation import gettext as _ |
|
|
|
|
|
|
|
|
from sigi.apps.contatos.models import Endereco, Telefone |
|
|
|
|
|
from sigi.apps.servidores.models import Servidor, Servico |
|
|
from sigi.apps.servidores.models import Servidor, Servico |
|
|
from sigi.apps.utils.admin_widgets import AdminImageWidget |
|
|
from sigi.apps.servidores.filters import ServicoFilter |
|
|
from sigi.apps.utils.base_admin import BaseModelAdmin |
|
|
|
|
|
from sigi.apps.utils.filters import AlphabeticFilter |
|
|
|
|
|
|
|
|
|
|
|
class ServidorFilter(AlphabeticFilter): |
|
|
|
|
|
title = _('Nome do Servidor') |
|
|
|
|
|
parameter_name = 'servidor__nome_completo' |
|
|
|
|
|
|
|
|
|
|
|
class ServicoFilter(admin.SimpleListFilter): |
|
|
|
|
|
title = _("Subordinados à") |
|
|
|
|
|
parameter_name = 'subordinado__id__exact' |
|
|
|
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin): |
|
|
|
|
|
return ([('None', _("Nenhum"))] + |
|
|
|
|
|
[(s.id, s.nome) for s in Servico.objects.exclude(servico=None)]) |
|
|
|
|
|
|
|
|
|
|
|
def queryset(self, request, queryset): |
|
|
|
|
|
if self.value(): |
|
|
|
|
|
if self.value() == "None": |
|
|
|
|
|
queryset = queryset.filter(subordinado=None) |
|
|
|
|
|
else: |
|
|
|
|
|
queryset = queryset.filter(subordinado__id=self.value()) |
|
|
|
|
|
return queryset |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ServicoInline(admin.TabularInline): |
|
|
class ServicoInline(admin.TabularInline): |
|
|
model = Servico |
|
|
model = Servico |
|
|
fields = ['nome', 'sigla', 'responsavel',] |
|
|
fields = ['nome', 'sigla', 'responsavel',] |
|
|
|
|
|
autocomplete_fields = ['responsavel',] |
|
|
|
|
|
|
|
|
class ServidorInline(admin.TabularInline): |
|
|
class ServidorInline(admin.TabularInline): |
|
|
model = Servidor |
|
|
model = Servidor |
|
|
fields = ('imagem_foto', 'nome_completo', 'is_active', ) |
|
|
fields = ('imagem_foto', 'nome_completo', 'is_active', ) |
|
|
readonly_fields = ('imagem_foto', 'nome_completo', 'is_active', ) |
|
|
readonly_fields = ('imagem_foto', 'nome_completo', 'is_active', ) |
|
|
|
|
|
|
|
|
def has_add_permission(self, request): |
|
|
def has_add_permission(self, request, obj): |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj): |
|
|
def has_delete_permission(self, request, obj): |
|
@ -47,11 +24,13 @@ class ServidorInline(admin.TabularInline): |
|
|
|
|
|
|
|
|
def imagem_foto(sels, servidor): |
|
|
def imagem_foto(sels, servidor): |
|
|
if servidor.foto: |
|
|
if servidor.foto: |
|
|
return '<img src="{url}" style="height: 60px; width: 60px; border-radius: 50%;">'.format(url=servidor.foto.url) |
|
|
return mark_safe( |
|
|
|
|
|
f'<img src="{servidor.foto.url}" style="height: 60px; ' |
|
|
|
|
|
f'width: 60px; border-radius: 50%;">' |
|
|
|
|
|
) |
|
|
else: |
|
|
else: |
|
|
return "" |
|
|
return "" |
|
|
imagem_foto.short_description = _("foto") |
|
|
imagem_foto.short_description = _("foto") |
|
|
imagem_foto.allow_tags = True |
|
|
|
|
|
|
|
|
|
|
|
def is_active(self, servidor): |
|
|
def is_active(self, servidor): |
|
|
if servidor.user: |
|
|
if servidor.user: |
|
@ -62,19 +41,19 @@ class ServidorInline(admin.TabularInline): |
|
|
is_active.boolean = True |
|
|
is_active.boolean = True |
|
|
is_active.short_description = _('ativo') |
|
|
is_active.short_description = _('ativo') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Servico) |
|
|
@admin.register(Servico) |
|
|
class ServicoAdmin(admin.ModelAdmin): |
|
|
class ServicoAdmin(admin.ModelAdmin): |
|
|
list_display = ['sigla', 'nome', 'subordinado', 'responsavel'] |
|
|
list_display = ['sigla', 'nome', 'subordinado', 'responsavel'] |
|
|
list_filter = [ServicoFilter,] |
|
|
list_filter = [ServicoFilter,] |
|
|
search_fields = ['nome', 'sigla',] |
|
|
search_fields = ['nome', 'sigla',] |
|
|
|
|
|
autocomplete_fields = ['subordinado', 'responsavel',] |
|
|
inlines = [ServicoInline, ServidorInline,] |
|
|
inlines = [ServicoInline, ServidorInline,] |
|
|
|
|
|
|
|
|
@admin.register(Servidor) |
|
|
@admin.register(Servidor) |
|
|
class ServidorAdmin(BaseModelAdmin): |
|
|
class ServidorAdmin(admin.ModelAdmin): |
|
|
list_display = ('imagem_foto', 'nome_completo', 'is_active', 'servico', ) |
|
|
list_display = ('imagem_foto', 'nome_completo', 'is_active', 'servico', ) |
|
|
list_display_links = ('imagem_foto', 'nome_completo',) |
|
|
list_display_links = ('imagem_foto', 'nome_completo',) |
|
|
list_filter = ('user__is_active', 'externo', 'servico') |
|
|
list_filter = ('user__is_active', 'externo', 'servico',) |
|
|
search_fields = ('nome_completo', 'user__email', 'user__first_name', |
|
|
search_fields = ('nome_completo', 'user__email', 'user__first_name', |
|
|
'user__last_name', 'user__username', 'servico__nome', |
|
|
'user__last_name', 'user__username', 'servico__nome', |
|
|
'servico__sigla') |
|
|
'servico__sigla') |
|
@ -92,16 +71,6 @@ class ServidorAdmin(BaseModelAdmin): |
|
|
return super(ServidorAdmin, self).lookup_allowed(lookup, value) or \ |
|
|
return super(ServidorAdmin, self).lookup_allowed(lookup, value) or \ |
|
|
lookup in ['user__is_active__exact'] |
|
|
lookup in ['user__is_active__exact'] |
|
|
|
|
|
|
|
|
# def has_add_permission(self, request): |
|
|
|
|
|
# return False |
|
|
|
|
|
|
|
|
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs): |
|
|
|
|
|
if db_field.name == 'foto': |
|
|
|
|
|
request = kwargs.pop("request", None) |
|
|
|
|
|
kwargs['widget'] = AdminImageWidget |
|
|
|
|
|
return db_field.formfield(**kwargs) |
|
|
|
|
|
return super(ServidorAdmin, self).formfield_for_dbfield(db_field, **kwargs) |
|
|
|
|
|
|
|
|
|
|
|
def is_active(self, servidor): |
|
|
def is_active(self, servidor): |
|
|
if servidor.user: |
|
|
if servidor.user: |
|
|
return servidor.user.is_active |
|
|
return servidor.user.is_active |
|
@ -113,8 +82,10 @@ class ServidorAdmin(BaseModelAdmin): |
|
|
|
|
|
|
|
|
def imagem_foto(sels, servidor): |
|
|
def imagem_foto(sels, servidor): |
|
|
if servidor.foto: |
|
|
if servidor.foto: |
|
|
return '<img src="{url}" style="height: 60px; width: 60px; border-radius: 50%;">'.format(url=servidor.foto.url) |
|
|
return mark_safe( |
|
|
|
|
|
f'<img src="{servidor.foto.url}" style="height: 60px; ' |
|
|
|
|
|
f'width: 60px; border-radius: 50%;">' |
|
|
|
|
|
) |
|
|
else: |
|
|
else: |
|
|
return "" |
|
|
return "" |
|
|
imagem_foto.short_description = _("foto") |
|
|
imagem_foto.short_description = _("foto") |
|
|
imagem_foto.allow_tags = True |
|
|
|