diff --git a/sapl/base/receivers.py b/sapl/base/receivers.py index 6a1e622be..0813ccf9a 100644 --- a/sapl/base/receivers.py +++ b/sapl/base/receivers.py @@ -4,7 +4,7 @@ import logging from django.conf import settings from django.contrib.contenttypes.models import ContentType 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 from django.db.utils import DEFAULT_DB_ALIAS 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.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() diff --git a/sapl/decorators.py b/sapl/decorators.py index fac94344a..a377c954b 100644 --- a/sapl/decorators.py +++ b/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 diff --git a/sapl/parlamentares/models.py b/sapl/parlamentares/models.py index 807e7c835..096c16814 100644 --- a/sapl/parlamentares/models.py +++ b/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 @@ -311,7 +312,7 @@ class Parlamentar(models.Model): @property def avatar_html(self): return ''if self.fotografia else '' + +self.fotografia.url + '>'if self.fotografia else '' def delete(self, using=None, keep_parents=False): if self.fotografia: @@ -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): '''