Corp/backend/tasks/signals.py

24 lines
782 B
Python

from django.db import transaction
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from .models import Task, TaskAttachment
@receiver(post_save, sender=Task)
def ensure_task_core_participants(sender, instance, **kwargs):
participant_ids = [instance.created_by_id]
if instance.assignee_id:
participant_ids.append(instance.assignee_id)
instance.participants.add(*participant_ids)
@receiver(post_delete, sender=TaskAttachment)
def delete_task_attachment_file(sender, instance, **kwargs):
if not instance.file or not instance.file.name:
return
storage = instance.file.storage
name = instance.file.name
transaction.on_commit(lambda: storage.delete(name) if storage.exists(name) else None)