From d0bd3822d407f448d4e19f42197fc1ae5dc9e1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Tue, 15 Jun 2021 17:05:40 -0300 Subject: [PATCH] App Servidores preparada para django 3 --- requirements/requirements.txt | 1 + sigi/apps/servidores/admin.py | 70 +++++-------------- .../servidores/migrations/0001_initial.py | 16 ++--- .../migrations/0006_auto_20210429_0822.py | 2 +- sigi/apps/servidores/models.py | 32 ++++----- .../servidores/servidor/change_form.html | 2 +- .../servidores/servidor/change_list.html | 14 ---- sigi/settings/base.py | 2 +- sigi/urls.py | 2 +- 9 files changed, 47 insertions(+), 94 deletions(-) delete mode 100644 sigi/apps/servidores/templates/admin/servidores/servidor/change_list.html diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 576d881..6bea0ba 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -2,3 +2,4 @@ ipython==7.24.1 Django==3.2.4 psycopg2==2.8.6 django-bootstrap5==2.0.1 +Pillow==8.2.0 diff --git a/sigi/apps/servidores/admin.py b/sigi/apps/servidores/admin.py index 7d19536..23fddf5 100644 --- a/sigi/apps/servidores/admin.py +++ b/sigi/apps/servidores/admin.py @@ -1,34 +1,9 @@ -# -*- coding: utf-8 -*- from django.contrib import admin -from django.contrib.contenttypes import generic +from django.contrib.admin.filters import RelatedOnlyFieldListFilter +from django.utils.safestring import mark_safe from django.utils.translation import ugettext as _ -from sigi.apps.contatos.models import Endereco, Telefone from sigi.apps.servidores.models import Servidor, Servico -from sigi.apps.utils.admin_widgets import AdminImageWidget -from sigi.apps.utils.base_admin import BaseModelAdmin -from sigi.apps.utils.filters import AlphabeticFilter - -class ServidorFilter(AlphabeticFilter): - title = _(u'Nome do Servidor') - parameter_name = 'servidor__nome_completo' - -class ServicoFilter(admin.SimpleListFilter): - title = _(u"Subordinados à") - parameter_name = 'subordinado__id__exact' - - def lookups(self, request, model_admin): - return ([('None', _(u"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): model = Servico @@ -39,7 +14,7 @@ class ServidorInline(admin.TabularInline): 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 def has_delete_permission(self, request, obj): @@ -47,11 +22,12 @@ class ServidorInline(admin.TabularInline): def imagem_foto(sels, servidor): if servidor.foto: - return u''.format(url=servidor.foto.url) + return mark_safe(''.format( + url=servidor.foto.url)) else: - return u"" - imagem_foto.short_description = _(u"foto") - imagem_foto.allow_tags = True + return "" + imagem_foto.short_description = _("foto") def is_active(self, servidor): if servidor.user: @@ -60,18 +36,17 @@ class ServidorInline(admin.TabularInline): return False is_active.admin_order_field = 'user__is_active' is_active.boolean = True - is_active.short_description = _(u'ativo') - + is_active.short_description = _('ativo') @admin.register(Servico) class ServicoAdmin(admin.ModelAdmin): list_display = ['sigla', 'nome', 'subordinado', 'responsavel'] - list_filter = [ServicoFilter,] + list_filter = [('subordinado', RelatedOnlyFieldListFilter),] search_fields = ['nome', 'sigla',] inlines = [ServicoInline, ServidorInline,] @admin.register(Servidor) -class ServidorAdmin(BaseModelAdmin): +class ServidorAdmin(admin.ModelAdmin): list_display = ('imagem_foto', 'nome_completo', 'is_active', 'servico', ) list_display_links = ('imagem_foto', 'nome_completo',) list_filter = ('user__is_active', 'externo', 'servico') @@ -83,7 +58,7 @@ class ServidorAdmin(BaseModelAdmin): (None, { 'fields': ('user', 'nome_completo', 'foto', 'servico',) }), - (_(u"outros órgãos"), { + (_("outros órgãos"), { 'fields': ('externo', 'orgao_origem', 'qualificacoes'), }), ) @@ -92,16 +67,6 @@ class ServidorAdmin(BaseModelAdmin): return super(ServidorAdmin, self).lookup_allowed(lookup, value) or \ 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): if servidor.user: return servidor.user.is_active @@ -109,12 +74,13 @@ class ServidorAdmin(BaseModelAdmin): return False is_active.admin_order_field = 'user__is_active' is_active.boolean = True - is_active.short_description = _(u'ativo') + is_active.short_description = _('ativo') def imagem_foto(sels, servidor): if servidor.foto: - return u''.format(url=servidor.foto.url) + return mark_safe(''.format( + url=servidor.foto.url)) else: - return u"" - imagem_foto.short_description = _(u"foto") - imagem_foto.allow_tags = True \ No newline at end of file + return "" + imagem_foto.short_description = _("foto") \ No newline at end of file diff --git a/sigi/apps/servidores/migrations/0001_initial.py b/sigi/apps/servidores/migrations/0001_initial.py index cae4cc6..edc31a1 100644 --- a/sigi/apps/servidores/migrations/0001_initial.py +++ b/sigi/apps/servidores/migrations/0001_initial.py @@ -97,8 +97,8 @@ class Migration(migrations.Migration): ('apontamentos', models.TextField(null=True, verbose_name='apontamentos', blank=True)), ('email_pessoal', models.EmailField(max_length=75, null=True, verbose_name=b'email pessoal', blank=True)), ('ramal', models.CharField(max_length=25, null=True, blank=True)), - ('servico', models.ForeignKey(blank=True, to='servidores.Servico', null=True)), - ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, unique=True)), + ('servico', models.ForeignKey(blank=True, to='servidores.Servico', null=True, on_delete=models.CASCADE)), + ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, unique=True, on_delete=models.CASCADE)), ], options={ 'ordering': ('nome_completo',), @@ -112,7 +112,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('nome', models.CharField(max_length=250, null=True)), ('sigla', models.CharField(max_length=10, null=True)), - ('responsavel', models.ForeignKey(related_name=b'diretor', to='servidores.Servidor', null=True)), + ('responsavel', models.ForeignKey(related_name=b'diretor', to='servidores.Servidor', null=True, on_delete=models.CASCADE)), ], options={ 'ordering': ('nome',), @@ -122,31 +122,31 @@ class Migration(migrations.Migration): migrations.AddField( model_name='servico', name='responsavel', - field=models.ForeignKey(related_name=b'chefe', to='servidores.Servidor', null=True), + field=models.ForeignKey(related_name=b'chefe', to='servidores.Servidor', null=True, on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='servico', name='subsecretaria', - field=models.ForeignKey(to='servidores.Subsecretaria', null=True), + field=models.ForeignKey(to='servidores.Subsecretaria', null=True, on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='licenca', name='servidor', - field=models.ForeignKey(to='servidores.Servidor'), + field=models.ForeignKey(to='servidores.Servidor', on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='funcao', name='servidor', - field=models.ForeignKey(to='servidores.Servidor'), + field=models.ForeignKey(to='servidores.Servidor', on_delete=models.CASCADE), preserve_default=True, ), migrations.AddField( model_name='ferias', name='servidor', - field=models.ForeignKey(to='servidores.Servidor'), + field=models.ForeignKey(to='servidores.Servidor', on_delete=models.CASCADE), preserve_default=True, ), ] diff --git a/sigi/apps/servidores/migrations/0006_auto_20210429_0822.py b/sigi/apps/servidores/migrations/0006_auto_20210429_0822.py index 72737ed..d50e2cb 100644 --- a/sigi/apps/servidores/migrations/0006_auto_20210429_0822.py +++ b/sigi/apps/servidores/migrations/0006_auto_20210429_0822.py @@ -15,7 +15,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='servidor', name='user', - field=models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, null=True), + field=models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE), preserve_default=True, ), ] diff --git a/sigi/apps/servidores/models.py b/sigi/apps/servidores/models.py index 0915855..a239480 100644 --- a/sigi/apps/servidores/models.py +++ b/sigi/apps/servidores/models.py @@ -1,19 +1,17 @@ -# -*- coding: utf-8 -*- from django.contrib.auth.models import User -from django.contrib.contenttypes import generic from django.db import models from django.db.models.signals import post_save from django.utils.translation import ugettext as _ class Servico(models.Model): - nome = models.CharField(_(u'Setor'), max_length=250, null=True) + nome = models.CharField(_('setor'), max_length=250, null=True) sigla = models.CharField(max_length=10, null=True) subordinado = models.ForeignKey( 'self', on_delete=models.SET_NULL, null=True, blank=True, - verbose_name=_(u"subordinado a") + verbose_name=_("subordinado a") ) responsavel = models.ForeignKey( 'servidores.Servidor', @@ -25,11 +23,11 @@ class Servico(models.Model): class Meta: ordering = ('-subordinado__sigla', 'nome',) - verbose_name = _(u'serviço') - verbose_name_plural = _(u'serviços') + verbose_name = _('serviço') + verbose_name_plural = _('serviços') - def __unicode__(self): - return u"{sigla} - {nome}".format(sigla=self.sigla, nome=self.nome) + def __str__(self): + return "{sigla} - {nome}".format(sigla=self.sigla, nome=self.nome) class Servidor(models.Model): user = models.ForeignKey( @@ -38,9 +36,10 @@ class Servidor(models.Model): null=True, blank=True ) - nome_completo = models.CharField(max_length=128) - apelido = models.CharField(max_length=50, blank=True) + nome_completo = models.CharField(_('nome completo'), max_length=128) + apelido = models.CharField(_('apelido'), max_length=50, blank=True) foto = models.ImageField( + _('foto'), upload_to='fotos/servidores', width_field='foto_largura', height_field='foto_altura', @@ -52,20 +51,21 @@ class Servidor(models.Model): Servico, on_delete=models.SET_NULL, blank=True, - null=True + null=True, + verbose_name=_('serviço') ) - externo = models.BooleanField(_(u"colaborador externo"), default=False) + externo = models.BooleanField(_("colaborador externo"), default=False) orgao_origem = models.CharField( - _(u"órgão de origem, "), + _("órgão de origem, "), max_length=100, blank=True ) - qualificacoes = models.TextField(_(u"qualificações"), blank=True) + qualificacoes = models.TextField(_("qualificações"), blank=True) class Meta: ordering = ('nome_completo',) - verbose_name_plural = 'servidores' + verbose_name_plural = _('servidores') - def __unicode__(self): + def __str__(self): return self.nome_completo def save(self, *args, **kwargs): diff --git a/sigi/apps/servidores/templates/admin/servidores/servidor/change_form.html b/sigi/apps/servidores/templates/admin/servidores/servidor/change_form.html index b8fe1b2..6c3ed89 100644 --- a/sigi/apps/servidores/templates/admin/servidores/servidor/change_form.html +++ b/sigi/apps/servidores/templates/admin/servidores/servidor/change_form.html @@ -1,4 +1,4 @@ -{% extends 'base_change_form.html' %} +{% extends 'admin/change_form.html' %} {% load i18n %} {% block form_top %} diff --git a/sigi/apps/servidores/templates/admin/servidores/servidor/change_list.html b/sigi/apps/servidores/templates/admin/servidores/servidor/change_list.html deleted file mode 100644 index 0ed52dc..0000000 --- a/sigi/apps/servidores/templates/admin/servidores/servidor/change_list.html +++ /dev/null @@ -1,14 +0,0 @@ -{% extends 'admin/change_list.html' %} -{% load i18n reporting_tags %} - -{% block object-tools-items %} -
  • - - {% trans 'Relatório por cargo' %} -
  • -
  • - - {% trans 'Relatório por função' %} -
  • - {{ block.super }} -{% endblock %} diff --git a/sigi/settings/base.py b/sigi/settings/base.py index 397b094..0039935 100644 --- a/sigi/settings/base.py +++ b/sigi/settings/base.py @@ -26,7 +26,7 @@ INSTALLED_APPS = [ 'sigi.apps.utils', # 'sigi.apps.home', 'sigi.apps.contatos', - # 'sigi.apps.servidores', + 'sigi.apps.servidores', # 'sigi.apps.parlamentares', # 'sigi.apps.casas', # 'sigi.apps.convenios', diff --git a/sigi/urls.py b/sigi/urls.py index 1d6b3eb..688bda0 100644 --- a/sigi/urls.py +++ b/sigi/urls.py @@ -19,7 +19,7 @@ from django.views.static import serve from django.urls import path urlpatterns = [ - path('admin/', admin.site.urls), + path('', admin.site.urls), ] if settings.DEBUG: