From 0f8e03b08bc9cbee085a9a557a54425e0e5f6176 Mon Sep 17 00:00:00 2001 From: Leandro Roberto Date: Thu, 14 Mar 2019 20:28:10 -0300 Subject: [PATCH] add variavel em signal e move outro de arquivo --- sapl/base/consumers.py | 3 --- sapl/base/models.py | 45 ++++-------------------------------------- sapl/base/receivers.py | 8 +++----- sapl/base/signals.py | 45 ++++++++++++++++++++++++++++++++++++++++++ sapl/rules/apps.py | 8 ++++---- 5 files changed, 56 insertions(+), 53 deletions(-) diff --git a/sapl/base/consumers.py b/sapl/base/consumers.py index 7b0b64c4b..ddcad548f 100644 --- a/sapl/base/consumers.py +++ b/sapl/base/consumers.py @@ -2,9 +2,6 @@ import json from asgiref.sync import async_to_sync from channels.generic.websocket import AsyncWebsocketConsumer, WebsocketConsumer -from channels.layers import get_channel_layer -from django.db.models.signals import post_save, post_delete, pre_delete -from django.dispatch.dispatcher import receiver class TimeRefreshConsumer(AsyncWebsocketConsumer): diff --git a/sapl/base/models.py b/sapl/base/models.py index 87e497e84..462ed9153 100644 --- a/sapl/base/models.py +++ b/sapl/base/models.py @@ -1,13 +1,12 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models -from django.db.models.signals import post_migrate -from django.db.utils import DEFAULT_DB_ALIAS from django.utils.translation import ugettext_lazy as _ import reversion from sapl.utils import (LISTA_DE_UFS, YES_NO_CHOICES, - get_settings_auth_user_model, models_with_gr_for_model) + get_settings_auth_user_model) + DOC_ADM_OSTENSIVO = 'O' DOC_ADM_RESTRITIVO = 'R' @@ -165,7 +164,8 @@ class AppConfig(models.Model): choices=YES_NO_CHOICES, default=False) escolher_numero_materia_proposicao = models.BooleanField( - verbose_name=_('Indicar número da matéria a ser gerada na proposição?'), + verbose_name=_( + 'Indicar número da matéria a ser gerada na proposição?'), choices=YES_NO_CHOICES, default=False) class Meta: @@ -253,40 +253,3 @@ class Autor(models.Model): if self.user: return str(self.user.username) return '?' - - -def cria_models_tipo_autor(app_config=None, verbosity=2, interactive=True, - using=DEFAULT_DB_ALIAS, **kwargs): - - models = models_with_gr_for_model(Autor) - - print("\n\033[93m\033[1m{}\033[0m".format( - _('Atualizando registros TipoAutor do SAPL:'))) - for model in models: - content_type = ContentType.objects.get_for_model(model) - tipo_autor = TipoAutor.objects.filter( - content_type=content_type.id).exists() - - if tipo_autor: - msg1 = "Carga de {} não efetuada.".format( - TipoAutor._meta.verbose_name) - msg2 = " Já Existe um {} {} relacionado...".format( - TipoAutor._meta.verbose_name, - model._meta.verbose_name) - msg = " {}{}".format(msg1, msg2) - else: - novo_autor = TipoAutor() - novo_autor.content_type_id = content_type.id - novo_autor.descricao = model._meta.verbose_name - novo_autor.save() - msg1 = "Carga de {} efetuada.".format( - TipoAutor._meta.verbose_name) - msg2 = " {} {} criado...".format( - TipoAutor._meta.verbose_name, content_type.model) - msg = " {}{}".format(msg1, msg2) - print(msg) - # Disconecta função para evitar a chamada repetidas vezes. - post_migrate.disconnect(receiver=cria_models_tipo_autor) - - -post_migrate.connect(receiver=cria_models_tipo_autor) diff --git a/sapl/base/receivers.py b/sapl/base/receivers.py index b2176be14..a45c0bc44 100644 --- a/sapl/base/receivers.py +++ b/sapl/base/receivers.py @@ -1,13 +1,11 @@ -from django.db.models.signals import post_delete, post_save +from django.db.models.signals import post_delete from django.dispatch import receiver -from sapl.materia.models import Tramitacao -from sapl.protocoloadm.models import TramitacaoAdministrativo +from sapl.base.email_utils import do_envia_email_tramitacao from sapl.base.signals import tramitacao_signal +from sapl.protocoloadm.models import TramitacaoAdministrativo from sapl.utils import get_base_url -from sapl.base.email_utils import do_envia_email_tramitacao - @receiver(tramitacao_signal) def handle_tramitacao_signal(sender, **kwargs): diff --git a/sapl/base/signals.py b/sapl/base/signals.py index 3381ff3a6..b2cda2028 100644 --- a/sapl/base/signals.py +++ b/sapl/base/signals.py @@ -1,3 +1,48 @@ +from django.contrib.contenttypes.models import ContentType +from django.db.models.signals import post_migrate +from django.db.utils import DEFAULT_DB_ALIAS import django.dispatch +from django.utils.translation import ugettext_lazy as _ +from sapl.base.models import Autor, TipoAutor +from sapl.utils import models_with_gr_for_model + + tramitacao_signal = django.dispatch.Signal(providing_args=['post', 'request']) + + +def cria_models_tipo_autor(app_config=None, verbosity=2, interactive=True, + using=DEFAULT_DB_ALIAS, **kwargs): + + models = models_with_gr_for_model(Autor) + + print("\n\033[93m\033[1m{}\033[0m".format( + _('Atualizando registros TipoAutor do SAPL:'))) + for model in models: + content_type = ContentType.objects.get_for_model(model) + tipo_autor = TipoAutor.objects.filter( + content_type=content_type.id).exists() + + if tipo_autor: + msg1 = "Carga de {} não efetuada.".format( + TipoAutor._meta.verbose_name) + msg2 = " Já Existe um {} {} relacionado...".format( + TipoAutor._meta.verbose_name, + model._meta.verbose_name) + msg = " {}{}".format(msg1, msg2) + else: + novo_autor = TipoAutor() + novo_autor.content_type_id = content_type.id + novo_autor.descricao = model._meta.verbose_name + novo_autor.save() + msg1 = "Carga de {} efetuada.".format( + TipoAutor._meta.verbose_name) + msg2 = " {} {} criado...".format( + TipoAutor._meta.verbose_name, content_type.model) + msg = " {}{}".format(msg1, msg2) + print(msg) + # Disconecta função para evitar a chamada repetidas vezes. + post_migrate.disconnect(receiver=cria_models_tipo_autor) + + +post_migrate.connect(receiver=cria_models_tipo_autor) diff --git a/sapl/rules/apps.py b/sapl/rules/apps.py index 6b256b5db..48f8c4a41 100644 --- a/sapl/rules/apps.py +++ b/sapl/rules/apps.py @@ -257,13 +257,13 @@ def cria_usuarios_padrao(): rules.cria_usuarios_padrao() -def send_signal_for_websocket_time_refresh(inst, action): +def send_signal_for_websocket_time_refresh(project, action, inst): if not settings.USE_CHANNEL_LAYERS: return if hasattr(inst, '_meta') and not inst._meta.app_config is None and \ - inst._meta.app_config.name[:4] == 'sapl': + inst._meta.app_config.name[:4] == project: # um mensagem não deve ser enviada se é post_save mas originou se de # revision_pre_delete_signal @@ -307,12 +307,12 @@ def revision_pre_delete_signal(sender, **kwargs): @receiver(post_save, dispatch_uid='sapl_post_save_signal') def sapl_post_save_signal(sender, instance, using, **kwargs): - send_signal_for_websocket_time_refresh(instance, 'post_save') + send_signal_for_websocket_time_refresh('sapl', 'post_save', instance) @receiver(post_delete, dispatch_uid='sapl_post_delete_signal') def sapl_post_delete_signal(sender, instance, using, **kwargs): - send_signal_for_websocket_time_refresh(instance, 'post_delete') + send_signal_for_websocket_time_refresh('sapl', 'post_delete', instance) models.signals.post_migrate.connect(