add signal handler to auto-delete video file on Video model deletion
This commit is contained in:
parent
bf0785bc78
commit
5510054961
3 changed files with 18 additions and 1 deletions
|
|
@ -4,3 +4,6 @@ from django.apps import AppConfig
|
|||
class ContentConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'content'
|
||||
|
||||
def ready(self):
|
||||
from . import signals
|
||||
|
|
|
|||
14
backend/videoflix/content/signals.py
Normal file
14
backend/videoflix/content/signals.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from .models import Video
|
||||
from django.dispatch import receiver
|
||||
from django.db.models.signals import post_delete
|
||||
import os
|
||||
|
||||
@receiver(post_delete, sender=Video)
|
||||
def auto_delete_file_on_delete(sender, instance, **kwargs):
|
||||
"""
|
||||
Deletes file from filesystem
|
||||
when corresponding `Video` object is deleted.
|
||||
"""
|
||||
if instance.video_file:
|
||||
if os.path.isfile(instance.video_file.path):
|
||||
os.remove(instance.video_file.path)
|
||||
|
|
@ -51,7 +51,7 @@ INSTALLED_APPS = [
|
|||
'rest_framework.authtoken',
|
||||
'rest_framework',
|
||||
'corsheaders',
|
||||
'content',
|
||||
'content.apps.ContentConfig',
|
||||
'users',
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue