Browse Source

add variavel em signal e move outro de arquivo

pull/2601/head
Leandro Roberto 6 years ago
parent
commit
0f8e03b08b
  1. 3
      sapl/base/consumers.py
  2. 45
      sapl/base/models.py
  3. 8
      sapl/base/receivers.py
  4. 45
      sapl/base/signals.py
  5. 8
      sapl/rules/apps.py

3
sapl/base/consumers.py

@ -2,9 +2,6 @@ import json
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from channels.generic.websocket import AsyncWebsocketConsumer, WebsocketConsumer 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): class TimeRefreshConsumer(AsyncWebsocketConsumer):

45
sapl/base/models.py

@ -1,13 +1,12 @@
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db import models 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 _ from django.utils.translation import ugettext_lazy as _
import reversion import reversion
from sapl.utils import (LISTA_DE_UFS, YES_NO_CHOICES, 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_OSTENSIVO = 'O'
DOC_ADM_RESTRITIVO = 'R' DOC_ADM_RESTRITIVO = 'R'
@ -165,7 +164,8 @@ class AppConfig(models.Model):
choices=YES_NO_CHOICES, default=False) choices=YES_NO_CHOICES, default=False)
escolher_numero_materia_proposicao = models.BooleanField( 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) choices=YES_NO_CHOICES, default=False)
class Meta: class Meta:
@ -253,40 +253,3 @@ class Autor(models.Model):
if self.user: if self.user:
return str(self.user.username) return str(self.user.username)
return '?' 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)

8
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 django.dispatch import receiver
from sapl.materia.models import Tramitacao from sapl.base.email_utils import do_envia_email_tramitacao
from sapl.protocoloadm.models import TramitacaoAdministrativo
from sapl.base.signals import tramitacao_signal from sapl.base.signals import tramitacao_signal
from sapl.protocoloadm.models import TramitacaoAdministrativo
from sapl.utils import get_base_url from sapl.utils import get_base_url
from sapl.base.email_utils import do_envia_email_tramitacao
@receiver(tramitacao_signal) @receiver(tramitacao_signal)
def handle_tramitacao_signal(sender, **kwargs): def handle_tramitacao_signal(sender, **kwargs):

45
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 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']) 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)

8
sapl/rules/apps.py

@ -257,13 +257,13 @@ def cria_usuarios_padrao():
rules.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: if not settings.USE_CHANNEL_LAYERS:
return return
if hasattr(inst, '_meta') and not inst._meta.app_config is None and \ 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 # um mensagem não deve ser enviada se é post_save mas originou se de
# revision_pre_delete_signal # revision_pre_delete_signal
@ -307,12 +307,12 @@ def revision_pre_delete_signal(sender, **kwargs):
@receiver(post_save, dispatch_uid='sapl_post_save_signal') @receiver(post_save, dispatch_uid='sapl_post_save_signal')
def sapl_post_save_signal(sender, instance, using, **kwargs): 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') @receiver(post_delete, dispatch_uid='sapl_post_delete_signal')
def sapl_post_delete_signal(sender, instance, using, **kwargs): 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( models.signals.post_migrate.connect(

Loading…
Cancel
Save