47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
|
|
from api.notificaciones.models import Notificacion
|
|
from api.record.models import Document
|
|
|
|
|
|
@receiver(post_save, sender=Document)
|
|
def trigger_notificacion(sender, instance, created, **kwargs):
|
|
if not created:
|
|
return
|
|
|
|
from api.cuser.models import CustomUser
|
|
from api.notificaciones.models import TipoNotificacion
|
|
from core.permissions import user_has_permission
|
|
|
|
tipo_info, _ = TipoNotificacion.objects.get_or_create(
|
|
tipo='info',
|
|
defaults={'descripcion': 'Notificación informativa'},
|
|
)
|
|
|
|
mensaje = (
|
|
f"Se agregó el documento {instance.archivo} "
|
|
f"al pedimento {instance.pedimento.pedimento}\n"
|
|
f"{instance.document_type.nombre}"
|
|
)
|
|
|
|
usuarios_org = CustomUser.objects.filter(
|
|
organizacion=instance.organizacion,
|
|
is_active=True,
|
|
).prefetch_related('rfc')
|
|
|
|
for usuario in usuarios_org:
|
|
if not user_has_permission(usuario, 'notificaciones.receive'):
|
|
continue
|
|
|
|
# Importadores: solo si el pedimento corresponde a uno de sus RFC
|
|
if usuario.is_importador:
|
|
if instance.pedimento.contribuyente not in usuario.rfc.all():
|
|
continue
|
|
|
|
Notificacion.objects.create(
|
|
tipo=tipo_info,
|
|
dirigido=usuario,
|
|
mensaje=mensaje,
|
|
)
|