from django.apps import AppConfig class ProjectsConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'projects' def ready(self): import shutil from pathlib import Path from django.conf import settings from django.db import transaction from django.db.models.signals import post_delete from .models import CardAttachment, Project def remove_stored_file(sender, instance, **kwargs): if instance.file: instance.file.delete(save=False) post_delete.connect(remove_stored_file, sender=CardAttachment, weak=False, dispatch_uid='projects.remove_attachment_file') def remove_project_mirror(sender, instance, **kwargs): root = Path(settings.PROJECT_DOCS_MIRROR_ROOT) path = root / str(instance.pk) if path.is_symlink() or not path.is_dir(): return transaction.on_commit(lambda: shutil.rmtree(path, ignore_errors=True)) post_delete.connect(remove_project_mirror, sender=Project, weak=False, dispatch_uid='projects.remove_documentation_mirror')