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. 19
      sapl/base/receivers.py
  2. 40
      sapl/decorators.py
  3. 5
      sapl/parlamentares/models.py

19
sapl/base/receivers.py

@ -4,7 +4,7 @@ import logging
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import serializers from django.core import serializers
from django.db.models.signals import post_delete, post_save,\ from django.db.models.signals import post_delete, post_save, \
post_migrate post_migrate
from django.db.utils import DEFAULT_DB_ALIAS from django.db.utils import DEFAULT_DB_ALIAS
from django.dispatch import receiver from django.dispatch import receiver
@ -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.email_utils import do_envia_email_tramitacao
from sapl.base.models import AuditLog, TipoAutor, Autor from sapl.base.models import AuditLog, TipoAutor, Autor
from sapl.decorators import receiver_multi_senders
from sapl.materia.models import Tramitacao from sapl.materia.models import Tramitacao
from sapl.protocoloadm.models import TramitacaoAdministrativo from sapl.protocoloadm.models import TramitacaoAdministrativo
from sapl.utils import get_base_url, models_with_gr_for_model 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=Tramitacao)
@receiver(post_save, sender=TramitacaoAdministrativo) @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, def cria_models_tipo_autor(app_config=None, verbosity=2, interactive=True,
using=DEFAULT_DB_ALIAS, **kwargs): using=DEFAULT_DB_ALIAS, **kwargs):
models = models_with_gr_for_model(Autor)
print("\n\033[93m\033[1m{}\033[0m".format( print("\n\033[93m\033[1m{}\033[0m".format(
_('Atualizando registros TipoAutor do SAPL:'))) _('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) content_type = ContentType.objects.get_for_model(model)
tipo_autor = TipoAutor.objects.filter( tipo_autor = TipoAutor.objects.filter(
content_type=content_type.id).exists() 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'. * A classe precisa conter os atributos 'data_inicio' e 'data_fim'.
* 'data_inicio' e 'data_fim' precisam ser do tipo models.DateField * 'data_inicio' e 'data_fim' precisam ser do tipo models.DateField
""" """
@wraps(decorated_method) @wraps(decorated_method)
def display_atual(self): def display_atual(self):
string_displayed = decorated_method(self) string_displayed = decorated_method(self)
@ -41,3 +42,42 @@ def vigencia_atual(decorated_method):
return string_displayed return string_displayed
return display_atual 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

5
sapl/parlamentares/models.py

@ -4,6 +4,7 @@ from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from image_cropping.fields import ImageCropField, ImageRatioField from image_cropping.fields import ImageCropField, ImageRatioField
from model_utils import Choices from model_utils import Choices
from prompt_toolkit.key_binding.bindings.named_commands import self_insert
import reversion import reversion
from sapl.base.models import Autor from sapl.base.models import Autor
@ -311,7 +312,7 @@ class Parlamentar(models.Model):
@property @property
def avatar_html(self): def avatar_html(self):
return '<img class="avatar-parlamentar" src='\ return '<img class="avatar-parlamentar" src='\
+ self.fotografia.url + '>'if self.fotografia else '' +self.fotografia.url + '>'if self.fotografia else ''
def delete(self, using=None, keep_parents=False): def delete(self, using=None, keep_parents=False):
if self.fotografia: if self.fotografia:
@ -496,6 +497,7 @@ class CargoMesa(models.Model):
def __str__(self): def __str__(self):
return self.descricao return self.descricao
@reversion.register() @reversion.register()
class MesaDiretora(models.Model): class MesaDiretora(models.Model):
data_inicio = models.DateField(verbose_name=_('Data Início'), null=True) 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 'parlamentar': self.parlamentar, 'cargo': self.cargo
} }
@reversion.register() @reversion.register()
class Frente(models.Model): class Frente(models.Model):
''' '''

Loading…
Cancel
Save