Browse Source

Fix #3422

cria decorator receiver multi senders e aplica a função com todos os
autor_relateds pesquisados buscados via SaplGenericRelation
pull/3453/head
Leandro Roberto 3 years ago
parent
commit
a87879d60e
  1. 17
      sapl/base/receivers.py
  2. 40
      sapl/decorators.py
  3. 3
      sapl/parlamentares/models.py

17
sapl/base/receivers.py

@ -13,10 +13,23 @@ from django.utils.translation import ugettext_lazy as _
from sapl.base.email_utils import do_envia_email_tramitacao
from sapl.base.models import AuditLog, TipoAutor, Autor
from sapl.decorators import receiver_multi_senders
from sapl.materia.models import Tramitacao
from sapl.protocoloadm.models import TramitacaoAdministrativo
from sapl.utils import get_base_url, models_with_gr_for_model
models_with_gr_for_autor = models_with_gr_for_model(Autor)
@receiver_multi_senders(post_save, senders=models_with_gr_for_autor)
def handle_update_autor_related(sender, **kwargs):
# for m in models_with_gr_for_autor:
instance = kwargs.get('instance')
autor = instance.autor.first()
if autor:
autor.nome = str(instance)
autor.save()
@receiver(post_save, sender=Tramitacao)
@receiver(post_save, sender=TramitacaoAdministrativo)
@ -143,11 +156,9 @@ def audit_log_post_save(sender, **kwargs):
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:
for model in models_with_gr_for_autor:
content_type = ContentType.objects.get_for_model(model)
tipo_autor = TipoAutor.objects.filter(
content_type=content_type.id).exists()

40
sapl/decorators.py

@ -13,6 +13,7 @@ def vigencia_atual(decorated_method):
* A classe precisa conter os atributos 'data_inicio' e 'data_fim'.
* 'data_inicio' e 'data_fim' precisam ser do tipo models.DateField
"""
@wraps(decorated_method)
def display_atual(self):
string_displayed = decorated_method(self)
@ -41,3 +42,42 @@ def vigencia_atual(decorated_method):
return string_displayed
return display_atual
def receiver_multi_senders(signal, **kwargs):
"""
A decorator for connecting receivers to signals. Used by passing in the
signal (or list of signals) and keyword arguments to connect::
@receiver(post_save, senders=MyModelsList)
def signal_receiver(sender, **kwargs):
...
@receiver([post_save, post_delete], senders=MyModelsList)
def signals_receiver(sender, **kwargs):
...
"""
def _decorator(func):
senders = kwargs.get('senders', [])
if isinstance(signal, (list, tuple)):
if not senders:
for s in signal:
s.connect(func, **kwargs)
else:
senders = kwargs.pop('senders')
for sender in senders:
for s in signal:
s.connect(func, sender=sender, **kwargs)
else:
if not senders:
signal.connect(func, **kwargs)
else:
senders = kwargs.pop('senders')
for sender in senders:
signal.connect(func, sender=sender, **kwargs)
return func
return _decorator

3
sapl/parlamentares/models.py

@ -4,6 +4,7 @@ from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from image_cropping.fields import ImageCropField, ImageRatioField
from model_utils import Choices
from prompt_toolkit.key_binding.bindings.named_commands import self_insert
import reversion
from sapl.base.models import Autor
@ -496,6 +497,7 @@ class CargoMesa(models.Model):
def __str__(self):
return self.descricao
@reversion.register()
class MesaDiretora(models.Model):
data_inicio = models.DateField(verbose_name=_('Data Início'), null=True)
@ -532,6 +534,7 @@ class ComposicaoMesa(models.Model):
'parlamentar': self.parlamentar, 'cargo': self.cargo
}
@reversion.register()
class Frente(models.Model):
'''

Loading…
Cancel
Save